Add Modbus TCP/IP to Your Simulation
import os
import yaml
import spx_python
# 1) Connect to the running SPX Server
client = spx_python.init(
address=os.environ.get("SPX_BASE_URL", "http://localhost:8000"),
product_key=os.environ["SPX_PRODUCT_KEY"],
)
# 2) Define the model (PT100-like sensor) + Modbus TCP mapping
pt_100_yaml = '''
attributes:
temperature: 25.0
sensor_fault: 0
actions:
- { ramp: $in(temperature), stop_value: 150, duration: 5, type: overshoot, overshoot: 5}
- { noise: $out(temperature), std: 0.01, mode: proportional}
communication:
- modbus_slave:
host: 0.0.0.0
port: 502
unit_id: 1
mapping:
temperature: { address: [0,1], group: h_r, type: float }
sensor_fault: { address: 4, group: c_o, type: bool }
'''
# 3) Register the model and create an instance
model_def = yaml.safe_load(pt_100_yaml)
client["models"]["pt_100_modbus"] = model_def
client["instances"]["pt100_mb_1"] = "pt_100_modbus"
inst = client["instances"]["pt100_mb_1"]
client.prepare()
modbus = inst["communication"]["modbus_slave"]
modbus.start()
# 4) Step deterministically (so external tools see changing readings)
for k in range(1, 51): # ~5 seconds with dt=0.1
inst["timer"]["time"] = k * 0.1
client.run()
print("internal temperature:", inst["attributes"]["temperature"].internal_value)
print("external temperature:", inst["attributes"]["temperature"].external_value)
print("sensor_fault:", inst["attributes"]["sensor_fault"].internal_value)
# A Modbus TCP client can now read temperature at holding registers 0–1 and sensor_fault at coil 4.Quick Verification with a Modbus TCP Client
Last updated

