I am using the POCO C++ library for sockets and TCP connection.
When I open and close the connection for each request, all is normal :
Poco::Net::SocketAddress sa("192.168.2.55", 502);
Poco::Net::StreamSocket socket;
socket.connect(sa);
socket.sendBytes(/*set coil 5 to 1*/, 12);
socket.close();
socket.connect(sa);
socket.sendBytes(/*set coil 6 to 1*/, 12);
socket.close();
socket.connect(sa);
socket.sendBytes(/*set coil 7 to 1*/, 12);
socket.close();
Coils 5, 6, and 7 are set to 1 normally.
But when I send the 3 requests over the same TCP connection, only the first is taken :
Poco::Net::SocketAddress sa("192.168.2.55", 502);
Poco::Net::StreamSocket socket;
socket.connect(sa);
socket.sendBytes(/*set coil 5 to 1*/, 12);
socket.sendBytes(/*set coil 6 to 1*/, 12);
socket.sendBytes(/*set coil 7 to 1*/, 12);
socket.close();
Only the coil number 5 is set to 1.
Which one is better? And what can I do in order to make the second one work?
As per the comments its worth processing the device response before sending a further request. The spec states:
The number of requests accepted by a server depends on its capacity in term of number of resources and size of the TCP windows. In the same way the number of transactions initialized simultaneously by a client depends also on its resource capacity. This implementation parameter is called "NumberMaxOfClientTransaction" and must be described as one of the MODBUS client features. Depending of the device type this parameter can take a value from 1 to 16.
So some devices may only not support multiple simultaneous requests.
Regardless of the above it is worth processing the response because the device may be responding with an error.
Note: Whilst you can implement the Modbus protocol yourself (its a fairly simple protocol) you may find one of the many available libraries of benefit (e.g. macchina.io,EasyModbusTCP.NET).