How-to
How to Send String Data Over Modbus Between an HMI and a Gateway
Short answer
Modbus defines only coils and 16-bit registers, so an HMI cannot write a native string tag to a gateway. Pack the text as two ASCII characters per holding register, agree byte order and a length word at both ends, and rebuild the text in a gateway script before it is applied.
On this page
Modbus has no string data type. The protocol moves 1-bit coils and 16-bit registers, and it does not define what the bits in a register mean. An alarm recipient name typed on an HMI therefore reaches a notification gateway only if you pack it yourself: two ASCII characters per holding register, one agreed byte order, and a length word, with a small script at the gateway that rebuilds the text and writes it into the notification field. This article fixes the convention, shows the register map with the arithmetic, gives the steps for both ends, and names what each symptom means when the text arrives wrong.
What Modbus actually moves #
The application layer offers four tables: coils and discrete inputs at one bit each, input registers and holding registers at 16 bits each. Reading a contiguous block of holding registers is function 03; writing a block is function 16, up to 123 registers in one request. A register is a raw 16-bit word, and the Modbus specification does not define the format of the data in it — integer, half of a floating-point value, or two characters is a decision the two ends make.
That is why strings work in practice but are never native. Some PC-based drivers and gateways implement a string type mapped over a block of holding registers; many operator terminals and cloud gateways do not, and the missing data type you see in both tag editors is real, not a setting you overlooked. Check both editors first. The rest of this article is the path for the common case where neither side has a string object. Word order bites numeric tags in the same place, as with PLC real values that display wrong when the Modbus word order is swapped; text simply hides the problem one level deeper.
The convention to agree first #
Write these five decisions down before touching a tag, because both ends implement them and one mismatch garbles everything:
- Encoding: 7-bit ASCII, characters at codes 0 to 127. A plant-floor recipient name fits that. If you later need accented characters, UTF-8 stores one character in several bytes and the one-character-per-byte packing no longer holds, so restrict the entry field instead of widening the encoding.
- Density: two characters per holding register, one per byte — packing ASCII strings into 16-bit Modbus registers works through the same layout in depth.
- Byte order: the first character of each pair goes in the high byte. That is the order Modbus itself puts a register on the wire, and the order field devices use — a barcode reader that returns 13617 is handing you the characters
51, since '5' = 53 and '1' = 49 and 53 × 256 + 49 = 13617 (0x3531), as a barcode reader's 13617 register value explained works through. The opposite order exists in the wild, which is why you write it down and probe it. - Length: one register ahead of the data holding the character count. A length word beats a NUL terminator because the reader never scans for a zero byte and padding cannot be mistaken for more text.
- Maximum: 40 characters. That is generous for a recipient name and costs 21 registers including the length word — under a fifth of what one block write carries.
The register map, with the arithmetic #
The worked example uses the recipient name ALARM.LINE.014, 14 characters, so seven data registers plus the length word:
| Register | Characters | Value (decimal) | Value (hex) |
|---|---|---|---|
| 40001 | length = 14 | 14 | 0x000E |
| 40002 | A L |
16716 | 0x414C |
| 40003 | A R |
16722 | 0x4152 |
| 40004 | M . |
19758 | 0x4D2E |
| 40005 | L I |
19529 | 0x4C49 |
| 40006 | N E |
20037 | 0x4E45 |
| 40007 | . 0 |
11824 | 0x2E30 |
| 40008 | 1 4 |
12596 | 0x3134 |
Each data word is the first character's code × 256 plus the second's: 40002 holds 'A' = 65 and 'L' = 76, so 65 × 256 + 76 = 16716. An odd character count pads the low byte of the last register with 0x00 or 0x20, and the length word says how many characters are real. The 40001 numbering is the 4x reference; the request on the wire carries offset 0 for the same register, and how the 4x address convention decodes between PLC and HMI is the offset habit to align before anything else.
The procedure, end to end #
- Open the Modbus tag editor on the HMI and the tag list on the gateway. If both expose a string type over a holding-register block, map it with the same base register, character count and byte order, and stop here.
- On the HMI, take the text from the entry object in its scripting layer. Reject entries longer than the agreed maximum or containing a character above code 127, and say so on the screen — silent truncation produces a valid-looking wrong address.
- Pack the text two characters at a time exactly as the table shows, and write the length word and the data words in one function-16 write, so the gateway never sees a half-updated block. If your write tool cannot do a block write, write the data first and the length word last.
- On the gateway, script a rebuild that triggers on a change of the length word: read the block, unpack high byte first, join exactly the number of characters the length word names, and write the result into the notification recipient field. Confirm first, in the gateway's own documentation, that its notification configuration accepts a script-written recipient.
- Probe the byte order with
AB: 'A' = 65 and 'B' = 66, so the register must read 65 × 256 + 66 = 16706 (0x4142). If the far end rebuildsBA, reading 16961 (0x4241), one side is reversed; change one end only. - Run the full test with the example recipient name: check the eight registers against the table, check the rebuilt string character by character, then send a real notification to a mailbox you control. Keep a timestamped log of the raw block and the rebuilt string for the first weeks.
One safety note, because this path ends in a notification: an email is an operator convenience, not a safety function. Machine stop and alarm shutdown stay hardwired and independent of anything this string transfer does.
When the text arrives wrong #
- Pairs reversed —
ALARM.LINE.014rebuilds asLARA.MILEN0.41— with correct register values is byte order. The unpack reads the low byte first; flip one end. - The whole block shifted by one register is the base-address offset, not the data. Re-check the 4x-versus-zero-based start register on both ends.
- An empty or one-character result at the reading side means the reader's string settings do not match the map. Some operator terminals return an empty string rather than an error when the length or order option is wrong, so an empty read is a settings symptom, not proof that nothing was written.
- No requests reaching the gateway at all is a transport problem, not an encoding problem; diagnosing an HMI Modbus device with no response over RS-485 is that checklist. The packing here is identical over RTU and TCP — only the transport checks differ.
- Rebuilt text correct but recipient unchanged: the transfer has done its job, and the missing piece is the gateway-side step that applies the value to its notification configuration.
The decision for each case #
- Both ends expose a string tag type over registers: use it. Align base register, character count and byte order, run the
ABprobe once, and you are finished. - Neither end does, the usual HMI-plus-gateway pairing: commit to this packing — ASCII only, two characters per register, first character in the high byte, one length word, one block write, script rebuild at the gateway, 40-character maximum. Every rule fits on one line, which is what makes it maintainable.
- The gateway offers no supported way for a script to set its notification recipient: stop. Set the address in the gateway's own configuration interface and leave the HMI a display-only copy, or move the notification duty to a device designed to take its addressing from the plant floor.
Questions asked
Is Modbus ASCII mode the same thing as sending ASCII text?
No. Modbus ASCII is a transmission mode, not a text format: each byte of the frame goes out as two hexadecimal characters, and the character size drops to 7 data bits. It changes the framing on the wire, not what your data means. A string still has to be packed into registers whether the link runs RTU or ASCII mode, and the register map in this article works unchanged on both.
How many registers does a string need?
For n characters, take n divided by 2, rounded up, data registers, plus one register for the length word. A 14-character recipient name uses 7 plus 1, so 8 registers; a 40-character maximum uses 20 plus 1, so 21. An odd count pads the low byte of the last register with 0x00 or 0x20.
Can the PLC pack the string instead of the HMI?
Only if it must be in the path. The packing needs a device that can handle text character by character, so it lives wherever the scripting lives, usually the HMI or the gateway. A controller that merely relays the registers adds nothing. Put the PLC in the text path only when the HMI has no scripting layer or cannot reach the gateway directly, and only if its instruction set actually supports character handling.
Why does the gateway show the right characters in swapped pairs?
One end packs the first character of each pair into the high byte and the other reads it from the low byte, so every pair comes back reversed while the register values themselves are correct. Fix it at one end only, in the unpack order at the gateway or the pack order at the HMI. Changing both reverses it twice and buries the mismatch in the documentation instead of the code.