IMDHMIControls LibraryShop main store

Troubleshooting

Why a custom Modbus TCP slave fails on port 502

Industrial networks Published 6 min read By IMDHMI controls team

Short answer

The SlimLine operating system already runs its own Modbus TCP slave on TCP port 502, so the second server in your program cannot bind that port and faults. Delete the custom SysTCPServer and ModbusSlave_v5 file, keep the Modicon M241 as master, and let it read or write DB100 through the built-in slave, whose registers begin at 40000 and accept the functions 16#01, 16#03, 16#06 and 16#10.

On this page
  1. Why the SlimLine refuses the second slave
  2. Delete the custom server file
  3. Add the SlimLine to the M241 project
  4. Read ten registers at 40000 to prove the map
  5. Keep your own slave only for a different map
  6. Symptom-to-cause checks
  7. Questions asked

The fault on the SlimLine is a port collision, not a logic error: its operating system already runs a Modbus TCP slave of its own, so the SysTCPServer and ModbusSlave_v5 pair in your program cannot claim that endpoint and faults every time it starts. Delete the whole custom server file; the built-in slave already exposes the DB100 area, so the program duplicates a service the controller already provides. Keep the Modicon M241 as the master — it already drives your inverters and the other M241 controllers — add the SlimLine beneath its existing Ethernet device, and prove the register map with one read of ten registers, worked through below.

Why the SlimLine refuses the second slave #

A TCP endpoint is one IP address plus one port, and an IP stack lets a single listener hold that pair. The SlimLine firmware brings up its own Modbus TCP service with the controller, so by the time your program asks for the port, it is taken: the bind request from a second server is refused, and the fault you see is that refusal. Connection counts, buffer sizes and flush timers cannot change the outcome, because the request dies before any of them applies.

The built-in slave is not a stub. It maps the whole DB100 area, serves up to three masters at the same time, and answers the four functions in this table with register addresses that begin at 40000.

Function Work it does on the built-in map
16#01 Reads bits from the mapped area
16#03 Reads 16-bit registers from the mapped area
16#06 Writes one register
16#10 Writes a block of registers

Keep that service and build on it. The same ownership rule decides other port fights in controls work: an FX3U-ENET module that cannot serve Modbus TCP on port 502 fails for the identical reason, a listener that came up earlier already holds the pair.

Delete the custom server file #

Remove the entire program file, not just the call to it. The file carries the SysTCPServer instance, the ModbusSlave_v5 instance named SMdb, and the first-loop block that set LocalAdd to 0.0.0.0, MaxConn to two connections, LifeTm to zero seconds, FlushTm to 50 ms and RxSize and TxSize to 128 each. Leave none of it behind: a partly deleted server still asks for the port on the scan where it runs.

The node value goes with it. SMdb.Node belonged to the deleted slave, and the built-in service is managed by the operating system without any added program, so nothing in your code configures it. Set the unit field inside each M241 transaction instead, and let a read decide it: data coming back confirms the field, and an exception points at it first.

Add the SlimLine to the M241 project #

In the M241 project, add a Modbus TCP slave device beneath the existing Ethernet device. Slave here names the remote SlimLine from the master's point of view; the M241 stays a master and keeps polling the inverters and the other M241 controllers it already serves. Enter the SlimLine's LAN address in the device entry and leave the port at 502, the built-in service's setting. Make the first transaction a small read of holding registers, so the link proves itself without writing to controller memory, and add write transactions only after that read returns data.

Keep the two fault families apart. A connection that never opens points at the device address, the port, the network path or exhausted connection capacity on the SlimLine. An exception arriving over an open connection points at the unit field, the function, the register range or the quantity instead.

If the roles ever reverse and the SlimLine polls an M241, instantiate the ModbusMaster block in the SlimLine and read the M241's %IW and %QW objects with a holding-register read, writing %IW with the single-register or block write functions.

Read ten registers at 40000 to prove the map #

Inputs: function 16#03, a holding-register read, taken from the command table above; base register 40000, the first address of the built-in map; quantity ten registers, an example choice kept deliberately small for a first request; test value 12345, an example pattern placed in the first word of DB100 from the programming tool before the M241 connects.

The rule in words: the built-in map lines register 40000 up with the first word of DB100, one register is one 16-bit word or two bytes, so register n sits at byte offset (n - 40000) x 2 inside DB100, and a read of Q registers from the base returns 2 x Q bytes.

The arithmetic: last register 40000 + 10 - 1 = 40009; byte span 10 x 2 = 20 bytes, so the request covers DB100 byte offsets 0 through 19.

