Data types
All multi-byte values are big-endian, as required by Modbus.
| Type | Registers | Range or content |
|---|---|---|
UINT16 | 1 | 0 to 65535 |
INT16 | 1 | −32768 to 32767, two's complement |
UINT32 | 2 | 0 to 4294967295, high word first |
INT32 | 2 | −2147483648 to 2147483647, two's complement, high word first |
STRING | length | Text, two bytes per register, see Strings |
BLOCK | length | Raw 16-bit words with no interpretation |
BLOCK is only used for internal registers and
reserved ranges.
32-bit values
32-bit values use big-endian word order: the register at the lower address holds the most significant 16 bits. PLC tools call this "high word first", "big-endian" or "ABCD".
For example, the value 123456 (0x0001E240) is sent as two registers:
| Register | Value |
|---|---|
| first (lower) | 0x0001 |
| second (higher) | 0xE240 |
If a 32-bit value reads back with its halves swapped (123456 reads as 3795845121), the tool is configured for little-endian word order.
Saturation
A value that does not fit the item's type is saturated to the nearest value
the type can hold when read. It is never wrapped. For example, a counter at
70000 reads as 65535 through a UINT16 item, and a negative value reads as 0
through an unsigned item.
Use a 32-bit type for any value that can exceed 65535.
Strings
A STRING item of length L occupies L registers and holds up to 2·L
bytes of UTF-8 text. The bytes are packed two per register, the first byte in
the high byte of the register:
"AB12" → 0x4142 0x3132
"ABC" → 0x4142 0x4300
When read, text shorter than the item is padded with NUL (0x00) bytes, and
text longer than the item is truncated.
When written, trailing NUL bytes are removed before the text is used. A client may therefore either write the whole item, padded with NUL bytes, or write only the registers it needs, starting at the item's first register:
- A function 16 write that starts at the item's first register may stop before the end of the item. The rest of the item is cleared.
- A function 06 write to the item's first register writes one register, that is up to two bytes, and clears the rest of the item.
Written text must be valid UTF-8, otherwise the write returns exception 03. Plain ASCII is always valid UTF-8. Characters outside ASCII take two to four bytes each, so fewer of them fit in an item.
Example
The lot number L0042, written to the 8-register item at address 2 in the
example mapping list:
| Register | 2 | 3 | 4 | 5–9 |
|---|---|---|---|---|
| Bytes | L 0 | 0 4 | 2 NUL | NUL |
| Value | 0x4C30 | 0x3034 | 0x3200 | 0x0000 |
Writing only registers 2–4 gives the same result.
Many PLCs store strings with a length byte or in little-endian byte order. Convert to the layout above before writing, and after reading.