5. Simple writing data to PLC
using PLCcom.*;
private PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnWrite_Click(object sender, EventArgs e)
{
WriteDataRequest myWriteRequest = new WriteDataRequest(eRegion.DataBlock, // Region
Integer.valueOf(100), // DB / only for data block operations otherwise 0
Integer.valueOf(0), // write start address
Byte.valueOf(txtBit.getText())); // Bit/only for bit operations
//set the request parameters
//write 4 bytes in DB100
WriteRequest[] myWriteRequest = new WriteRequest[1];
myWriteRequest[0] = new WriteRequest();
//add writable Data here
myWriteRequest.addByte(new byte[] { 11, 12, 13, 14 });
//write
WriteDataResult res = Device.writeData(myWriteRequest);
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res[0].Message;
if (res[0].Quality.Equals(OperationResult.eQuality.GOOD))
{
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res[0].Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
6. Optimized reading and writing of data
import PLCCom.*;
public class main {
//declare the Device
private PLCcomDevice Device; //see section 'connect' for declare and connect a PLCcom-Device
private void btnOptReadWriteActionPerformed(java.awt.event.ActionEvent evt) {
ReadWriteRequestSet myRequestSet = new ReadWriteRequestSet();
//set optimize options
myRequestSet.setOperationOrder(eOperationOrder.WRITE_BEVOR_READ);
myRequestSet.setReadOptimizationMode(eReadOptimizationMode.AUTO);
myRequestSet.setWriteOptimizationMode(eWriteOptimizationMode.CROSS_AREAS);
// declare a ReadRequest object set the request parameters,
//in this case => read 10 Bytes from DB1 at Byte 0
ReadDataRequest myReadDataRequest = new ReadDataRequest(eRegion.DataBlock, // Region
1, // DB only for datablock operations otherwise 0
0, // read start adress
eDataType.BYTE, // desired datatype
10); // Quantity of reading values
// add the read request to the request set
myRequestSet.addRequest(myReadDataRequest);
// declare a WriteRequest object set the request parameters,
//in this case => write 4 bytes to DB100 at address 0
WriteDataRequest myWriteRequest = new WriteDataRequest(eRegion.DataBlock, // Region
100, // DB
0); // startaddress
// add writable Data here
// in this case => write 4 bytes in DB100
myWriteRequest.addByte(new byte[] { 11, 12, 13, 14 });
// add the write request to the request set
myRequestSet.addRequest(myWriteRequest);
// ....... add more requests to request set
// read, write and getting the results
ReadWriteResultSet results = Device.readWriteData(myRequestSet);
// evaluate the results of read operations...
for (ReadDataResult res : results.getReadDataResults()) {
// for getting read results see chapter simple read
}
// ...and evaluate the results of write operations
for (WriteDataResult res : results.getWriteDataResults()) {
// for getting write results see chapter simple write
}
}
}
7. Get basic info 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 btngetPLCBasicInfoActionPerformed(java.awt.event.ActionEvent evt) {
//execute function
BasicInfoResult res = Device.GetBasicInfo();
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
StringBuilder sb = new StringBuilder();
sb.append("Device Name: ");
sb.append(res.Name());
sb.append(System.getProperty("line.separator"));
sb.append("Order Number: ");
sb.append(res.Ordernumber());
sb.append(System.getProperty("line.separator"));
sb.append("Module Version: ");
sb.append(res.ModuleVersion());
sb.append(System.getProperty("line.separator"));
sb.append("Firmware Version: ");
sb.append(res.FirmwareVersion());
sb.append(System.getProperty("line.separator"));
JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
}
}
}
8. Get mode and state from CPU
import PLCCom.*;
import javax.swing.JOptionPane;
public class NewClass {
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btngetCPUModeActionPerformed(java.awt.event.ActionEvent evt) {
//execute function
CPUModeInfoResult res = Device.GetCPUMode();
//evaluate results
if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
StringBuilder sb = new StringBuilder();
sb.append("CPU Mode = ");
sb.append(String.valueOf(res.CPUModeInfo()));
sb.append(System.getProperty("line.separator"));
sb.append("CPU State = ");
sb.append(String.valueOf(res.CPUStateInfo()));
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/