I very knew Modbus & its related domain. I need to implement node js application that should read data from Modbus register via TCP connection and convert it into readable formats such as Integer, Float, Time, Char, String. I was using Modbus-serial npm package. I was implemented somehow the conversation part but don't know it's correct or wrong. Please suggest with your feedback.
Assuming each register having 2-byte information & the byte data looks like So Integer, Char - 1 byte Float - 2 byte Time allocated with 3 bytes (Assuming the storage will be in UNIX timestamp, Even please suggest if any other better way for handling )
Register Holding value | Buffer data (bytes) | Converted Value | DataType |
---|---|---|---|
97 | Buffer 00 61 | 97 | Integer |
100 | Buffer 00 64 | d | Char |
[565,598] | Buffer 02 35 02 56 | 1.32984502248624 | Float |
[1,1,1] | Buffer 00 01 00 01 00 01 | 2106-02-08 00:40:33 3333 | Time |
Modbus does not define how the data types are sent, it only defines the sending of one or more 16-bit words and also bits.
As these registers are interpreted as signed or unsigned integers, or as two consecutive registers are interpreted as a float and even less as 3 registers are interpreted as time, then each implementation of the protocol does as it thinks best.
For this reason, when a float is sent, sometimes the most significant word is sent first and sometimes it is the opposite.The Modbus client should be prepared to invert the words in this case.
And in the case of transmitting a time, it can be even more complicated.
Agree !. Modbus doesn't know the data type. As result, we have defined the datatypes & conversion. Question is looking forward more into the conversion method results correct or wrong.
@Narayanan because the standard does not define data types other than 16-bit words and bits your question is unanswerable (there is no 'correct' method). There are multiple ways of implementing Char, Float and Time; the correct way will be whatever way the manufacturer of the device you are communicating with chose! (if communicating with multiple devices you may well need multiple encoding/decoding methods). I find this calculator useful when trying to work out what encoding has been used.
@Brits, You clarified very clearly,, Thanks !!