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
- Check your company's rules on external AI first.
- Do not put in-house code, customer information, traffic logs or equipment information into an external AI. Give the AI only the device map (JSON), which is taken from manufacturers' public documents.
- If you cannot use an external AI, step 3 still works as is for code written by a person. With the bind address at 127.0.0.1, Modbus Slave accepts connections only from this PC.
- With an AI approved by your company or a local model, you can follow the same order.
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 code | Actual result | Correct |
|---|---|---|
| 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 error | 201 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] | 3795845121 | In 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
| Step | Do this |
|---|---|
| 1 | Open PortDock Modbus Slave. |
| 2 | Connection «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». |
| 5 | With «Addresses» set to «0-based», enter 2 (the forward bit) in cell 8 and press Enter → cell 200 rises to 3000. |
| 6 | To see word order, enter 123456 in cell 276 and press Enter. |
| 7 | Run 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).
| Cell | On screen | Code output |
|---|---|---|
| 200 | 3000 | 30.00 Hz |
| 201 | 250 | 2.50 A |
| 202 | 1100 | 110.0 V |
| 276 | 123456 | 123456 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 hardware | Not checked |
|---|---|
| Address, count, unit ID | Errors in the map itself, firmware and model differences |
| Scale and word order calculation | Real ramps and control response |
| Exception handling — writing to read-only cell 200 gives exception 02 and the value stays | Run status bits and alarms (not emulated) |
| Handling a failed connection | RS-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.
| Step | Do this |
|---|---|
| 1 | Check the map's addresses and scales against that device's manual. The map is unofficial. |
| 2 | Run only the read code and compare the values with the device's display. |
| 3 | For 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. |
| 4 | Read back after writing. |
- On the FR-E800, 40009 (address 8) means different things for reads and writes. Writing bit 0 means stop; reading it means RUN (running). The simulator returns what you wrote, so this difference does not show.
- This map is for Modbus RTU over the PU connector (RS-485), with a factory default of 19200 8E1. TCP is only for this test, so on a real device change the connection line as below. Set Pr.549 = 1 and Pr.117 = 1–247 first (the default 0 gets no reply).
.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
- FR-E800 Instruction Manual (Communication) IB(NA)-0600871ENG-S — 3.5 MODBUS RTU pp.257-267: 40001 addressing, 40009 bits (p.265), Pr.549 and Pr.117
- FR-E800 Instruction Manual (Function) IB(NA)-0600868ENG-U — Pr.52 monitor item list pp.395-399: 40201-40203, 40277 (CDAB)
- PyModbus — Client — ModbusTcpClient, ModbusSerialClient, device_id
Published 2026-11-05