IMDHMIControls LibraryShop main store

Troubleshooting

Why a CODESYS Modbus master gives error 150 or 153

Industrial networks Published 8 min read

Short answer

Error 150 means the master never opened the TCP connection to the slave; error 153 means it sent the request and no reply came back in time. Work the path in order: rebuild the library on a compiler it supports, confirm the slave speaks the transport you selected, then scope the firewall rule to the runtime executable and prove the exchange at a packet capture.

On this page
  1. What the two codes tell you
  2. Clear compile errors 3820 and 3821 before touching the network
  3. Confirm the slave speaks the transport you selected
  4. Scope the firewall rule to the runtime executable
  5. Fire one request that cannot fail
  6. Prove the path at a packet capture
  7. The decision per case
  8. Questions asked

Error 150 and error 153 from a CODESYS Modbus master function block are socket verdicts, not Modbus protocol answers. 150 means the master never got the TCP connection open — the library reports it as "server not available". 153 means the request left the runtime and no valid reply came back inside the timeout. In both cases the exchange never completed, so no coil or register value is in play yet. Work the path in a fixed order: make the library compile on a compiler generation it targets, confirm the slave actually supports the transport you selected, then scope a Windows firewall rule to the runtime executable itself, and let a packet capture arbitrate whatever remains. A ping is not evidence at any step, because ICMP and a Modbus socket are separate permissions.

What the two codes tell you #

The codes come from the error output of the Modbus-over-Ethernet master blocks for the classic CoDeSys generation; the reported bench paired CoDeSys 2.3.01 with the RTE on 32-bit Windows 7 and produced 150 from the TCP block and 153 from the UDP block against the same device. Turn each code into its next check:

  • 150 — the TCP connect never completed. The cause sits before the Modbus layer: wrong address or port, no listening server on the slave, or a firewall swallowing the outbound attempt. The slave's application never saw a request.
  • 153 — the request went out over an established connection or as a UDP datagram, but nothing matching it returned in time. The usual causes are a slave with no service on the chosen transport, a blocked return path, or a timeout shorter than the slave's documented response time.

Neither code is a Modbus exception code. Exception codes travel inside a valid response frame from the slave, so the day you receive exception 01 or 02 the hard part is done: the path works and the fault has moved into the request fields.

Clear compile errors 3820 and 3821 before touching the network #

Two library stacks were tried on the reported bench and both stopped the compiler, which means no packet could ever have existed. The stops were:

Library Declared dependencies Compiler stop
ModbusEthernet_04.lib Ethernet.lib plus Mod_com.lib, which declares the CRC16 type #3821 at ETH_is16Bit (7) — at least one input required for functions
WAGOLibModbus_IP_01.lib SysLibSocket.lib #3820 at ModbusRequest(36) — VAR_OUTPUT and VAR_IN_OUT not allowed in functions

Mod_com.lib was not named anywhere in the first stack's documentation; it had to be tracked down because the CRC16 type used by the master block lives there, so count dependencies as part of the install rather than as a surprise. The reported root cause for both stops was the compiler generation: the 2.3.01 build was identified as very old, and the compile problems were attributed to that version rather than to the libraries. Error #3820 is the old compiler refusing an output-variable declaration inside a function, a construct newer CODESYS compiler releases accept, so the fix is a compiler and runtime generation the library targets. Do not edit the library's function declarations to silence #3820 or #3821; changing signatures alters calling semantics inside code you did not write.

Gate one is a clean rebuild with the master function block instantiated and no unresolved or language-validation errors. Nothing downstream is diagnosable until it passes.

Confirm the slave speaks the transport you selected #

Modbus TCP is the transport the specification defines for Ethernet: the master opens a TCP connection to the slave's listening server before the first request, and that server listens on TCP 502 unless the device documents another port. Modbus TCP does not use UDP. A Modbus-over-UDP variant exists only where a device's documentation explicitly lists it, and a UDP request is a datagram with no session — a device without a UDP service simply stays silent, which is exactly what error 153 reports.

