Project - Master/Slave Modbus (RTU/TCP) Library for NETMF and Windows

Found the problem. You need to make 1 small change to the code to get the library to work with fast slave devices on the same bus.

Change line 188 in ModbusDevices to Thread.Sleep(1) instead of Thread.Sleep(50)

The reason for this is that the sleep for 50ms can cause the receive buffer to start receiving the data from the next message or reply from a slave. With the sleep at 1, the loop is processed much faster and in fact I now find that the reply to the master is much faster too whereas before it was over 64ms, now it’s around 20ms which matches up with the VSD I am using.

You may have to play around with the interframe delays on your master to ensure reliable comms. I am using 50ms which gives me 100% success rate and fast enough for this projects needs.

PS… This only applies if you use the library for Serial RTU devices. For TCP/IP this is not an issue.

1 Like

Thank you Dave,

I will add this fix to my Version too.
And I have to admit that my implementation of the RTU device is a bit lazy.
I think it is hard to implement the correct Timing (3.5 Charakters at higher baud rates are a really shoirt time) in NETMF.
The solution would be to implement the RTU device receive Code in RLP. But I currently do not have the Need to do so, so I i will not (for now :slight_smile:
btw. this Change should also imprrove the Performance of the TCP device a bit :smiley:

1 Like

Hi Reinhard,

Ran into another issue today. I was testing with a Schneider ATV312 VSD and I got it working. The ATV312 replied about 15ms after it received the Modbus request. The timing gaps could be detected by the software

Testing with with the ATV61 which is a much bigger unit it replies within 6ms of receiving the Modbus request. Running the bus at 9600bps.

The modified code running on a G120 is just not fast enough to detect the gaps in the messages when it is being sent to another device. When it does detect a gap I am seeing the request from the PLC to the ATV61 and it’s reply in the buffer. For your code to work, it has to detect the GAP between these 2 messages.

Looks like I need to either do the extraction and timing test as an RLP or rewrite a lot of the code to remove any overhead. I’ve already created a new ReceiveTelagram function that does not worry about the expected length but still not enough to handle the fast response of the VSD.

The 6ms should be more than enough to detect the 1.5ms :frowning:

Hi,

I want to try Tcp functions with modbus master, but I cannot figure out how to configure the socket.

public ModbusTcpInterface(string host, int port = 502, short maxDataLength = 252)

host string is my GHI G400 IP or a single modbus slave device to connect to? IPEndPoint want the slave device IP and port?

If I want to connect a lot of TCP devices does I need to use a external socket using manually .Connect and .Close methods and reinit the interface with the new Device IP address every Modbus request to different device?

Cheers.

host is the IP address of the Modbus TCP device you want to connect to.
The Default Modbus TCP port is 502, but if your device listens to a different port, you can Change this.
If you have multiple modbus TCP devices, you Need multiple instances of the ModbusMaster class, each with ist own ModbusTcpInterface.
ModbusTcp is not really a bus, it’s more like a Point to Point Connection.
So the address Byte in each message is usually set to 248 for TCP devices, which is actually an invalid id.
The modbus id is nut fully useless tho.
A Modbus TCP device can be a Gateway to an Modbus RTU bus.
So the TCP device would Forward all Messages with id’s > 0 && < 248 to the RTU bus, and consume id 248 by itself.
There is also a implementation for the Gateway in the library.