我正在尝试使用以下代码与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'
无论我如何改变不同参数的访问单词都没有希望,我仍然没有成功。
请帮忙。
答案 0 :(得分:1)
首先,您需要安装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