System designExplainer

Modbus TCP unit ID for machine vision devices

When the Modbus TCP unit ID matters for a vision device: direct connections ignore it, gateways route by it, 255 reaches the gateway itself, 0 broadcasts.

LumaSpec Vision10 min read Share on LinkedIn

On this page 9 sections
  1. 01One byte in a seven-byte header
  2. 02Why a direct connection ignores it
  3. 03Where the unit ID routes: gateways and bridges
  4. 04Worked example: one gateway, four light stations
  5. 05A five-minute test that proves who answered
  6. 06Choosing unit IDs before you build
  7. 07When the usual answer is wrong
  8. 08Checklist before you commission
  9. 09Questions

You have a light controller or a vision controller to put on Modbus TCP, and the sample code you started from never mentions a unit ID. On a direct connection the value usually does not matter: the IP address already selected the device, and most stacks answer whatever you send and echo it back. That comfort ends the moment a TCP-to-serial gateway enters the line, because the gateway is the one component that routes on the unit ID. This guide gives you the header layout, the cases where the value must be right, a timing worked example for a gateway drop, and a short test that proves which device answered.

One byte in a seven-byte header

Every Modbus TCP exchange is a function code plus data — the PDU — wrapped in a seven-byte MBAP header, carried on TCP port 502. The header has four fields:

  • Transaction ID — 2 bytes, your counter, copied untouched into the response. This is how a client matches replies to requests.
  • Protocol ID — 2 bytes, always 0x0000 for plain Modbus.
  • Length — 2 bytes, the count of every byte that follows it, unit ID included.
  • Unit ID — 1 byte, values 0 to 255.

The unit ID descends from the Modbus RTU slave address. On a serial bus that address is the only routing a frame has. On TCP, the connection to a device's IP address does that job first, which is why the byte can look dead.

MBAP Length = 1 (unit ID) + PDU byte count response PDU (function 03) = 1 + 1 + 2 x registers read

Read Holding Registers, function code 03, moves at most 125 registers per request, so the largest legal response carries MBAP Length = 1 + 1 + 1 + 250 = 253.

Why a direct connection ignores it

Open one connection to one device at one IP address and there is exactly one slave behind that socket. Most stacks accept any unit ID, execute the request, and copy your value into the response; sample code and command-line test tools bury the parameter for exactly that reason, because on the bench it changes nothing. The reply is matched on the transaction ID, not the unit ID.

A minority of stacks police the byte and answer a mismatch with an exception or with silence, and nothing stops a firmware update from adding that check to a device that ignored it yesterday. Treat the unit ID as part of the device contract: send the documented value even after you prove that a wrong one works. How strictly single devices treat the unit ID sorts the two behaviors apart.

A vision controller is the classic single-IP device: the PLC polls one box instead of several cameras. The MV-VC3301H-128G66 carries 4 built-in light channels and 5+ LAN ports, so a single station address can serve the whole cell. The unit ID rides along unused while the register map does the real work, and PLC mapping that starts at register 50000 walks through that half of the contract.

Vision controllersHikrobot MV-VC3301H-128G66 I3-8100, 8GB+128G SSD, No PoE, PCIe expansion supported, Incl. VM6200Pro licenseMV-VC3301H-128G66
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
Light outputs
4 built-in channels
Memory
8 GB
$2,419.00View partBuild a system with it

Where the unit ID routes: gateways and bridges

A TCP-to-serial gateway is one IP address serving several Modbus RTU slaves. The IP address cannot tell them apart, so the gateway forwards on the unit ID: the byte must equal the target's serial slave address. Send 4 and the gateway puts address 04 on the wire. Send 255 and most gateways keep the request for themselves, because 255 is reserved for reaching the gateway's own identification interface. Unit ID 0 has no broadcast meaning in TCP, but a gateway may map it to the RTU broadcast address, so a write sent at 0 can land on every slave on the drop.

Light controllers with serial command ports are the usual residents behind a gateway. The LS-CTL-0487 is a 2-channel 24 V digital LED light controller with an RS-232 command port; in front of a gateway, the unit ID that reaches it comes from the gateway's port mapping, so read that mapping before you blame the controller. Selecting unit identifiers for shared-port devices covers the mapping rules in depth.

Light controllers2-channel 24 V digital LED light controller, 240 W, RS-232 (LS-CTL-0487)LS-CTL-0487
Output
Constant current, Constant voltage 24 V
Channels
2 channels
Control
Digital
$385.00View partBuild a system with it

Worked example: one gateway, four light stations

Four Modbus RTU light controllers sit on an RS-485 drop at slave addresses 1, 2, 3 and 4, behind one gateway. The PLC sweeps all four, reading 8 holding registers (function 03) from each at 19200 baud, 11 bits per character.

Per station, on the serial side:

  • Request PDU: 1 (function) + 2 (start address) + 2 (quantity) = 5 bytes, so MBAP Length = 1 + 5 = 6.
  • Frame onto the wire: 1 (slave address) + 5 (PDU) + 2 (CRC) = 8 bytes = 88 bits, and 88 / 19200 baud = 4.6 ms.
  • Frame back: 1 + 1 (byte count) + 16 (8 registers x 2 bytes) + 2 (CRC) = 21 bytes = 231 bits, and 231 / 19200 baud = 12.0 ms.
  • Wire time 16.6 ms plus about 5 ms of slave turnaround is roughly 22 ms per station.
t = (request bytes + response bytes) x 11 bits per character / baud rate

Four stations: 4 x 22 ms = 88 ms, call it 90 ms per full sweep. Drop to 9600 baud and every wire time doubles, so the sweep grows to about 150 ms and no longer fits a 100 ms PLC scan — poll one unit ID per scan in rotation instead. That is the same one-loop discipline that keeps several Modbus TCP cameras on one PLC CPU inside their scan budget.

