Reputation: 1
I want to make a script so that my controller acts as a Modbus RTU slave and reacts to request sent by another device.
The controller stores 10 booleans as 10 discrete inputs from address 1 to 10. They can only be read (fonction code 2).
As I understood, pymodbus can be used for this. My question is: is it how I'm supposed to set up a Modbus RTU slave? But because I get errors, do you know what I am doing wrong?
Here is the script that I'm trying to execute:
from pymodbus.datastore import ModbusSequentialDataBlock
from serial import Serial
import time
import sys
import random
# Create a serial port
serial = Serial(port='COM1',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
timeout=0.5)
# Create a Modbus server context
store = ModbusSlaveContext(
di=ModbusSequentialDataBlock.create(),
co=ModbusSequentialDataBlock.create(),
hr=ModbusSequentialDataBlock.create(),
ir=ModbusSequentialDataBlock.create(),
zero_mode=True
)
# Create a loop to change the discrete input from adress 1 to 10 every 2 seconds
while True:
try:
# Change the discrete input randomly
for i in range(1, 11):
store.setValues(1, i, [random.randint(0, 1)])
time.sleep(2)
except KeyboardInterrupt:
server.stop()
sys.exit()
My controller has a Windows OS and can execute python. It has a RS-232 port (DB9).
My testing set-up is a computer with QModMaster on it to try and read my discrete inputs.
Is it how a Modbus RTU slave should be set up?
I'm not sure what I'm doing wrong... The error message I get from QModMaster is just a time out error.
Upvotes: 0
Views: 562