That is the reported bench result in miniature: the Cognex DataMan 60SX barcode reader timed out on UDP in the reported tests, and its Modbus UDP support was never confirmed. Set the transport from the device's protocol list, not from whichever library happens to compile. If the documentation names only Modbus TCP, use the TCP function block; where both are documented, TCP is the default because it is the transport the specification defines.

Once a connection is up, the unit identifier becomes the next variable. Native TCP devices often fix or ignore it; a TCP-to-serial gateway routes it to the serial slave behind it, so the value comes from the device or gateway documentation. The same trap exists on serial: a poll aimed at slave address 0 instead of your device is its own failure, covered in why Modbus RTU sends to slave address 0 instead of your device.

Scope the firewall rule to the runtime executable #

On the reported bench the only firewall permission granted at installation covered another component, suspected to be the OPC server. A rule granted to one executable does not authorize another: Windows judges each outbound socket by the process that opens it, so the Modbus socket your runtime opens needs its own rule. Configure it with the outbound direction, the runtime's own executable as the program, the network profile the LAN adapter uses, TCP as the protocol, and the remote port set to the slave's Modbus TCP listening port. No extra port has to be reserved: the destination is the slave's listening port, and the rule decides who may open the socket, not which port exists.

Check the route while you are there. A PC with more than one adapter can carry the runtime's packets out one interface while a ping you ran by hand took another, so confirm the slave's subnet routes through the adapter the runtime actually uses, and capture on that same adapter.

Fire one request that cannot fail #

  1. Enter the slave address you verified at the device itself — for example 192.168.1.50 — plus the documented transport and the documented listening port.
  2. Take the unit identifier from the device or gateway documentation.
  3. Read one coil at a documented address, quantity 1, before attempting any write.
  4. Trigger exactly one request edge, then wait for done or error before issuing another request.
  5. Compare the returned state against the device's own indication.

Address notation is the classic trap at this step. Device documentation may print a coil reference while the request field expects an offset, so use the form the library's address field requires and check it against the device's Modbus map. If the connection is up and the read comes back as an invalid-address exception, that is the register convention biting — the same convention behind why Modbus error 8189 needs 400001 added to the register address.

Keep the first write on a coil you are allowed to change and restore it afterwards; a coil read is harmless, a coil write moves the machine.

Prove the path at a packet capture #

Run a capture on the Windows adapter that faces the slave — Wireshark or an equivalent analyzer — and trigger a single request. The frame list separates a runtime problem from a network problem without adding any ping routine to the project:

  • No outbound TCP attempt when you trigger the block: the failure is before the socket — the task is not calling the block, the execution edge never arrives, or the rebuilt project is not actually on the controller.
  • SYN leaves, no SYN-ACK returns: wrong address or port, the slave's server is not listening, or a firewall is dropping the attempt.
  • Handshake completes but no request frame appears: socket handling inside the runtime; re-check the rule scoped to the runtime executable.
  • Request and response both visible while the block still reports 153: the wire is proven innocent and the fault is in the block's endpoint fields or the library and runtime pairing. A connected session that yields no usable data through a gateway is its own pattern, covered in why an S7-1200 Modbus TCP client shows no data through a gateway.
  • UDP request leaves, no reply returns: one frame on the wire instead of two — treat that as missing UDP support at the device until its documentation says otherwise.

Worked example: set the block's timeout input to 1000 ms and take the device's response budget from its documentation — 50 ms as an example budget. One TCP read then shows 3 handshake frames, 1 request frame and 1 response frame: 3 + 1 + 1 = 5 frames before teardown, with the response landing 12 ms after the request. Both budgets hold, because 12 ms sits inside the 1000 ms timeout and the 50 ms device budget, so if the block still reports 153 over this capture, swap the endpoint fields first and the library and runtime pairing second; the network needs nothing.

