PLC Communication Using .NET - CodeProject PDF
PLC Communication Using .NET - CodeProject PDF
Introduction
In this article I will explain how to communicate with PLC (Programmable Logic Controller). I started my PLC communication program using .NET
from 2007.
For a basic understanding of what PLC is, use Google because basically I am not a PLC Engineer or Electrical Engineer, but I will explain to you how
to connect PLC using .NET programs, how to read data from PLC, and how to write data to PLC. My sample programs are all based on MELSEC PLC
using TCP/IP communication.
There are different kinds of PLC available like MELSEC ,SIMENS, and etc. For each PLC communication there are different types of protocols
available. In this demo program I have used TCP IP communication for MELSEC PLC.
Here I have mentioned .NET programs instead of a language in .NET. In my article you will find four Zip files:
1. SPLCSharpConnect.zip contains the PLC component DLL source code. I started PLC communication programming in 2007. At that time I
was coding using VB.NET. I created my own Winsock component using VB.NET. Even now for PLC communication I am using the same
Winsock component MelsecPLC.dll in my projects. In that component I have created simple functions as Connect, Disconnect,
Read, Write, and a DataArrival event. In the sample project I have attached I have used the same DLL.
Before we start PLC communication programming, I'd like to explain the read and write processes in PLC communication.
You can see the difference, to read we use "0401" and "009500" but for write we use "1401" and "009501". The detailed code will be listed below.
//
using MelsecPLC;
2. Variable declarations
For connection we need the PLC IP address. Here my PLC IP address is “10.126.224.221”. Enter the local port and remote port. Here I have used
“1027” for the local port and “8000” for the remote port. In form load, we call the PLC Connect function and we create a MelsecPLC
DataArrival event. The DataArrival event will be triggered when PLC sends data to the PC (our computer).
The detailed connect functions and DataArrival declaration in the form load are listed below.
//
private void Form1_Load(object sender, EventArgs e)
{
winsock1Connect();
winsock1.DataArrival +=
new MelsecPLC.Winsock.DataArrivalEventHandler(winsock1_DataArrival);
timer1.Enabled = true;
timer1.Start();
}
4. DataArrival Event
In the DataArrival event we get the actual data send by PLC after the Read signal is sent to PLC. This event will be triggered whenever PLC
sends data to the PC. To get data from PLC we use the winsock1.GetData() method.
//
private void winsock1_DataArrival(MelsecPLC.Winsock sender, int BytesTotal)
{
String s = String.Empty;
winsock1.GetData(ref s);
Jig01 = s;
}
To read data from PLC we need to send the signal to PLC for data read. I have explained the read functions in my article introduction. To send read
signal to PLC, we use the winsock1.Send() method.
//
private void btnRead_Click(object sender, EventArgs e)
{
if (winsock1.GetState.ToString() != "Connected")
{
winsock1Connect();
}
//String cmd = "500000FF03FF000018000A04010000D*0095000001";
String cmd = "";
String OutAddress = "0001";
cmd = "";
cmd = cmd + "5000" ;// sub HEAD (NOT)
cmd = cmd + "00" ;// network number (NOT)
cmd = cmd + "FF" ;//PLC NUMBER
cmd = cmd + "03FF" ;// DEMAND OBJECT MUDULE I/O NUMBER
cmd = cmd + "00" ;// DEMAND OBJECT MUDULE DEVICE NUMBER
cmd = cmd + "001C" ;// Length of demand data
cmd = cmd + "000A";// CPU inspector data
cmd = cmd + "0401";// Read command
cmd = cmd + "0000" ;// Sub command
cmd = cmd +"D*" ;// device code
cmd = cmd + "009500"; //adBase
cmd = cmd + OutAddress; //BASE ADDRESS
winsock1.Send(cmd);
}
To write data to PLC we need to send the signal to PLC for data write. I have explained the write functions in my article introduction. To send a
write signal to PLC here we use the winsock1.Send() method.
//
private void btnWrite_Click(object sender, EventArgs e)
{
if (winsock1.GetState.ToString() != "Connected")
{
winsock1Connect();
}
String cmd = "";
String OutAddress = txtWrite.Text.Trim();
cmd = "";
cmd = cmd + "5000";// sub HEAD (NOT)
cmd = cmd + "00";// network number (NOT)
cmd = cmd + "FF";//PLC NUMBER
cmd = cmd + "03FF";// DEMAND OBJECT MUDULE I/O NUMBER
cmd = cmd + "00";// DEMAND OBJECT MUDULE DEVICE NUMBER
cmd = cmd + "001C";// Length of demand data
cmd = cmd + "000A";// CPU inspector data
cmd = cmd + "1401";// Write command
cmd = cmd + "0000";// Sub command
cmd = cmd + "D*";// device code
cmd = cmd + "009501"; //adBase
cmd = cmd + OutAddress; //BASE ADDRESS
winsock1.Send(cmd);
}
Points of Interest
I love to work and play with HMI (Human Interface Program). I have worked with several HMI programs using C#, like PLC, Sensor Programming,
and NutRunner tool communication program. I want readers to benefit from this program.
History
Initial release: 2013/07/05.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
syed shanu
Team Leader
India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog
Search Comments
Hello i just have a question use a random read. Zerglrisk 2-Dec-18 19:14
(MyHealingLife)
Cmd = Chr(02) : Cmd = Cmd + "0103C02" : Cmd = Cmd + Chr(03) : Member 14019021 14-Oct-18 1:38
Cmd = Cmd + "6C"
How to read the data from Panasonic PLC FP-X series using Visual Member 13353163 9-Aug-17 0:24
Studio in C#?
PLC Communication using VB 6.0 Member 13314243 17-Jul-17 10:57
is there need of instoling mecsec.dll file to run this code Member 12797883 6-Mar-17 23:05
Out address mean what sir can u plz Member 12582008 23-Jun-16 21:09
Re: Out address mean what sir can u plz smmoon 6-Jul-16 22:09
Re: Out address mean what sir can u plz Member 12718116 5-Sep-16 4:21
Re: Out address mean what sir can u plz smmoon 1-Dec-16 17:35
Can u Plz Help to read and write from plc Member 12582008 23-Jun-16 21:00
Re: Can u Plz Help to read and write from plc Garth J Lancaster 23-Jun-16 21:08
Re: Can u Plz Help to read and write from plc Member 12582008 23-Jun-16 21:17
Last Visit: 11-Feb-20 9:06 Last Update: 11-Feb-20 9:06 Refresh 1 2 3 4 Next »
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.