13. Start and stop a PLC
import PLCCom.*;
import javax.swing.JOptionPane;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
//Start PLC
private void btnwriteRawDataActionPerformed(java.awt.event.ActionEvent evt) {
//execute function
OperationResult res = Device.StartPLC();
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
//Stop PLC
private void btnwriteRawDataActionPerformed(java.awt.event.ActionEvent evt) {
//execute function
OperationResult res = Device.StopPLC();
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
}
14. Reading block list from PLC
import PLCCom.*;
import javax.swing.JOptionPane;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btngetPLCBlockListActionPerformed(java.awt.event.ActionEvent evt) {
eBlockType BlockType = eBlockType.AllBlocks;
BlockListResult res = Device.GetBlockList(BlockType);
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
StringBuilder sb = new StringBuilder();
for (BlockListEntry ble : res.getBlockList()) {
sb.append(ble.getBlockType().toString());
sb.append(String.valueOf(ble.getBlockNumber()));
sb.append(System.getProperty("line.separator"));
}
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
}
15. Get length of block
import PLCCom.*;
import javax.swing.JOptionPane;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnBlockLenActionPerformed(java.awt.event.ActionEvent evt) {
//get Len from DB100
int BlockNumber = 100;
eBlockType BlockType = eBlockType.AllBlocks;
//evaluate results
BlockListLengthResult res = Device.GetBlockLenght(BlockType, BlockNumber);
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
StringBuilder sb = new StringBuilder();
sb.append(res.getBlockType().toString());
sb.append(String.valueOf(res.getBlockNumber()));
sb.append(" Len:");
sb.append(String.valueOf(res.getBlockLength()));
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
}
16. Backup block
import PLCCom.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnBackup_BlockActionPerformed(java.awt.event.ActionEvent evt) {
//Backup OB1
int BlockNumber = 1;
eBlockType BlockType = eBlockType.OB;
//open SaveFileDialog
final JFileChooser dr = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Binary Files *.mc7", "mc7");
dr.addChoosableFileFilter(filter);
int returnVal = dr.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//read Block into ReadPLCBlockResult
ReadPLCBlockResult res = Device.ReadPLCBlock_MC7(BlockType, BlockNumber);
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
try {
//save buffer in specified file
File file = new File(dr.getSelectedFile().getAbsolutePath());
//rename file to .mc7, you can adjust the extension
if (!file.getAbsolutePath().endsWith(".mc7")) {
file = new File(dr.getSelectedFile().getAbsolutePath() + ".mc7");
}
// if file doesn´t exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fs = new FileOutputStream(file);
fs.write(res.getBuffer());
fs.close();
JOptionPane.showMessageDialog(null, "Block " + String.valueOf(String.valueOf(BlockType)) + String.valueOf(BlockNumber) + resources.getString("successful_saved") + dr.getSelectedFile().getName(), "", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "operation unsuccessful", "", JOptionPane.ERROR_MESSAGE);
}
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "operation aborted", "", JOptionPane.ERROR_MESSAGE);
}
}
}
17. Restore block
import PLCCom.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnRestore_BlockActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser dr = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Binary Files *.mc7, *.bin", "mc7", "bin");
dr.addChoosableFileFilter(filter);
int returnVal = dr.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = new File(dr.getSelectedFile().getAbsolutePath());
if (!file.exists()) {
JOptionPane.showMessageDialog(null, "operation unsuccessful, file not exist", "", JOptionPane.ERROR_MESSAGE);
return;
}
InputStream is = null;
byte[] buffer = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
buffer = new byte[is.available()];
is.read(buffer);
} finally {
is.close();
}
WritePLCBlockRequest Requestdata = new WritePLCBlockRequest(buffer);
//Write Buffer into PLC
OperationResult res = Device.WritePLCBlock_MC7(Requestdata);
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
JOptionPane.showMessageDialog(null, "Block " + String.valueOf(Requestdata.getBlockInfo().getHeader().getBlockType()) + String.valueOf(Requestdata.getBlockInfo().getHeader().getBlockNumber()) + "successful saved in PLC! File:" + dr.getSelectedFile().getName(), "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "operation unsuccessful", "", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "operation aborted", "", JOptionPane.ERROR_MESSAGE);
}
}
}
18. Delete block
import PLCCom.*;
import javax.swing.JOptionPane;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnDeleteBlockActionPerformed(java.awt.event.ActionEvent evt) {
//Delete DB100
int BlockNumber = 100;
eBlockType BlockType = eBlockType.DB;
OperationResult res = Device.DeleteBlock(BlockType, BlockNumber);
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
}
Reference : https://www.plccom.net/en/plccom/for-s7/fors7-code-examples/