Warm tip: This article is reproduced from serverfault.com, please click

Modus TCP byte to standard data types

发布于 2021-05-06 14:58:03

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
Questioner
Narayanan
Viewed
0
from56 2021-05-06 23:56:49

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.