Our application reads data from the modbus device in every 50 milliseconds by using j2mod library.
There are two bugs while processing transaction id in tcp transport.
First bug (and possible fix):
After 3-4 hours, the transaction id validity check in ModbusTcpTransaction fails. The investigation of the code (based on r92) reveals that the transaction id equality fails after reaching the signed short limit.
In ModbusTcpTransport, decoding of transaction id is done with "registerToShort" method. This operation causes the unsigned 32768 to be interpreted as -32768 resulting failure in transaction id comparison.
ModbusUtil class is lacking registerToUnsignedShort method with the additional index parameter. We added such method to be used it in ModbusTcpTransport replacing registerToShort. In addition, Modbus constant MAX_TRANSACTION_ID is 65534 (stated as Short.Max * 2). The constant value should be 65535 which is unsigned short upper limit.
Second bug (and possible fix):
Tcp requests are done in ModbusTcpTransaction class and static transaction id variable is incremented by 1 after each request which is OK. However, the very first requests of each message type are always sent with the transaction id 0 (zero). This is solved by moving incrementTransactionId invocation before the big while loop (retry loop).
Last edit: bberker 2014-12-11
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Our application reads data from the modbus device in every 50 milliseconds by using j2mod library.
There are two bugs while processing transaction id in tcp transport.
First bug (and possible fix):
After 3-4 hours, the transaction id validity check in ModbusTcpTransaction fails. The investigation of the code (based on r92) reveals that the transaction id equality fails after reaching the signed short limit.
In ModbusTcpTransport, decoding of transaction id is done with "registerToShort" method. This operation causes the unsigned 32768 to be interpreted as -32768 resulting failure in transaction id comparison.
ModbusUtil class is lacking registerToUnsignedShort method with the additional index parameter. We added such method to be used it in ModbusTcpTransport replacing registerToShort. In addition, Modbus constant MAX_TRANSACTION_ID is 65534 (stated as Short.Max * 2). The constant value should be 65535 which is unsigned short upper limit.
Second bug (and possible fix):
Tcp requests are done in ModbusTcpTransaction class and static transaction id variable is incremented by 1 after each request which is OK. However, the very first requests of each message type are always sent with the transaction id 0 (zero). This is solved by moving incrementTransactionId invocation before the big while loop (retry loop).
Last edit: bberker 2014-12-11