Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to comunicate to a PLC via ModbusRTU and control some engines based on the data collected from some sensors, but I have no idea how to do it using C#. I have done some C# aplications before( so I know the basics), but nothing like that, so I'm new to that kind of programming.

Any idea how could I do it?

Thanks in advance

As Collin has said, use the serial port to communicate with the MODBUS device. You'll probably need an adapter, though, as the usual implementation is RS-485, not the RS-232 port provided on a PC. As you'll discover when you Google around, MODBUS uses some weird data types, especially bit data, which are mapped to specific memory addresses in the RTU. The RTU/PLC processor reads and writes to these memory addresses to obtain configuration settings, receive commands, and record data. Your application should be able to write to these memory bits and to select specified slices of data from the RTU memory.

Depending on the device, it might be able to generate alarms, in which case you'll want to monitor the port closely. I'd stick that function in a separate thread that can interrupt the rest of the application when an alarm comes in. Otherwise, in the RTU slave mode, the device will simply execute its PLC program until you poll it.

This is something I spent a lot of time looking into a few months back, as my company has 57 sewer lift stations which are controlled by Allen Bradley PLCs using a MODBUS protocol for control. My goal is to one day link them via radio, and write a SCADA system to monitor and control them from the office. I'll look forward to the article you'll surely be writing for us when it's done, and plan to learn from it how this is done properly.:-)
Share this answer
Just curious why you want to do this?

Why not control the engines using the PLC language? By using a C# app you are risking that the engine ends up in a bad state. PCs like to shut down and drop communication as you may know. This could do something such as leave the engine running dry or even worse ramp it up.

I have done similar things in the past and it is really best to have the PLC have base logic, for state control. Then if you are not liking the UI or want to simply work from a standard PC (not a Windows CE or something) you communicate via TCP/IP (if possible).

If you still need to I am guessing you will need to use a serial communication port to connect to the Modbus. You will need to look at the protocols of how to do this.

This[^] should get you started. Again, I would recomend not doing it though for state control reasons. PLCs are more robust and should have full control over the state. External apps should merely 'request' state change.
Share this answer
Mehdi Gholam 20-Oct-11 10:28am    
my 5!
[no name] 20-Oct-11 11:13am    
Thank you!

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900