【问题标题】:pymodbus: request creation and response receivingpymodbus:请求创建和响应接收
【发布时间】:2013-09-18 01:25:56
【问题描述】:

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

我有 PLC,我想用作从站和 PC - 作为主站。

我试图这样做:

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)

【问题讨论】:

    标签: python modbus modbus-tcp pymodbus


    【解决方案1】:

    设置unit 参数并使用print(request.registers) 而不是print(request)

    这是一个例子:

    request = client.read_holding_registers(4138, 1, unit=1)  # Set 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 for error handling.
        print('Error message: {}'.format(request))
    

    【讨论】:

    • 知道如何跟踪到holding_registers 的读取请求数吗?
    【解决方案2】:

    您可以执行dir(response) 来检查响应的组成,但如果 pymodbus TCP 主机类似于 RTU 串行主机实现,则数据在寄存器字段中可用,因此请尝试打印 response.Registers 而不是响应。 response.Registers 应该是一个包含您请求读取的寄存器值的单元素数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多