Result: the M241 receives 20 bytes, ten registers, and the first one holds 12345, the value placed in DB100.

Decision: matching values prove the listener, the framing and the address translation in one step. Leave the custom file deleted, and extend the device entry with 16#06 for a single register or 16#10 for a block, watching each target online. Keep the words you map away from outputs on running equipment until a write path has been proven against a harmless target. An exception instead of data says shrink the quantity and recheck the unit field; a refused connection says count the live masters against the built-in slave's three-connection limit.

Keep your own slave only for a different map #

ModbusSlave_v5 earns its place only when the application needs a memory area other than DB100, or a register base other than the default one. Then give your slave a free port, or move the operating system service off its default port first and reconfigure every client with the new numbers. Because either branch changes every client configuration, the built-in DB100 map stays the simpler answer whenever it covers the application.

Symptom-to-cause checks #

Work down this list from the symptom you actually see:

  • Fault the moment the custom server starts — the port is already held by the built-in service. Delete the whole file.
  • The M241 never opens the connection — wrong device address, wrong port value in the entry, service unreachable, or the network path down. Reach the SlimLine with a ping, then recheck the entry.
  • The connection opens but the request returns an exception — unit field, function, register range or quantity. Cut back to the small read from the worked example.
  • Data arrives but reads wrong — offset, byte order, word order, width or sign interpretation differs between the two programs. Write a recognizable pattern into DB100 and compare raw register words before applying any scaling.
  • The read passes and the write does not — wrong write function, or a target the SlimLine application rewrites. Use 16#06 for a single register and 16#10 for a block, and watch the exact target online.
  • Some masters connect and one will not — the built-in slave holds at most three. Count live clients, including test utilities, and close the stale ones.

When several clients share one server, connection capacity and instance discipline decide who gets served; the note on why two MB_CLIENT instances stop Modbus TCP reads from one server covers the client-side half of that problem.

Questions asked

Does the node value of the deleted Modbus slave stay at 1?

No setting carries over. The node field belonged to the ModbusSlave_v5 instance in the deleted file, and the built-in slave is run by the operating system without any program, so your old code configured nothing that survives. Set the unit field inside each M241 transaction instead, prove it with one read of the DB100 area, and change it only if that read returns a Modbus exception rather than data.

How many Modbus TCP masters can the built-in slave serve at once?

Three master devices can be connected to the service. Count every client you attach, including a test utility running on a PC, and close test tools once the check is done so the M241 never competes with a forgotten connection for one of the places.

Will different socket settings let the custom server bind?

They cannot. The refusal happens when the custom server asks the IP stack for a port that already belongs to the firmware's listener, before buffer or timing settings matter. FlushTm, LifeTm, RxSize, TxSize and even a smaller MaxConn shape a socket that never opened, so no combination frees the port; removing the program is what frees it.

How can I check the built-in slave before touching the M241 project?

With the custom server file removed, run a Modbus TCP master test utility from a PC on the same LAN, address the controller's IP, and read holding registers from the base of the map. If the values you placed in DB100 come back unchanged, the service and its register layout are alive, and any remaining fault sits in the M241 device entry or the network path.

Keep reading

  • Troubleshooting

    Why WinCC Modbus TCP reads fail on an FX5U server

    A Modicon-driver WinCC connection to an FX5U Modbus TCP server fails when it uses port 299 and remote slave ID 255 while the FX5U listens on port 502 and answers unit ID 1, and when D0 is not addressed as holding register 4x400001. Correct those three settings, then verify ping, TCP port, a capture and a known value in D0.

    Industrial networks

  • Comparison

    Choosing how a Siemens PLC talks to a Modicon M580

    An S7-1200 or S7-1500 and a Modicon M580 need no gateway: both speak Modbus TCP on their existing Ethernet ports, with the M580 as server and MB_CLIENT on the Siemens side or MB_SERVER polled by the M580's I/O scanner, and OPC UA is native to both. Add a PROFINET-to-Modbus TCP gateway only when a participant such as a PROFINET-only drive control unit cannot take a Modbus role, because the M580 has no PROFINET stack.

    Industrial networks

  • How-to

    Connecting third-party Modbus TCP devices to a Modicon MC80

    Yes. A Modicon MC80 under Control Expert Classic exchanges data with any third-party Modbus TCP device through explicit READ_VAR and WRITE_VAR calls plus the device's register map — there is no ESD or EDS import, no IO Scanning and no DTM support. Run the MC80 as the TCP client, keep the exchange in the coil and holding-register tables, and latch each result in the reader so a 16 ms update survives a slower PLC poll.

    Industrial networks