我对pymodbus相当陌生,我正在尝试使用pymodbus读取协作机器人的保持寄存器,以获取当前z坐标的值。此类信息位于7053地址。我看了一些较旧的问题,但我的代码无法正常工作:
from pymodbus.client.sync import ModbusTcpClient
host = '192.168.250.201' # Robot IP address
port = 502 # port
client = ModbusTcpClient(host, port)
client.connect()
request = client.read_holding_registers(
address=0x03, # The starting address to read from
count=4, # The number of registers to read
unit=1) # The slave unit this request is targeting
response = client.execute(request)
print(response.bits[0])
client.close()
我一直收到此错误消息:
ConnectionException:Modbus错误:[Connection]无法连接[ModbusTcpClient(192.168.250.201:502)]
我想我的代码一定有问题,或者其他原因阻止我建立连接。有什么建议么?谢谢
您有几个小问题:
1)您正在查询的寄存器的地址似乎不正确,请仔细检查设备的手册以查看您是否正在读取正确的地址,很可能需要查询address=7053
。
2)您正在读取保持寄存器,但随后尝试以线圈(位)的形式打印该值。检查它们是否真的在保存寄存器,并使用print(response.registers[0])