PLC-PC communication with C#: a quick resume about data exchange libraries

Share Button

Let’s say that you want to build your own HMI, because:

  • for your application a commercial SCADAs are too expensive
  • you need more control over the code and VBA is not good
  • you want to protect your know how
  • you need a better HMI and you can’t do it with a normal SCADA

and you choose to use .Net and C# or Visual Basic .Net.
Now the first and most important thing that you need to do is to Read and Write Data to your PLC, and to do this you have 4 choices:

  1. using Modbus TCP (or RTU)
  2. using OPC
  3. using open source drivers (gpl or lgpl licensed)
  4. using third-party libraries (activex, dlls and so on)

I will try to explain how and when to use this libraries, but more detailed articles will follow on how to implement the code.

Modbus TCP and RTU

Modbus TCP is based on Ethernet TCP/IP connection from PLC to PC, there is no need for a custom card on PC but the most of the times you have to buy an additional Ethernet Card for the PLC that support Modbus. In the last 2 years many plcs are provided with ethernet integrated port that support Modbus, let’s say for example Allen Bradley Micrologix 1400(ser. B) or Siemens S7 1200.
To communicate to a PLC with Modbus TCP you need a library called nModbus: download it on the google code site.
This library has been tested a lot and it’s almost a standard for Modbus TCP and RTU, because it use the same methods for both protocols.
Modbus (especially TCP) is a good choice to communicate with PLCs with C# and gives you the most of flexibility, because you build your program and you can run it wherever you want without licenses to pay and so on.
If you need some examples, i wrote an article on how to use NModbus with C#.
All manifacturers support Modbus, in particular:
Siemens s7-300 and s7-400 support both Modbus TCP and RTU: read more about it in the official documentation. For Modbus TCP you need of course the Ethernet card or CPU Profinet version.
Allen Bradley CompactLogix and Controllogix do not support Modbus TCP and Modbus RTU, Micrologix 1400 ser. B support both Modbus TCP and RTU.
If you need details about Modbus protocol you can check this site, that has a good video and an exaustive explanation.

OPC: OLE for Process Control

OPC is the simplest, safer and easiest way to communicate with a PLC.
An Opc Server is basically a program that connects to the plc using closed source libraries, and exposes the data in a standard way that is not dependant from the PLC that you used.
To use the OPC with C# you need to write your own client using the libraries given by your OPC server vendor, by OPC Foundation or you can use third-party clients. Usually to write a client you just have to create a group and items that you have to read, and wait for the callback that notifies you when the data that you subscribed are changed. OPC permits also many advanced features, like “browse plcs online, browse all tags in a treeview, alarms and events management” and so on… I think that what you need anyway is simply reading and writing tags with different priority. You can find a simple c# client with a video on this article. The best choice to communicate with C# and OPC is to use the OpcNetApi.dll and OpcNetApi.Com.dll; you can download those 2 libraries from OPC Foundation site or from some open source projects around the web, just run a search on google. You are free to redistribute dlls to your customers but you are strongly reccomended to get the documentation from OPC Foundation website.
So what’s bad about OPC ?
1) it’s not free, this means that you have to purchase a copy of the OPC Server for every pc where you will run your program.
2) it runs only on windows because is based on COM and DCOM… but no one cares because 99% of industrial panels are built with windows XP embedded or Windows CE.

OPEN SOURCE DRIVERS:

There are some drivers, for the most common PLC manifacturers, that has been developed with reverse engineering of the protocols.

