- Analog input card
- Digital input card
1. Connect to PLC
using PLCcom;
public class myTestClass
{
//declare the Device
private PLCcomDevice Device;
private void btnConnect_Click(object sender, System.EventArgs e)
{
//create TCP_ISO_Device instance from PLCcomDevice
Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
//or create MPI_Device instance from PLCcomDevice
//Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel);
//or create PPI_Device instance from PLCcomDevice
//Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
authentication.User = ""; //Enter User here
authentication.Serial = ""; //Enter Serial here
Device.Connecttimeout = 1000;
Device.Readtimeout = 2000;
ConnectResult res = Device.Connect();
MessageBox.Show(res.Message);
}
}
2. Disconnect from PLC
Untuk memutuskan koneksi ke PLC cukup 1 baris perintah : Device.DisConnect();
3. Automatic connect to PLC
using PLCcom;
public class myTestClass
{
//declare the Device
private PLCcomDevice Device;
private void btnConnect_Click(object sender, System.EventArgs e)
{
//create TCP_ISO_Device instance from PLCcomDevice
Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
//or create MPI_Device instance from PLCcomDevice
//Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel);
//or create PPI_Device instance from PLCcomDevice
//Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
authentication.User = "";
authentication.Serial = "";
//Set auto connect
//The connection is opened automatically when it is needed. If after the expiry of the given period, no more requests are sent, the connection is automatically closed.
Device.setAutoConnect(true, 10000);
}
}
4. Simple reading data from PLC
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
private void btnRead_Click(object sender, EventArgs e)
{
//declare a ReadRequest object
//set the request parameters
//read 10 Bytes from DB100
ReadDataRequest myReadRequest = new ReadDataRequest(eRegion.DataBlock, 100, 0, eDataType.BYTE, 10);
//read from device
ReadDataResult res = Device.ReadData(myReadRequest);
//evaluate results
txtMessage.Text = res.Message;
if (res.Quality == OperationResult.eQuality.GOOD)
{
foreach (byte b in (byte[])res.GetValues())
{
txtResult.Text += b.ToString() + Environment.NewLine;
}
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/