pymodbus:请求创建和响应接收 [英] pymodbus: request creation and response receiving

查看:451
本文介绍了pymodbus:请求创建和响应接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释如何使用pymodbus通过Modbus TCP/IP以正确的方式创建请求并获得响应吗?

Can anyone explain how to create the request and get the response in right way using pymodbus via Modbus TCP/IP?

我拥有要用作从站和PC-作为主站的PLC.

I have the PLC wich I want to use as slave and PC - as master.

我试图以这种方式做到这一点:

I trying to do it in such way:

from pymodbus.client.sync import ModbusTcpClient

host = '192.168.56.9'
port = 502   

client = ModbusTcpClient(host, port)
client.connect()

#Register address 0x102A (4138dec) with a word count of 1
#Value - MODBUS/TCP Connections
#Access - Read
#Description - Number of TCP connections

request = client.read_holding_registers(4138, 1) 
response = client.execute(request)

print response

>>> ReadRegisterResponse (1)

推荐答案

设置unit参数,并使用print(request.registers)代替print(request).

Set the unit argument and use the print(request.registers) instead of print(request).

这是一个例子:

request = client.read_holding_registers(4138, 1, unit=1)  # Notice: Set the unit argument.

if not request.isError():
    '''isError() method implemented in pymodbus 1.4.0 and above'''

    print(request.registers)  # Your problem is here.

else:
    # Do stuff to error handling.
    print('Error message: {}'.format(request))

这篇关于pymodbus:请求创建和响应接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