Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C#

PLC Communication using .NET

Rate me:
Please Sign up or sign in to vote.
4.60/5 (30 votes)
21 Jan 2015CPOL3 min read 356.6K   30.6K   74   88
Read and write data to Melsec PLC using .NET.

Image 1

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. However, 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, 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:

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 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.

Read Data from PLC

PLC Read Command: To read data from PLC, we need to send the command to PLC. Basically, the command we send will be like this: "500000FF03FF000018000A04010000D*0095000001";

C#
String   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 (to read the data from PLC we should "0401"
cmd = cmd + "0000" ;  //  Sub command
cmd = cmd +"D*"  ;    //   device code
cmd = cmd + "009500"; //adBase 
cmd = cmd + "0001";
//Device No ,It’s a Address every PLC device will have an address
//we need to send the appropriate address to read the data.

Write Data to PLC

PLC Write Command: To write data to PLC, we need to send the command to PLC. Basically, the command we send will be like this: "500000FF03FF00001C000A14010000D*0095010002".

C#
String   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 + "0002";   // BASE ADDRESS

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 the Code

I have attached C# sample programs for PLC communication in this article but for explanation here, I have used my component MelsecPLC.dll in the test project. First, add MelsecPLC.dll to your project.

1. Declare MelsecPLC.dll in the Form

C#
//
using MelsecPLC;

2. Variable Declarations

C#
//
public MelsecPLC.Winsock winsock1; // Declare the MelsecPLC Winsock Component
string Jig01;                      // To store the PLC read data.

3. Connect (PLC Connection)

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:

C#
//
private void Form1_Load(object sender, EventArgs e)
{
    winsock1Connect();
    winsock1.DataArrival += 
      new MelsecPLC.Winsock.DataArrivalEventHandler(winsock1_DataArrival);
    timer1.Enabled = true;
    timer1.Start();
}

private void winsock1Connect()
{
    try
    {
        if (winsock1.GetState.ToString() != "Connected")
        {
            winsock1.LocalPort = 1027;
            winsock1.RemoteIP ="10.126.224.221";
            int a = 8000;
            winsock1.RemotePort = 8000;
            winsock1.Connect();
        }
    }
    catch (Exception ex)
    {
    }
}

4. DataArrival Event

In the DataArrival event, we get the actual data sent 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.

C#
//
private void winsock1_DataArrival(MelsecPLC.Winsock sender, int BytesTotal)
{
    String s = String.Empty;
    winsock1.GetData(ref s);
    Jig01 = s;
}

5. Read Data from PLC

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.

C#
//
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);         
}

6. Write Data to PLC

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.

C#
//
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

  • 5th July, 2013: Initial release
  • 21st January, 2015: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

 
GeneralI think AB PLC Don't have IP Pin
Coder Block12-Mar-15 2:24
Coder Block12-Mar-15 2:24 
GeneralRe: I think AB PLC Don't have IP Pin
stixoffire17-Apr-15 4:55
stixoffire17-Apr-15 4:55 
Questioncode from dll ? Pin
Member 1146945221-Feb-15 14:58
Member 1146945221-Feb-15 14:58 
Questionsample for AB PLC Pin
Member 978717210-Feb-15 3:25
Member 978717210-Feb-15 3:25 
AnswerRe: sample for AB PLC Pin
syed shanu10-Feb-15 13:31
professionalsyed shanu10-Feb-15 13:31 
AnswerRe: sample for AB PLC Pin
stixoffire17-Apr-15 4:57
stixoffire17-Apr-15 4:57 
GeneralRe: sample for AB PLC Pin
Member 1242774423-May-16 20:50
Member 1242774423-May-16 20:50 
AnswerRe: sample for AB PLC Pin
stixoffire23-May-16 21:55
stixoffire23-May-16 21:55 
If you have RSLinx then you have OpcNetApi , use that API in your project OPC DA; if you are talking about browsing for tags - it is probably the best way for you to do it. There is also a UA project on Code project that you can find lots of samples how to do it with OPC UA ; I do not know if Linx supports UA though - it might if it is a recent version.
QuestionTimer? Pin
bojammis22-Jan-15 7:53
professionalbojammis22-Jan-15 7:53 
AnswerRe: Timer? Pin
syed shanu22-Jan-15 13:03
professionalsyed shanu22-Jan-15 13:03 
GeneralRe: Timer? Pin
bojammis23-Jan-15 9:18
professionalbojammis23-Jan-15 9:18 
QuestionShowing Exception : Must be idle to change the local port Pin
varadha0034-Jan-15 18:50
varadha0034-Jan-15 18:50 
AnswerRe: Showing Exception : Must be idle to change the local port Pin
syed shanu4-Jan-15 18:59
professionalsyed shanu4-Jan-15 18:59 
QuestionCan get data if plc have a password? Pin
Ar.Gorgin21-Dec-14 21:45
Ar.Gorgin21-Dec-14 21:45 
QuestionQuestions help Pin
Olival19-May-14 7:43
Olival19-May-14 7:43 
AnswerRe: Questions help Pin
syed shanu19-May-14 14:03
professionalsyed shanu19-May-14 14:03 
GeneralRe: Questions help Pin
Poer Edy13-Jul-14 18:37
Poer Edy13-Jul-14 18:37 
GeneralRe: Questions help Pin
syed shanu13-Jul-14 18:41
professionalsyed shanu13-Jul-14 18:41 
GeneralRe: Questions help Pin
Poer Edy13-Jul-14 18:45
Poer Edy13-Jul-14 18:45 
GeneralRe: Questions help Pin
syed shanu13-Jul-14 18:47
professionalsyed shanu13-Jul-14 18:47 
QuestionWhat type of plc ? Pin
TwoNeuronsFaulty3-Apr-14 23:18
TwoNeuronsFaulty3-Apr-14 23:18 
QuestionSource code DLL Pin
Member 1035158726-Nov-13 3:07
Member 1035158726-Nov-13 3:07 
AnswerRe: Source code DLL Pin
syed shanu26-Nov-13 12:30
professionalsyed shanu26-Nov-13 12:30 
GeneralRe: Source code DLL Pin
chy6710011-Jul-14 19:49
chy6710011-Jul-14 19:49 
GeneralRe: Source code DLL Pin
syed shanu1-Jul-14 19:54
professionalsyed shanu1-Jul-14 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.