pymodbus3无法从Modbus设备读取

时间:2017-06-21 09:52:23

标签: python modbus pymodbus3

我遇到了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模块有什么问题吗?或者我使用它错了吗?

谢谢你, 的Riccardo

0 个答案:

没有答案