我遇到了 python pymodbus3 模块的问题。
我有一个 modbus 设备和这两个脚本:
pymodbus_tester.py
#!/usr/bin/env python
from pymodbus.client.sync import ModbusSerialClient
if __name__ == '__main__':
mdbcli = ModbusSerialClient(
method = "rtu",
parity = "N",
stopbits = 1,
bytesize = 8,
timeout = 1,
port = "/dev/ttyUSB0",
baudrate = "115200"
)
if not mdbcli.connect():
print("Could not connect to Modbus")
print("""
********************************************************************
***** SINGLE READ TEST *****
********************************************************************
""")
ret = mdbcli.read_input_registers(
address = 0x3104,
count = 1,
unit = 0x01
)
print("Single read: {0}".format(ret))
pymodbus3_tester.py
#!/usr/bin/env python3
from pymodbus3.client.sync import ModbusSerialClient
if __name__ == '__main__':
mdbcli = ModbusSerialClient(
method = "rtu",
parity = "N",
stopbits = 1,
bytesize = 8,
timeout = 1,
port = "/dev/ttyUSB0",
baudrate = "115200"
)
if not mdbcli.connect():
print("Could not connect to Modbus")
print("""
********************************************************************
***** SINGLE READ TEST *****
********************************************************************
""")
ret = mdbcli.read_input_registers(
address = 0x3104,
count = 1,
unit = 0x01
)
print("Single read: {0}".format(ret))
如您所见,除了 pymodbus 模块之外,它们几乎相同。
然而,这是我在启动它们时得到的(为简单起见合并启动):
griccardo@pc:~$ python pymodbus_tester.py ; echo "---------------" ; python3 pymodbus3_tester.py
********************************************************************
***** SINGLE READ TEST *****
********************************************************************
Single read: ReadRegisterResponse (1)
---------------
********************************************************************
***** SINGLE READ TEST *****
********************************************************************
Single read: None
在设备端,我在两种情况下都收到相同的请求并输出相同的响应,正如您从以下日志中看到的(有点):
REQ: '1~�'
RES: '�0'
REQ: '1~�'
RES: '�0
pymodbus3 模块有问题吗?还是我用错了?
谢谢你,里卡多