Skip to main content

Modbus TCP Server/Slave Codec

Simulates a Modbus TCP device. Together with a TCP Server connection it answers requests addressed to its Unit ID from an in-memory data store, so you can test a Modbus TCP client (SCADA, a gateway, your own code) without the real device.

The device's data store is set by Initial State, a JSON object with coils, discrete_inputs, holding_registers and input_registers (address → value). Writes from the client change the store, so the client reads back what it wrote. An optional effects list makes values move on their own: sine, cosine, triangle, sawtooth, square, ramp, random_walk, step_sequence, noise, gaussian_noise, offset, scale and clamp for registers; toggle, pulse, invert and random_bool for coils and discrete inputs. Each effect names a target and an address (or address_range).

cycbox/examples/modbus_tcp_loopback_demo.lua runs this codec on 127.0.0.1:1502 with a Modbus TCP client polling it, which gives live data without any hardware.

A simulated Modbus TCP device — a TCP server on 127.0.0.1:1502 with the Modbus TCP Server codec and its data store.

Configuration reference​

Config IDmodbus_tcp_server_codec
Selected byapp_codec = "modbus_tcp_server_codec"
PlatformsLinux, Windows, Android
LicensePremium

Modbus TCP Server/Slave simulator with configurable data store

Simulates a Modbus TCP slave device, responds to client requests based on internal register/coil state

SettingKeyTypeDefaultNotes
Unit ID (Slave Address)modbus_tcp_server_codec_unit_idInteger1Required
Initial State (JSON)modbus_tcp_server_codec_initial_stateCodesee below

Setting details​

Unit ID (Slave Address)​

Device address 0-247

Initial State (JSON)​

JSON object with initial values for coils, discrete_inputs, holding_registers, input_registers. Keys are address numbers, values are booleans (coils/discrete) or integers (registers). An optional "effects" array overlays dynamic transforms at read time (sine, cosine, triangle, sawtooth, square, ramp, noise, gaussian_noise, step_sequence, random_walk, offset, scale, clamp for registers; toggle, pulse, invert, random_bool, force for coils/discrete inputs). Each effect item has a "type", "target", and either "address" or "address_range": [lo, hi]. Effects are applied in array order, so listing sine then noise yields a sine wave with random noise on top.

Default:

{
"coils": {
"0": true,
"1": false,
"2": true,
"3": false
},
"discrete_inputs": {
"0": true,
"1": false,
"2": false,
"3": true
},
"holding_registers": {
"0": 100,
"1": 200,
"2": 300,
"3": 400
},
"input_registers": {
"0": 1000,
"1": 2000,
"2": 3000,
"3": 4000
},
"effects": [
{ "type": "sine", "target": "input_registers", "address": 0,
"amplitude": 200, "period_ms": 5000 },
{ "type": "noise", "target": "input_registers", "address": 0, "amplitude": 10 },
{ "type": "toggle", "target": "discrete_inputs", "address": 0, "period_ms": 2000 }
]
}

Example config​

Default values as they appear in a saved configuration (for example the JSON block at the end of a cycbox/examples/*.lua file). Settings detected at runtime are omitted.

{
"app": {
"app_codec": "modbus_tcp_server_codec"
},
"modbus_tcp_server_codec": {
"modbus_tcp_server_codec_unit_id": 1,
"modbus_tcp_server_codec_initial_state": "{\n \"coils\": {\n \"0\": true,\n \"1\": false,\n \"2\": true,\n \"3\": false\n },\n \"discrete_inputs\": {\n \"0\": true,\n \"1\": false,\n \"2\": false,\n \"3\": true\n },\n \"holding_registers\": {\n \"0\": 100,\n \"1\": 200,\n \"2\": 300,\n \"3\": 400\n },\n \"input_registers\": {\n \"0\": 1000,\n \"1\": 2000,\n \"2\": 3000,\n \"3\": 4000\n },\n \"effects\": [\n { \"type\": \"sine\", \"target\": \"input_registers\", \"address\": 0,\n \"amplitude\": 200, \"period_ms\": 5000 },\n { \"type\": \"noise\", \"target\": \"input_registers\", \"address\": 0, \"amplitude\": 10 },\n { \"type\": \"toggle\", \"target\": \"discrete_inputs\", \"address\": 0, \"period_ms\": 2000 }\n ]\n}"
}
}