When data types use more than 1 register per value, the register order within the data value is
important. Some devices will swap the high and low bytes between registers. You can
compensate for this by selecting the appropriate
ModbusOption
.
Byte order is also important when communicating data over Modbus. Big Endian byte order is
the reverse of Little Endian byte order. It may not always be apparent which a device uses. If you
receive garbled data, try reversing the byte order. Reversing byte order is done using the
MoveBytes()
instruction. There is an example in CRBasic help for reversing the bytes order of
a 32-bit variable.
After properly reading in a value from a Modbus device, you might have to convert the value to
proper engineering units. With integer data types, it is common to have the value transmitted in
hundredths or thousandths.
Unsigned 16-bit integer
The most basic data type used with Modbus is unsigned 16-bit integers. It is the original Modbus
data type with 1 register per value. On the data logger, declare your destination variable as type
Long. A Long is a 32-bit signed integer that contains the value received. Select the appropriate
ModbusOption
to avoid post-processing.
Signed 16-bit integer
Signed 16-bit integers use 1 register per value. On the data logger, declare your destination
variable as type Long. A Long is a 32-bit signed integer that contains the value received . Select
the appropriate
ModbusOption
to avoid post-processing.
Signed 32-bit integer
Signed 32-bit integers require two registers per value. This data type corresponds to the native
Long variable type in Campbell data loggers. Declare your variables as type Long before using
them as the Variable parameter in
ModbusMaster()
. Select the appropriate
ModbusOption
to avoid post-processing.
Unsigned 32-bit integer
Unsigned 32-bit integers require two registers per value. Declare your variables as type Long
before using them as the
Variable
parameter in
ModbusMaster()
. The Long data type is a
signed integer, and does not have a range equal to that of an unsigned integer. If the integer
value exceeds 2,147,483,647 it will display incorrectly as a negative number. If the value does not
exceed that number, there are no issues with a variable of type Long holding it.
15. Communications protocols
94