Python 使用 ModbusTcpClient 库在三菱 PLC 上读取 modbus 通信字
Python modbus communcation reading words on Mitsubishi PLC using ModbusTcpClient library
我正在尝试使用以下代码在三菱 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'
不管我怎么改不同参数的访问词都没有希望,还是没有成功。
请帮忙。
使用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
我正在尝试使用以下代码在三菱 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'
不管我怎么改不同参数的访问词都没有希望,还是没有成功。
请帮忙。
使用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