Templates by BIGtheme NET
Home / PLC / Allen Bradley / PLC-PC communication with C#: a quick resume about data exchange libraries

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:

  • commercial SCADAs are too expensive for your application
  • 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)

Now I’ll try to explain how and when to use these libraries, but more detailed articles on how to implement the code will come later.

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 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 supports 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 uses the same methods for both protocols.
Modbus (especially TCP) is a good way to communicate with PLCs with C# and it 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, take a look at my 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 supports both Modbus TCP and RTU.
If you need details about Modbus protocol you can check this site. It has a good video and an exhaustive explanation.

OPC: OLE for Process Control

OPC is the simplest, safest 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 use.
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 in this article. The best way 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, which 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 have been developed with reverse engineering of the protocols.

Siemens
The most common for Siemens is LibNoDave, a free library that is used the most with Ethernet cards (CP343-1). It’s a very common library to communicate with Siemens PLCs and it’s well tested because of being widely used. It supports also other interfaces (I think it supports MPI, Profibus and many Siemens PC-Cards).
There is also a C# driver, S7.Net, that it’s really easy to get it working and it’s on NuGet; you can find an article with examples here: http://www.mesta-automation.com/siemens-s7-plc-c-s7-net-plc-driver/
A brand new library with many useful features is Snap7. It supports many languages (C++, C#, Pascal, LabView, etc). It’s quite easy to setup and has a great documentation.
Another library for Siemens PLC is DotNetSiemensPLCToolBoxLibrary, that is a layer above LibNoDave to simplify the functions and hide all the interop pain. The sources are full of examples.

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 project that deserves special mention is an “almost open-source” project AdvancedHMI. It contains free drivers written in Visual Basic .Net. The drivers are not open-source, but they are free and the interface to them is open-source. To use these 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 considerations about those drivers: they work well but you should consider that in case of problems you are alone with an “unkown dll” and the “quick support” is granted only if you pay.
I’ve 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 drivers? 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 becomes evident with the growth of the complexity because of integration of plcs of different manifacturers. When this happens, OPC is a must.

Proprietary Libraries

These libraries are custom dlls or ActiveX provided by the manifacturer or 3rd party companies. They are generally not free and they remind me of legacy applications, where every plc had its own library with its own code. Here 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

32 comments

  1. Thanks, good collection of information.

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

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

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

    • 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. it is possible to develop a software in visual c# and mange the plc (allen bradley) from it?

    • 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. Great site, i can learn a lot, and english is not my first language too. XD

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

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

  9. 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. 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. Mesta thank you for the article. You saved me a lot of searching and answered a lot of questions I had.

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

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

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

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

  14. Congrats for the good blog.
    I want to create my own commercial scada based on .net that should support Allen Bradley PLCs ?
    What driver would you suggest me to use ? There are third party .net drivers but they are not royalty free. Also I don’t think that the drivers in AdvancedHMI are appropriate for developing another application. They are tightly connected with the WinForms platform.
    What do you think about using open source drivers written in C ?

    Thanks

  15. Thanks a lot Mesta.You have cleared many vogue points in my mind. This site is like a treasure!!

  16. hi, do you know if it’s possible to communicate a cp 343-1 card from siemens with a micrologix 1400?

  17. Anyone know of any mitsubishi ethernet drivers? Hopefully in c++?

    • You are probably better by using an OPC client or buy a Modbus ethernet card, if Modbus isn’t supported natively. If you want to go with Melsec protocol, here is a library in c# with a sample client. Probably you can translate in in C++ or wrap it in managed C++. I never used that library anyway.

  18. Michael Dalgaard Andersen

    Hello Mesta

    Nice work about the communication library’s.
    I would like to make a data logger tool for Siemens in C# and I wonder what communication library I should use.

    I found 4 possible choices
    -LibNoDave
    -DotNetSiemensPLCToolBoxLibrary
    -S7 Net Plus
    -Snap7

    How do these library’s compare ?
    Are they all build out of the same communication philosophi ?
    What about communication speeds?

    What library would you suggest ?

    /michael

    • Hello, all the libraries works and the choice should be based on your experience in using C#, C++, interoperability and knowledge of the S7 protocol.
      S7 Net is simple and full C#, while the other libraries are in C++, so if you have to debug the messages from the plc to pc, it’s usually easier with S7.Net. It only support Profinet, so you need an ethernet port on your plc to use it.
      Snap7 is a great library, complete and that offer a lot of features, not only client-server connectivity. It’s working for many different platforms, not only C#,and the author gives you the C# wrapper and the dll to include in your project.
      LibNoDave is the basic C library, it provides all the functions to communicate with S7 plc, nothing more. But it probably support all protocols, not only Profinet. A c# wrapper is provided I think.
      DotNetSiemensPLCToolBoxLibrary is an extended c# wrapper of LibNoDave, it has a lot of features and you can do a lot of things with it.
      If you are not familiar with S7 protocol and with C/C++ in general, you can start with S7 Net, and then maybe move to another library when you learned or you need more advanced features.
      Anyway because the protocol is the same, there is no problem usually to switch from one library to another.

      • ok thanks mesta….
        I think I’ll go with the snap7 library, it looks to be the best documented and as you say it has all the features.

        I still wonder about the performance of the different librarys.
        Do you have any experience performance wise??

        /michael

        • These libraries are low level libraries, so there isn’t much to say about performance. They are as good as Windows sockets are.

  19. Very good collection of information.

Leave a Reply