3

我已经开始使用pymodbus从 modbus 读取值以存储在异地数据库中。我一直在努力解决一个问题,即响应中收到的值与我在 Jace 上看到的值不同。

我也试过modbus-tk了,我得到了同样的错误响应,所以它一定是我的 Python 代码中的某些东西导致了这个问题。从我们的遗留系统 (VB.Net) 检索到的读数与我在 Jace 上看到的输出相同。

这是从 modbus 检索数据的简单函数。我们有 2 个寄存器,4016040162一个正在读取366,这是正确的,第二个正在读取367(这是我遇到问题的那个)。我也看到了与其他寄存器相同的问题,即使我可以在 Jace 上看到值增加了,读数也不会更新。

# -*- coding: utf-8 -*-

from __future__ import division, print_function, unicode_literals

from pymodbus.client.sync import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder

def get_modbus_register_data(ip_address, register, device, count=2):
        """
        Retrieve modbus data.
        """
        client = ModbusTcpClient(ip_address, timeout=10)
        client.connect()

        # Read registers
        response = client.read_holding_registers(
            address=register,  # 40162
            count=count,       # 2
            unit=device)       # 4

        decoder = BinaryPayloadDecoder.fromRegisters(
            registers=response.registers,
            byteorder=Endian.Big,
            wordorder=Endian.Little)

        value = decoder.decode_32bit_float()

        client.close()

        return value # 366 and it should be 367

Pymodbus 调试日志

DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x4 0x3 0x0 0xa0 0x0 0x2
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0x7 0x4 0x3 0x4 0x0 0x0 0x43 0xb7
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0x7 0x4 0x3 0x4 0x0 0x0 0x43 0xb7
DEBUG:pymodbus.factory:Factory Response[ReadHoldingRegistersResponse: 3]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'

杰斯阅读

配置

更新

在 Sanju 的帮助下,有人向我指出我使用的偏移量可能不正确。情况确实如此,通过将偏移量更改 1 (40162 - 40001 = 161) 我能够从寄存器中检索正确的值,wordorder需要更改为Endian.Big.

更新代码

def get_modbus_register_data(ip_address, register, device, count=2):
        """
        Retrieve modbus data.
        """
        client = ModbusTcpClient(ip_address, timeout=10)
        client.connect()

        # Read registers
        response = client.read_holding_registers(
            address=register,  # 40161
            count=count,       # 2
            unit=device)       # 4

        decoder = BinaryPayloadDecoder.fromRegisters(
            registers=response.registers,
            byteorder=Endian.Big,
            wordorder=Endian.Big)

        value = decoder.decode_32bit_float()

        client.close()

        return value # 367
4

1 回答 1

1

使用 pymodbus,您将不得不注意 pymodbus 如何处理偏移量,偏移量 0 映射到寄存器 40001,因此 40162 的偏移量将是40162-40001is0xa1并且类似地40160偏移量将是0x9f。有关更多信息,请参阅https://pymodbus.readthedocs.io/en/latest/source/library/pymodbus.html#pymodbus.register_read_message.ReadHoldingRegistersRequest

另请注意,默认Endianness值为for和for 。如果这些不正确,您最终会得到错误的解码值。BinaryPayloadDecoderEndian.LittlebyteorderEndian.Bigwordorderorders

于 2018-08-23T04:00:26.547 回答