PortDock

Write Modbus code with AI and test it without hardware — device map, pymodbus, simulator

Give AI a device map so it does not invent Modbus addresses, then run the pymodbus code against a simulator. See real address and word order mistakes.

Tools for this guideModbus Slave Windows program · freeModbus Master Windows program · free

AI can write a plausible register address that does not exist. So give it a device map with sources, then run the code without hardware and compare the values. The output below is a real run against the PortDock Modbus Slave 0.11.0 portable edition (127.0.0.1) with Python 3.14 and pymodbus 3.16.2. The example device is a Mitsubishi Electric FR-E800 inverter.

First, your company's rules

1. Put the device map in the prompt

Full map: portdock.dev/devices/mitsubishi-electric/fr-e800.json — unofficial data taken from the manufacturer's manuals, with the source page on every register. You can paste only the registers you need.

Write Python (pymodbus 3.x) read code using only the device map below.

Rules:
- address is already 0-based. Do not recalculate it from manualAddress.
- Do not use addresses that are not in the map. If a register is missing, answer «not in the map».
- Real value = raw value × scale.
- Combine u32 values by order. In CDAB the first word is the low word.
- Reads only (FC 03). No write code.
- Connection: Modbus TCP 127.0.0.1:1502, unit 1.

Device map (Mitsubishi Electric FR-E800):
[
  {"table":"holding-registers","address":200,"manualAddress":"40201","type":"u16","name":{"en":"Output frequency"},"unit":"Hz","scale":0.01,"access":"r","fc":[3]},
  {"table":"holding-registers","address":201,"manualAddress":"40202","type":"u16","name":{"en":"Output current"},"unit":"A","scale":0.01,"access":"r","fc":[3]},
  {"table":"holding-registers","address":202,"manualAddress":"40203","type":"u16","name":{"en":"Output voltage"},"unit":"V","scale":0.1,"access":"r","fc":[3]},
  {"table":"holding-registers","address":276,"manualAddress":"40277","type":"u32","order":"CDAB","name":{"en":"32-bit cumulative energy (kWh)"},"unit":"kWh","access":"r","fc":[3]}
]

With PortDock MCP added, you can have the AI fetch the same map with the device_map_get tool.

2. Review the code — read_e800.py

AI answers differ every time. This is an example written to the rules above.

from pymodbus.client import ModbusTcpClient

# Modbus TCP — connects to the Modbus Slave from step 3
client = ModbusTcpClient("127.0.0.1", port=1502, timeout=2)
if not client.connect():
    raise SystemExit("connect failed")
try:
    # 200=output frequency (×0.01 Hz), 201=output current (×0.01 A), 202=output voltage (×0.1 V)
    r = client.read_holding_registers(200, count=3, device_id=1)
    if r.isError():
        raise SystemExit(f"read error: {r}")
    print("raw 200-202:", r.registers)
    print(f"output frequency: {r.registers[0] * 0.01:.2f} Hz")
    print(f"output current:   {r.registers[1] * 0.01:.2f} A")
    print(f"output voltage:   {r.registers[2] * 0.1:.1f} V")

    # 276=cumulative energy (u32, CDAB — first word is the low word)
    r = client.read_holding_registers(276, count=2, device_id=1)
    if r.isError():
        raise SystemExit(f"read error: {r}")
    print("raw 276-277:", r.registers)
    print("energy:", (r.registers[1] << 16) | r.registers[0], "kWh")
finally:
    client.close()

Where it goes wrong. The results are from deliberately running the wrong code against the Modbus Slave set up in step 3.

Wrong codeActual resultCorrect
read_holding_registers(40200, count=1, device_id=1)ExceptionResponse(dev_id=1, function_code=131, exception_code=2)A 40201-style number is not a wire address. 40201 − 40001 = 200
read_holding_registers(201, count=1, device_id=1)[250] — no error201 is output current. Read as frequency it looks like 2.50 Hz. With no error, this is the hardest one to spot
r.registers[0]3000 — «3000 Hz»Multiply by the 0.01 scale: 30.00 Hz
(r.registers[0] << 16) | r.registers[1]3795845121In CDAB the first word is the low word — the right value is 123456
read_holding_registers(200, count=1, slave=1)TypeError: ... unexpected keyword argument 'slave'pymodbus 3.16.2 uses device_id. The argument name varies by version, so an old name can appear

3. Run it without hardware and compare

StepDo this
1Open PortDock Modbus Slave.
2Connection «TCP server (Modbus TCP)», bind address 127.0.0.1, port 1502.
3«Emulate device…» → search FR-E800 → select it and «Load» (leave the options at their defaults).
4«Start» → check the status shows «Listening».
5With «Addresses» set to «0-based», enter 2 (the forward bit) in cell 8 and press Enter → cell 200 rises to 3000.
6To see word order, enter 123456 in cell 276 and press Enter.
7Run the code with the commands below.
python -m venv .venv
.venv\Scripts\python -m pip install pymodbus==3.16.2
.venv\Scripts\python read_e800.py
raw 200-202: [3000, 250, 1100]
output frequency: 30.00 Hz
output current:   2.50 A
output voltage:   110.0 V
raw 276-277: [57920, 1]
energy: 123456 kWh

Compare with the screen. In the traffic log you can also check that the request's start address is 00 C8 (200).

CellOn screenCode output
200300030.00 Hz
2012502.50 A
2021100110.0 V
276123456123456 kWh

These are example values from the simulator, not a real drive's current or voltage. The simulator is also built from the same map, so a pass only means «the code matches the map».

Checked without hardwareNot checked
Address, count, unit IDErrors in the map itself, firmware and model differences
Scale and word order calculationReal ramps and control response
Exception handling — writing to read-only cell 200 gives exception 02 and the value staysRun status bits and alarms (not emulated)
Handling a failed connectionRS-485 wiring and timing

4. Before moving to a real device

Do not run AI code for writes (run commands) as is. The motor will move. Start with reads.

StepDo this
1Check the map's addresses and scales against that device's manual. The map is unofficial.
2Run only the read code and compare the values with the device's display.
3For writes, match each address and value against the manual yourself, line by line. Do not write to running equipment, and first make sure no one is near the machine and you can stop it.
4Read back after writing.
.venv\Scripts\python -m pip install pyserial

from pymodbus.client import ModbusSerialClient
client = ModbusSerialClient("COM3", baudrate=19200, bytesize=8, parity="E", stopbits=1, timeout=2)

This connection line was only checked as far as creating the object; it was not connected to a real FR-E800.

Sources

Published 2026-11-05