node-modbus-serial
node-modbus-serial copied to clipboard
Multi ADUs in single TCP message are not allowed from Modbus TCP specifcation
Introduction
We are using the modbus-serial library with NodeJS version 16 on an edge gateway to read and write some data to some Modbus clients. This works good except from the point that we are getting muliple ADUs in a single TCP packet.
What are we doing
If we are doing something like this for three or more addresses (every write to a different address generate a new ADU):
let client = new ModbusTCP()
// open connection to a tcp line
await client.connectTCP(MODBUS_SERVER, {port:MODBUS_PORT})
// set ID
client.setID(1)
let tasks = []
adresses.forEach(async (item, index) =>{
client.writeRegisters(item, data)
})
let responses = await Promise.all(tasks)
Error description
In the Modbus specification Modbus TCP Spec chapter4.2.1.1 is described that this is not allowed.

On wireshark it looks than like this:

Unlucky some ModbusTCP devices from us follow the Modbus TCP specification and closing the connection if they are getting more than one ADU in a TCP packet.
Workaround
At the moment we are resolving this issue by using the following code instead of running forEach and wait afterwards that all read or writes are finished, we are waiting after each action. Is there an possibility how to get this to run without of such a workaround?
If someone can point us to the correct direction, we will try to solve it and make a pull request.
for(let i = 0; i < numberOfActions; i++)
{
await client.writeRegisters(adresses[i], data)
}