CircuitPython Modbus TCP
pyrung.circuitpy can generate a Modbus TCP server, client, or both for the P1AM-200 via the P1AM-ETH shield. The register layout matches a real CLICK PLC. C-more HMIs, pyclickplc, and SCADA systems connect without translation. See CircuitPython Dialect for the base hardware model and code generation.
Hardware requirements
The P1AM-ETH shield provides a W5500 Ethernet controller on SPI with chip-select on board.D5. The generated code uses static IPv4 only. It does not use DHCP. The adafruit_wiznet5k library must be installed on the CIRCUITPY drive alongside the CircuitPython P1AM library.
Server
from pyrung import Bool, Int, Program, rung, out
from pyrung.circuitpy import ModbusServerConfig, P1AM, generate_circuitpy
from pyrung.click import ClickBlocks, TagMap
x, y, c, t, ct, sc, ds, dd, dh, df, xd, yd, xd0u, yd0u, td, ctd, sd, txt = ClickBlocks()
# Hardware
hw = P1AM()
inputs = hw.slot(1, "P1-08SIM")
outputs = hw.slot(2, "P1-08TRS")
Button = inputs[1]
Light = outputs[1]
Setpoint = Int()
# Logic
with Program() as logic:
with rung(Button):
out(Light)
# Map to CLICK addresses for Modbus visibility
mapping = TagMap({
Setpoint: ds[1],
Light: c[1],
})
# Generate with Modbus server
source = generate_circuitpy(
logic, hw,
target_scan_ms=10.0,
watchdog_ms=500,
modbus_server=ModbusServerConfig(ip="192.168.1.200"),
tag_map=mapping,
)
The generated code starts a Modbus TCP listener on the configured IP and port. Any Modbus client reading DS1 gets the current value of Setpoint. Writing DS1 updates it. Reading coil C1 returns the state of Light. The register layout is identical to a real CLICK PLC, with the same Modbus addresses and the same data encoding.
| Field | Type | Default | Notes |
|---|---|---|---|
ip |
str |
— | Static IPv4 for the P1AM-ETH shield |
subnet |
str |
"255.255.255.0" |
|
gateway |
str |
"192.168.1.1" |
|
dns |
str |
"0.0.0.0" |
|
port |
int |
502 |
1–65535 |
max_clients |
int |
2 |
1–7 concurrent connections (W5500 has 8 sockets, 1 reserved for listener) |
Supported function codes:
- FC 1 (read coils)
- FC 2 (read discrete inputs)
- FC 3 (read holding registers)
- FC 4 (read input registers)
- FC 5 (write single coil)
- FC 6 (write single register)
- FC 15 (write multiple coils)
- FC 16 (write multiple registers)
Client — send and receive
from pyrung import Bool, Int, IntBlock, Program, rung
from pyrung.circuitpy import ModbusClientConfig, P1AM, generate_circuitpy
from pyrung.click import ModbusTcpTarget, TagMap, send, receive
hw = P1AM()
hw.slot(1, "P1-08SIM")
Enable = Bool()
LocalSetpoint = Int()
RemoteWords = IntBlock(1, 4)
CommSending = Bool()
CommReceiving = Bool()
CommSuccess = Bool()
CommError = Bool()
CommEx = Int()
with Program() as logic:
with rung(Enable):
send(
target="plc1",
remote_start="DS1",
source=LocalSetpoint,
sending=CommSending,
success=CommSuccess,
error=CommError,
exception_response=CommEx,
)
with rung(Enable):
receive(
target="plc1",
remote_start="DS100",
dest=RemoteWords.select(1, 4),
receiving=CommReceiving,
success=CommSuccess,
error=CommError,
exception_response=CommEx,
)
source = generate_circuitpy(
logic, hw,
target_scan_ms=10.0,
modbus_client=ModbusClientConfig(
targets=(ModbusTcpTarget(name="plc1", ip="192.168.1.20"),)
),
tag_map=TagMap(),
)
send writes local tag values to a remote CLICK address. receive reads remote CLICK addresses into local tags. The target string must match a ModbusTcpTarget.name. Remote addresses use CLICK address format (DS1, C1, X001, etc.).
Raw Modbus addresses
When the remote device isn't a CLICK PLC, use ModbusAddress instead of a CLICK address string. This gives direct control over the register address and register type.
from pyrung import ModbusAddress, RegisterType
vfd = ModbusTcpTarget(name="vfd", ip="192.168.1.30")
with Program() as logic:
# Read a 32-bit speed value from holding registers 0x200–0x201, word-swapped
with rung(Enable):
receive(
target="vfd",
remote_start=ModbusAddress(0x200, RegisterType.HOLDING),
dest=Speed,
receiving=CommReceiving,
success=CommSuccess,
error=CommError,
exception_response=CommEx,
word_swap=True,
)
# Write a setpoint to a single holding register at 0x100
with rung(Enable):
send(
target="vfd",
remote_start=ModbusAddress(0x100),
source=Setpoint,
sending=CommSending,
success=CommSuccess,
error=CommError,
exception_response=CommEx,
)
ModbusAddress accepts MODBUS 984 addresses (e.g. 400001 for holding, 300001 for input) or raw register offsets (0–0xFFFE). Hex strings with an h suffix (e.g. "0h") are also supported. These require an explicit register_type, because the offset alone is ambiguous.
| Field | Type | Default | Notes |
|---|---|---|---|
address |
int or str |
— | 984-style int (400001), raw int (0–0xFFFE), or hex str ("0h") |
register_type |
RegisterType |
HOLDING |
Inferred for 984 addresses; required for hex |
The codegen maps register_type to the correct Modbus function code:
| Type | Send | Receive |
|---|---|---|
HOLDING |
FC 6 (single) / FC 16 (multiple) | FC 3 |
INPUT |
— | FC 4 |
COIL |
FC 5 (single) / FC 15 (multiple) | FC 1 |
DISCRETE_INPUT |
— | FC 2 |
word_swap on send()/receive() controls how 32-bit values (DINT, REAL) are split across register pairs. False (the default) puts the high word first (big-endian, common in VFDs and power meters). True puts the low word first.
CircuitPython code generation supports Modbus TCP only, not RTU (serial).
| Field | Type | Default | Notes |
|---|---|---|---|
name |
str |
— | Unique identifier, referenced by target= in send/receive |
ip |
str |
— | Remote PLC address |
port |
int |
502 |
|
device_id |
int |
1 |
Modbus unit ID (0–255) |
timeout_ms |
int |
1000 |
Per-transaction timeout |
The P1AM supports neither a DS tag as device_id nor com_port="wlan". ModbusClientConfig rejects both.
Unlike the CLICK dialect's threaded send/receive, the CircuitPython versions generate a non-blocking state machine. Each transaction advances one step per scan (connect, send request, wait for response, apply result). The scan loop is never blocked. Status tags (sending/receiving, success, error, exception_response) update as the transaction progresses. When the rung condition goes false, status tags reset to defaults.
Which registers the server serves
tag_map is required when modbus_server or modbus_client is set. The TagMap maps semantic tags to CLICK hardware addresses, and the codegen uses those addresses as Modbus register addresses.
The generated server serves:
- every register the TagMap maps, whether a rung uses it or not, so an HMI or SCADA system can write a value no ladder rung touches
- every raw CLICK register a rung uses directly (
ds[3901],c[1801]), at its own address - the whole bank, when a rung reaches it by pointer or block instruction (
ds[ptr],fill(0, c.select(1, 3))) - every X and Y coil, and the coils between X or Y slots. The server keeps a write to one that no tag holds, so a read returns it.
- XD and YD in CLICK's layout, built from the X and Y bits. A write to YD sets those Y bits.
A read of any other address in a CLICK bank returns 0, as on a CLICK. A write there gets Modbus exception 2 (illegal data address), because the P1AM has nowhere to store the value. A CLICK would store it. To keep such a value on the P1AM, map it in the TagMap. An unused T or CT bit is one of these addresses. A T or CT bit is unused if no rung uses it and the TagMap doesn't map it.
A read of the register after XD8 or after YD8 returns 0, as on a CLICK.
A write to an SC or SD that CLICK's Data View shows as Read Only gets exception 2. If an FC 15 or FC 16 write reaches one rejected address, it stores nothing.
The server sends every NaN in a Real as 0xFFFF:0xFFFF, the NaN that a CLICK's Math stores. An Unpack Copy, a Copy Binary into a Dint and send() give the same words. A CLICK keeps the bits of a NaN that a client writes, so a NaN such as 0x0000:0x7FC0 reads back differently from the P1AM. CircuitPython's floats are 30 bits wide, so they can't hold a NaN's bits.
A client that writes 2.5 to a Real holding a NaN one register at a time, low word first, reads back 2.50002 (0xFFFF:0x4020). The low word it wrote still makes a NaN, so the server sends 0xFFFF in its place. Write both words of a Real in one FC 16 request.
Scan cycle with Modbus
- Read physical inputs
- Execute rungs
- Write physical outputs
- Service Modbus server
- Service Modbus client
- Edge snapshots, watchdog pet, scan sleep
The server and client service calls run unconditionally, including in STOP mode. This matches CLICK behavior. An HMI can still read tag state and see sys.mode_run as False while the PLC is stopped.
Both server and client
The P1AM-200 can be both server and client simultaneously. A single Ethernet setup is shared.