YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Franšais
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

PLC Programming, Read data from PLC, Write Data to PLC, PLC Communication Source Code.


Author: Syed Shanu
              India

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 basic understanding of what is PLC use Google, because basically iam not a PLC Engineer or Electrical Engineer, but I will explain to you about 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 Kind of PLC available like MELSEC ,SIMENS and etc,For each PLC Communication there are different type of protocols available. Today 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. This has meaning because today in my article you will find 4 Zip files:

  1. ShanuMelsecPLCComponent.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 for PLC communication. In that component I have created simple functions as Connect,Disconnect, Read, Write, and a DataArrival event. In my sample project I have attached I have used the same DLL.
  2. ShanuPLCC#.NetSample.ZIP contains a C# sample program that demonstrates how to connect, read, and write to PLC using MelsecPLC.dll.
  3. ShanuPLCVB.NetSample.ZIP contains a VB.NET sample program that demonstrates how to  connect, read, and write to PLC using MelsecPLC.dll.
  4. ShanuPLCWPF.Net.ZIP contains a WPF sample program that demonstrates how to connect, read, and write to PLC using MelsecPLC.dll.

Before we are start PLC communication programming, I like to explain to you 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";

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

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.

2

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

1. Delcare MelsecPLC.dll in form

//
using MelsecPLC;

2. Variable Declarations

//
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 Remote port. In the form load, we call the PLCConnect function and we create a MelsecPLC DataArrival event. The DataArrival event will be triggered when PLC sends data to PC (our computer).

The detailed connect functions and DataArrival declaration in the form load is listed below.

//
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 send by the PLC after Read Signal is sent to PLC. This event will trigger 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;
}

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 here 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);         
}

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.

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

3

I love to work and play with the HMI (Human Interface Program). I have worked with several HMI programs using C#,  like PLC, Sensor Programming, and Nutrunner tool communication program. I want the end users who read this article to benefit from this program.

News:

1 UCanCode Advance E-XD++ CAD Drawing and Printing Solution Source Code Solution for C/C++, .NET V2017 is released!

2 UCanCode Advance E-XD++ HMI & SCADA Source Code Solution for C/C++, .NET V2017 is released!

3 UCanCode Advance E-XD++ GIS SVG Drawing and Printing Solution Source Code Solution for C/C++, .NET V2017 is released!

PLC_Programming_VB.zip

Shanu_PLC_Csharp.Net_Sample.zip

Ask any questions by MSN: ucancode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2011 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net