2

我正在尝试使用以下代码在 Mitsubishi PLC (Q06udeh) 上进行交流和阅读文字:

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('10.1.1.4',port=5007)
client.connect()

result = client.read_holding_registers(1,1)
print result
t = result.registers[0]
print t

我得到以下输出:

None

Traceback (most recent call last):
  File "C:\Users\serkan\Desktop\sasda.py", line 8, in <module>
    t = result.registers[0]
 AttributeError: 'NoneType' object has no attribute 'registers'

无论我如何更改具有不同参数的访问词都没有希望,我仍然没有成功。

请帮忙。

4

1 回答 1

1

使用 modbus_tk

首先,您需要安装 modbus_tk 包:

pip install modbus_tk

然后,尝试运行此示例代码:

#  General MODBUS includes
import modbus_tk
import modbus_tk.defines as cst
# TCP MODBUS includes
from modbus_tk import modbus_tcp

def main():
    device_id = 1
    master = modbus_tcp.TcpMaster("10.1.1.4")
    master.set_timeout(3)     #it's really optional
    master.set_verbose(True)  # this is optional too
    data = master.execute(device_id, cst.READ_HOLDING_REGISTERS, 100, 1) #Usage: 100 is registry number and 1 means that size of data is 16bit (modbus protocol specification)
    print "data    ", data
    print "data[0] ", data[0]

if __name__ == "__main__":
    main()

您可以在此处找到更多示例: https ://github.com/ljean/modbus-tk/tree/master/examples

于 2017-04-11T12:19:28.987 回答