69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
|
|
import sys
|
|
import time
|
|
import _thread
|
|
import json
|
|
import gc
|
|
|
|
from script.umodbus.serial import ModbusRTU
|
|
|
|
uart_id = 4
|
|
|
|
slave_addr = 10 # address on bus as client
|
|
|
|
client = ModbusRTU(
|
|
addr=slave_addr, # address on bus
|
|
baudrate=115200, # optional, default 9600
|
|
# data_bits=8, # optional, default 8
|
|
# stop_bits=1, # optional, default 1
|
|
# parity=None, # optional, default None
|
|
# ctrl_pin=12, # optional, control DE/RE
|
|
uart_id=uart_id # optional, default 1, see port specific documentation
|
|
)
|
|
|
|
|
|
register_definitions = {
|
|
"COILS": {
|
|
"control": {
|
|
"register": 0,
|
|
"len": 10,
|
|
"val": [0,0,0,0,0,0,0,0,0,0],
|
|
}
|
|
},
|
|
"HREGS": {
|
|
"params": {
|
|
"register": 0,
|
|
"len": 10,
|
|
"val": [0,0,0,0,0,0,0,0,0,0],
|
|
}
|
|
},
|
|
"ISTS": {
|
|
"status": {
|
|
"register": 0,
|
|
"len": 10,
|
|
"val": [0,0,0,0,0,0,0,0,0,0]
|
|
}
|
|
},
|
|
"IREGS": {
|
|
"input": {
|
|
"register": 0,
|
|
"len": 10,
|
|
"val": [0,0,0,0,0,0,0,0,0,0]
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
# use the defined values of each register type provided by register_definitions
|
|
client.setup_registers(registers=register_definitions)
|
|
|
|
print(client)
|
|
|
|
while True:
|
|
try:
|
|
result = client.process()
|
|
except KeyboardInterrupt:
|
|
print('KeyboardInterrupt, stopping RTU client...')
|
|
break
|
|
except Exception as e:
|
|
print('Exception during execution: {}'.format(e)) |