0
votes

I am currently working on an application which communicated with devices using Modbus serial. The application is written in C#.

Our devices have been modified to use Ethernet and I have now been tasked with converting this application to use Modbus TCP/IP.

I initially thought it would be very simple, and would only require changing the Serial connection to a TCP Client connection and Listener.

I have read some documentation on Modbus TCP/IP

https://www.prosoft-technology.com/kb/assets/intro_modbustcp.pdf https://www.honeywellprocess.com/library/support/Public/Documents/51-52-25-121.pdf

Although I feel like I still may be missing some essential differences.

From what I understand, this is what needs to be done.

  1. Introduce new MBAP header in place of the Additional Address section at the start of a packet.
  2. Remove checksum from the Modbus packet as this is handled by TCP/IP.
  3. Open connection with each device using the Socket class. To open these connections I will now need to store each Device IP in my Client/Master configuration. They will be listening on the default Port 502 and the connection must be opened from a random available port > 1024. The connection should also remain open, therefore I will maintain an Array of TcpClient objects. Or should I simply close the connection after I receive a response? Will I need to have a thread for each Server/Slave?
1

1 Answers

1
votes

You are in the right direction. But please do not close the TCP connections after receiving the response. I have developed Modbus TCP slaves before, on very resource limited platforms, and it was not fun to handle TCP connection creation/destruction every 1-2 seconds. Keep the connection open, it will be easier both for you and the slaves.

You don't need a thread for each slave. You can use some form of "select" available in C#.