The decision per case #

  • Compile stops at #3820 or #3821: the compiler and runtime generation do not match the library. Move to a generation the library targets and add every declared dependency, including the undocumented Mod_com.lib; do not edit declarations. Gate: a clean rebuild with the block instantiated.
  • Error 150 on TCP: the runtime cannot open the session. Confirm the documentation names Modbus TCP on the configured port, then give the runtime executable its own outbound rule on the profile the LAN uses. Gate: the capture shows SYN and SYN-ACK.
  • Error 153 on UDP: confirm the device documents Modbus UDP; if only TCP is listed, switch to the TCP block — the reported barcode-reader case timed out exactly this way. Gate: two frames, request and reply.
  • Error 153 on TCP: read the capture. Reply visible and early means endpoint fields or the library and runtime pair; no reply means the return path, the slave's server state, or a timeout shorter than the device's documented response time.

Fix the build first, prove the transport second, scope the firewall third, and let the capture end every remaining argument — the function block's error output is the verdict, the capture is the evidence.

Questions asked

Does a successful ping rule out error 150 and error 153?

No. Ping uses ICMP, which Windows answers itself, so it proves only that the PC has an IP path to the device. A Modbus request needs the runtime process to open its own socket, and Windows judges that socket by the rule scoped to the runtime executable. A device can answer every ping while the firewall silently drops the runtime's first attempt to connect to the Modbus TCP port, which still ends as error 150 or 153.

Are error 150 and error 153 Modbus exception codes?

No. Exception codes travel inside a valid response frame from the slave, so receiving one means the connection and the reply path already work. Codes 01, 02 and 03 mean illegal function, illegal data address and illegal data value: the slave understood the frame but rejects the request contents. 150 and 153 come from the master function block itself and mean no valid response arrived at all, so treat them as socket verdicts rather than protocol answers.

Which port should the master target?

The Modbus TCP specification assigns TCP port 502 as the listening port, so a device that documents no other port listens there. Devices that accept a configurable port state it in their own documentation, and a gateway between TCP and serial may be configurable too, so the value in the master's port field comes from that device's documentation. Modbus over UDP has no assigned port; where a device documents UDP at all, it names the port in the same place.

Can I build the master myself on raw sockets instead of a library?

Yes. The exchange is one frame pair: the request carries a seven-byte MBAP header made of a transaction identifier, a protocol identifier set to zero, a length field and the unit identifier, followed by the function code and its arguments, and the response mirrors the same transaction identifier. You manage connection state, match replies by transaction identifier and enforce the timeout yourself. That removes the library's compiler dependencies, but you own every failure mode the function block used to report.

Keep reading

  • Troubleshooting

    Why an S7-1500 X2 Modbus server accepts TCP but no DONE arrives

    The client's 0001-to-7005 loop means it builds a connection, sends a request and never receives an accepted response; the completed 7006 state and DONE never arrive. A blank Telnet session proves only that X2 accepts TCP on port 502. Match TCON_IP_v4.InterfaceID to X2's hardware identifier from device configuration, then read the one-scan ERROR and STATUS with a trace.

    Industrial networks

  • Troubleshooting

    Reading Modbus TCP from a ControlLogix PLC without a gateway

    Yes — ControlLogix reads Modbus TCP without a gateway card through Rockwell's sample Modbus TCP driver, but only on controller firmware version 20 or later with socket-capable Ethernet hardware such as a 1756-EN2xx module at firmware 5.007 or later. If either side misses the gate, use a standalone EtherNet/IP to Modbus TCP converter. When the driver runs, prove one request against a known-good PC client and match unit ID, start address and count field by field.

    Industrial networks

  • Troubleshooting

    Why a Micro850 will not read tags over a DF1 I/O server

    A Micro850 serves variables through a Modbus map you build in Connected Components Workbench, not through DF1 or ABLOGIX I/O servers, because its memory has no data-table files for those drivers to read. Map each variable, poll the mapped range with an independent Modbus client on unit identifier 1, and set the tag address to the offset that actually answers.

    Industrial networks