5. Simple writing data to PLC
using System;
using PLCcom;
using System.Windows.Forms;
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnWrite_Click(object sender, EventArgs e)
{
//declare a WriteDataRequest object and
//write 4 bytes in DB100 at Startbyte 0
WriteDataRequest myWriteRequest = new WriteDataRequest(eRegion.DataBlock, 100, 0);
//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.Message;
if (res.Quality.Equals(OperationResult.eQuality.GOOD))
{
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
6. Optimized reading and writing of data
using System;
using PLCcom;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnOptReadWriteClick(object sender, EventArgs e)
{
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...
foreach (ReadDataResult res in results.GetReadDataResults())
{
//for getting read results see chapter simple read
}
//...and evaluate the results of write operations
foreach (WriteDataResult res in results.GetWriteDataResults())
{
//for getting write results see chapter simple write
}
}
}
}
7. Get basic info from PLC
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void getGetPLCBasicInfo_Click(object sender, EventArgs e)
{
//execute function
BasicInfoResult res = Device.GetBasicInfo();
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
if (res.Quality == OperationResult.eQuality.GOOD)
{
StringBuilder sb = new StringBuilder();
sb.Append("Device Name: ");
sb.Append(res.Name);
sb.Append(Environment.NewLine);
sb.Append("Order Number: ");
sb.Append(res.OrderNumber);
sb.Append(Environment.NewLine);
sb.Append("Module Version: ");
sb.Append(res.ModuleVersion);
sb.Append(Environment.NewLine);
sb.Append("Firmware Version: ");
sb.Append(res.FirmwareVersion);
sb.Append(Environment.NewLine);
txtResult.Text = sb.ToString();
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
8. Get mode and state from CPU
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnGetCPUMode_Click(object sender, EventArgs e)
{
//execute function
CPUModeInfoResult res = Device.GetCPUMode();
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
if (res.Quality == OperationResult.eQuality.GOOD)
{
txtResult.Text = "CPU Mode = " + res.CPUModeInfo.ToString() + Environment.NewLine + "CPU State = " + res.CPUStateInfo.ToString();
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
Reference : https://www.plccom.net/en/plccom/for-s7/fors7-code-examples/