Skip to content

Commit b749394

Browse files
author
Tom Softreck
committed
update
1 parent a2a7e8e commit b749394

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

modapi/api/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
modapi.api - API modules for Modbus communication
3+
"""
4+
5+
from .rest import create_rest_app
6+
from .mqtt import start_mqtt_broker
7+
from .cmd import execute_command
8+
from .shell import interactive_mode
9+
10+
__all__ = [
11+
'create_rest_app',
12+
'start_mqtt_broker',
13+
'execute_command',
14+
'interactive_mode'
15+
]

simulate_modbus.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Modbus RTU Server Simulator
4+
"""
5+
from pymodbus.server.sync import StartSerialServer
6+
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
7+
from pymodbus.datastore import ModbusSequentialDataBlock
8+
9+
def run_simulator(port: str = "/tmp/ptyp0", baudrate: int = 9600):
10+
"""Run a Modbus RTU server simulator"""
11+
# Initialize data stores
12+
store = ModbusSlaveContext(
13+
di=ModbusSequentialDataBlock(0, [0]*100), # Discrete Inputs
14+
co=ModbusSequentialDataBlock(0, [0]*100), # Coils
15+
hr=ModbusSequentialDataBlock(0, [0]*100), # Holding Registers
16+
ir=ModbusSequentialDataBlock(0, [0]*100) # Input Registers
17+
)
18+
19+
context = ModbusServerContext(slaves=store, single=True)
20+
21+
# Set some test values
22+
store.setValues(1, 0, [1, 0, 1, 0]) # Coils 0-3
23+
store.setValues(3, 0, [1234, 5678, 9012]) # Holding Registers 0-2
24+
25+
print(f"Starting Modbus RTU simulator on {port} at {baudrate} baud")
26+
print("Test values set:")
27+
print(" - Coils 0-3: [1, 0, 1, 0]")
28+
print(" - Holding Registers 0-2: [1234, 5678, 9012]")
29+
30+
# Start the server
31+
StartSerialServer(
32+
context,
33+
port=port,
34+
baudrate=baudrate,
35+
timeout=0.1
36+
)
37+
38+
if __name__ == "__main__":
39+
run_simulator()

0 commit comments

Comments
 (0)