从“IllegalAddress”读取后异步pymodbus应用程序不起作用

时间:2018-02-09 11:14:18

标签: python asynchronous tcp modbus

应用程序:Python 2.7 + Pymodbus 1.4.0(异步客户端)

问题:我想处理用户想要在异步版本中读取错误地址(非法地址)的情况,但我的解决方案不起作用。

简化代码 - 异步版本:

#!/usr/bin/env python

from __future__ import print_function

from twisted.internet import reactor, protocol
from pymodbus.client.async import ModbusClientProtocol
from twisted.internet import serialport, reactor


def print_result(response, address):
    try:
        print("Response: ", response.registers[0])
    except:
        print("Response: ", response)


def start_measurements(client):
    # 16408, 16409 addresses are ok
    # 9015 is an Illegal Address
    addresses = [16408, 9015, 16409]

    for address in addresses:
        rr = client.read_holding_registers(address, 1, unit=255)
        # setting Callback/Errback function with an address argument
        rr.addBoth(print_result, address)

    reactor.callLater(10, client.transport.loseConnection)
    reactor.callLater(11, reactor.stop)


def handler_tcp_connection_error(client):
    print("Error while connecting")
    reactor.callLater(0, reactor.stop)


if __name__ == "__main__":
    defer = protocol.ClientCreator(reactor, ModbusClientProtocol).connectTCP("192.168.168.100", 502)
    defer.addCallbacks(start_measurements, handler_tcp_connection_error)

    reactor.run()

当我从同步版本的这些寄存器中读取时,我只是没有从错误的地址接收到值,但无论如何都会读取其他寄存器。

在异步版本(上面粘贴)中,当读取非法地址时,即使是来自其他寄存器也没有响应,输出是:

Response: 15
Response:  Exception Response(131, 3, IllegalAddress)
*waiting about 10s with no response*
Response:  [Failure instance: Traceback (failure with no frames): <class 'pymodbus.exceptions.ConnectionException'>: Modbus Error: [Connection] Connection lost during request]

我希望得到所有正确的值(不是来自非法地址),就像在同步版本中一样:

Response: 15
Exception Response(131, 3, IllegalAddress)
Response 7

如何解决此问题,跳过IllegalAddreses并从所有正确的地址获取值?

0 个答案:

没有答案