Siemens
The most common for Siemens is LibNoDave, a free library that is used for the most with Ethernet cards (CP343-1). It’s a very common library to communicate with Siemens PLCs and well tested, because has been used widely. It supports also other interfaces (i think it support MPI, Profibus and many Siemens PC-Cards).
A brand new library with many useful features is Snap7, that supports many languages (C++, C#, Pascal, LabView, etc) and it’s quite easy to setup and has a great documentation.
Other libraries for Siemens PLCs are S7.Net, based on Ethernet communication too, and a library based on LibNoDave called DotNetSiemensPLCToolBoxLibrary, that is a layer above LibNoDave to simplify the functions.

Allen Bradley
For Allen Bradley you should check 2 libraries written in VB.Net for Allen Bradley Plcs:
AB DF1 Protocol and Allen Bradley Slc 5/05 Ethernet Library.
Some other libraries written in C for Windows are the ones of Pvbrowser, famous is TuxEIP (ethernet IP stack for controllogix and micrologix) and TUX DF1. You can find some information on pvbrowser site or on the tux plc webarchive site; Leicht posted those libraries also on github. They are native for Linux but you can compile in windows using CygWin but please don’t ask how to.

A special mention is deserved for an “almost open-source” project,AdvancedHMI that contains free drivers. Those drivers are written in Visual Basic .Net and they are not opensource, but they are free and the interface to them is open-source. To use this libraries you need to import them in your C# project (not in Visual Studio Express edition, you need at least Professional edition) or build the dlls and add them as reference in your C# project.
Advanced HMI anyway offers drivers for: Allen Bradley DF1, Ethernet/IP (both Micrologix and CompactLogix) and Beckoff Twincat. AdvancedHMI offers also good looking objects if you are using Winforms, and you can import them same as you imported the drivers. It’s really a good project and it’s easy to get results. You should check it!
There are some consideration about those drivers: they works good but you should consider that if you meet problems you are alone with an “unkown dll” and the “quick support” is granted only if you pay.
I already used those drivers with success, before switching to Modbus, and i wrote an article about AdvancedHMI that you can read.

What’s good about Open Source driver? They are free and you can debug and improve the communication as much as you need for your plant. You can also write a standard interface (like the one provided by AdvancedHMI) to access to all PLC data in the same way.
The bad side of using this drivers happens when the complexity grows because of integration of plcs of different manifacturers. When this happens, OPC is a must.

Proprietary Libraries

This libraries are custom dlls or ActiveX provided by the manifacturer or 3rd party companies. They are generally not free and they reminds me legacy applications where every plc had his own library with his own code. This is a video that explains why OPC has been made and why you should really consider to use OPC instead of third-party libraries:

Share Button

, , , ,

20 Responses to PLC-PC communication with C#: a quick resume about data exchange libraries

  1. Frank April 16, 2012 at 19:53 #

    Thanks, good collection of information.

  2. John October 5, 2012 at 13:12 #

    Good info, but I guess English isn’t your first language…

  3. mesta October 5, 2012 at 18:15 #

    Unfortunately it’s not my first language and i’d really appreciate if you correct my mistakes.

  4. Wes October 19, 2012 at 09:08 #

    How do you phisically connect the pc and plc? ethernet?

    • mesta October 19, 2012 at 12:32 #

      Depends on your equipment and on project’s requirements. If it’s possible i prefer with ethernet, else i use what the plc offer, like CAN, RS-485, RS-232, Profibus, MPI, etc…

  5. jenny n November 27, 2012 at 00:50 #

    it is possible to develop a software in visual c# and mange the plc (allen bradley) from it?

    • mesta November 27, 2012 at 13:34 #

      If by “manage plc” you mean read and write values from plc, yes you can. Check advancedHmi to get started with a simple project and drivers.

  6. Daniel November 28, 2012 at 15:19 #

    Great site, i can learn a lot, and english is not my first language too. XD

  7. David March 13, 2013 at 02:18 #

    Personally i think this is awesome, really nice work mesta!

  8. Nirdesh April 2, 2013 at 08:01 #

    Thanks a lot. This is wonderful article. It got me started.

  9. Nate Pirtz August 18, 2013 at 17:10 #

    I have used drivers from ingear.com (Ethernet) which saved me a bit of time. The hardest part was designing a system using wpf, c# and the mvvm pattern with Microsoft prism pattern, because the background thread that was getting the tag values has to update the view thread, so if you have multiple nested usercontrol’s then you need to update the properties in those. I build a solution to this that works well, if anyone is interested, I can post examples somewhere.

  10. vanbang1707 November 19, 2013 at 04:01 #

    i have read some example in http:http://www.mesta-automation.com/
    but i don’t How do communicate over the ethernet from one CompactLogix to pc used C#. you can hepl me. thank you very much.

  11. Martin January 12, 2014 at 12:42 #

    Mesta thank you for the article. You saved me a lot of searching and answered a lot of questions I had.

  12. thinesh April 27, 2014 at 16:13 #

    how to communicate simens plc to allen bradly plc(micro logix 1400) via modbus communication if any possiblities please tell me

    • mesta April 28, 2014 at 12:28 #

      Depending on the models of the Siemens PLC, you may need hardware, then read the manuals on how to use the Modbus TCP or RTU library on both Siemens and AB, then setup the communication to read/write the registries that you need.

  13. SultanGeek August 9, 2014 at 20:07 #

    hello Mesta first good job!

    I want to create a web based scada . In scada i want put gauge,curve,thermometer.I use snap7 it’s an open source library for communicate with Siemens PLC.
    How can i do for create this web based supervision(langage?,technology?).I would use dotnet plateform.Do you know dotnet control for web.

    Thanks

    • mesta August 12, 2014 at 10:08 #

      I have no experience with web based scada. I know that with “Asp.Net webforms” was not so difficult to have some basic results, but it’s a dead technology. My experience is only in desktop applications. Thanks for pointing out to Snap7 library, didn’t know about its existance.

      • SultanGeek August 12, 2014 at 12:22 #

        Ok thanks.Snap7 is very simple to use and fully documented.Actually I want to test S7.NET, however I found some bug in this library.For example I cannot read a struct,this function don’t work.I fix the but concerning reading Boolean variable but for struct I cannot fix it.

Trackbacks/Pingbacks

  1. PLC-PC communication with C#: a quick resume about data exchange libraries « Blog Vi?t - November 29, 2012

    [...] using OPC [...]

Leave a Reply