The response that reaches the PLC carries MBAP Length = 1 + 18 = 19 and a unit ID byte equal to the address you targeted. That echo is free confirmation that the gateway routed the frame where you meant it to go.

A 4-channel digital control box such as the LS-CTL-0070 puts four strobe channels behind a single RS-232 port, so one unit ID at the gateway covers all four lights — the channel count lives in the registers, not in the addressing.

Light controllersDigital control box, 4 ch, 200 W, RS232 (LS-CTL-0070)LS-CTL-0070
Output
Constant voltage 24 V
Channels
4 channels
Control
Digital
Feature
Strobe
$549.00View partBuild a system with it

A five-minute test that proves who answered

  1. Open one TCP connection to the device IP on port 502.
  2. Read 8 holding registers with unit ID 1 and note the transaction ID and the unit ID in the reply.
  3. Repeat the identical read at unit ID 2, then at 120.
  4. Same data from all three: the device discards the unit ID and the connection is doing the routing. Only one value answers: the device polices the byte, so configure exactly that value.
  5. Behind a gateway, aim the read at a known slave address. A wrong ID should return exception 0x0B, Gateway Target Device Failed to Respond, or a clean timeout; either answer means the frame had nowhere to go.
  6. Between two reads, switch a light channel off at the controller. If the register still shows the old state, you are reading a cached frame, not the device.

Choosing unit IDs before you build

Architecture What routes the request Unit ID to configure Values that bite
One device, its own IP the TCP connection the documented value; 1 when undocumented none today, any after a strict firmware
One gateway, N serial slaves the gateway's ID-to-address map each slave's address, 1-247 255 (the gateway itself), 0 (broadcast), unmapped IDs
Several devices, several IPs each connection 1 on all of them, one convention mixed conventions between lines
Vision controller with built-in light channels one IP, one stack 1 none; spend the effort on the register layout

Number serial slaves 1, 2, 3 upward at commissioning and write the map on the panel door; retrofit renumbering is where mismatches breed. The field holds 255 values, but PLC drivers sometimes allow fewer, and a unit ID limit worked around for 40 RTU slaves shows what that looks like when it bites. When the payload matters more than the address, sizing the register block for a vision coordinate array is the next level down.

When the usual answer is wrong

It worked until the firmware update. A device that ignored the byte starts validating it, and every tag carrying a casual value times out at once. The fix is configuration, not code: set the documented unit ID everywhere, then re-run the two-value test from the section above.

The gateway answers, the slave never sees it. Exception 0x0A, Gateway Path Unavailable, means the gateway knows no path for that ID; 0x0B means the path exists but the slave stayed quiet. Both point at the mapping table or the serial side — power, termination, baud — not at your PLC logic. To get the serial side to a known-good state, getting the first reply over RS-232 is the shortest route.

Reads succeed, writes fail. That is function support, not addressing: a device that serves read functions but rejects single-register writes returns exception 01, Illegal Function. Why a vision camera rejects a Modbus write dissects exactly that failure.

Checklist before you commission

  • One line in the commissioning sheet per device: IP, port, unit ID, and where the value came from.
  • Serial map on the panel door: slave address, baud and parity for every controller on the drop.
  • No tag pointed at 0 or the reserved value 255.
  • Two-value test captured and filed for each direct device.
  • Poll budget computed: stations x 22 ms at 19200 baud for 8-register reads, and one unit ID per PLC scan when the sweep does not fit.
  • Register layout reviewed next: function 03 caps a read at 125 registers, so split large arrays across requests.

When you add the hardware, start from the light controllers category, the light controller finder or the controller step of the configurator; for the boxes that aggregate cameras and lights, see vision controllers. Bring the unit ID map to the contact page and the applications team will check it against the parts.

Questions

Does a Modbus TCP device check the unit ID you send?

Most do not: the connection to one IP address already selected them, so they accept any value and echo it back. A strict minority answer a mismatch with an exception or silence, and a firmware update can move a device into that group. Some stacks go further and reply with their own unit ID — often 0 or 1 — whatever you sent, which clients that compare the field then discard. Send the documented value and none of this touches you.

What unit ID should I configure for a single vision device?

The value its documentation names, which is 1 on most stacks. When nothing is documented, use 1 for every direct TCP device on the line: distinct IP addresses already keep them apart, and one convention means one less difference between stations. Keep 0 and 255 out of the plan — 0 can become a broadcast behind a gateway and 255 belongs to the gateway itself.

Why does a read work at unit ID 1 but time out at 2 through the same gateway?

The gateway forwards only the unit IDs its mapping table routes to a live serial slave. Unit ID 2 may be unmapped, or its slave may be powered down or off the bus; the gateway then returns exception 0x0B, Gateway Target Device Failed to Respond, or lets the request time out. Fix the mapping table or the serial side, not the PLC code: every configured ID should equal a real slave address.

Do I need a separate TCP connection for every unit ID?

No. One connection to a gateway carries requests for as many unit IDs as the gateway serves, and most PLC drivers let a single connection address several units by tag group. That is the economy of a shared port: four slaves, one socket, one poll loop. Connections multiply per IP address, not per unit ID.

How many devices can share one gateway's unit ID space?

The field itself holds 0-255, and Modbus RTU addresses slaves 1-247 with 0 reserved for broadcast, so 247 is the protocol ceiling per drop. Practical limits arrive earlier: some PLC drivers support fewer IDs, and poll time grows linearly — at 22 ms per slave for an 8-register read at 19200 baud, 40 slaves is roughly 0.9 s per sweep.