Pymodbus: Modbus RTU с использованием реактора - обратный вызов никогда не вызывался - PullRequest
0 голосов
/ 30 января 2019

Я пытаюсь создать клиент Modbus serial / RTU, который будет считывать данные из последовательного порта с использованием библиотеки Pymodbus.

Python: 3.6Pymodbus: 2.1.0Платформа: Linux / Windows

Мой пример кода приведен ниже:

def readDevices(modbusRTUDevice):
    deviceIP = modbusRTUDevice["ip"]
    devicePort = modbusRTUDevice["port"]
    logger.info("Connecting to Modbus RTU device at address {0}".format(deviceIP + ":" + str(devicePort)))
    modbusClientFactory = CustomModbusClientFactory()
    modbusClientFactory.address = deviceIP
    modbusClientFactory.modbusDevice = modbusRTUDevice
    SerialModbusClient(modbusClientFactory, devicePort, reactor)
    Thread(target=reactor.run, args=(False,)).start() 

class SerialModbusClient(serialport.SerialPort):
    def __init__(self, factory, *args, **kwargs):
        serialport.SerialPort.__init__(self, factory.buildProtocol(), *args, **kwargs)

class CustomModbusClientFactory(protocol.ClientFactory, ModbusClientMixin):
    modbusDevice = {} 

    def buildProtocol(self, addr=None):
        modbusClientProtocol = CustomModbusClientProtocol()
        modbusClientProtocol.factory = self
        modbusClientProtocol.modbusDevice = self.modbusDevice
        return modbusClientProtocol

class CustomModbusClientProtocol(ModbusClientProtocol):
    def connectionMade(self):
        framer = ModbusRtuFramer.__init__(self, ClientDecoder(), client=None)
        ModbusClientProtocol.__init__(self, framer, baudrate=9600, parity='E', bytesize=8, stopbits=1, timeout=0.2, retryOnEmpty=True, retries=3)
        ModbusClientProtocol.connectionMade(self)
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        logger.info("Modbus RTU device connected at address {0}".format(deviceIP + ":" + str(devicePort)))
        self.read()

    def read(self):
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        slaveAddress = self.modbusDevice["slaveAddress"]
        logger.info("Reading holding registers of Modbus RTU device at address {0}...".format(deviceIP + ":" + str(devicePort)))
        deferred = self.read_holding_registers(self.startingAddress, self.registerCount, unit=slaveAddress)
        deferred.addCallbacks(self.requestFetched, self.requestNotFetched)

    def requestNotFetched(self, error):
        logger.info("Error reading registers of Modbus RTU device : {0}".format(error))
        sleep(0.5)

    def requestFetched(self, response):
        logger.info("Inside request fetched...")
        #Do some other stuff here
        reactor.callLater(0, self.read) 

После отладки self метода read () сообщает CustomModbusClientProtocol: Null Transport.После этого я нажимаю F8 и нить переходит в состояние блокировки и обратные вызовы отложенного никогда не вызывались.

Выход:

INFO:__main__: (2019-01-30 15:42:53; Test.py:200 Connecting to Modbus RTU device at address 127.0.0.1:/dev/ttyUSB0)
INFO:__main__: (2019-01-30 15:42:53; Test.py:70 Modbus RTU device connected at address 127.0.0.1:/dev/ttyUSB0)
INFO:__main__: (2019-01-30 15:46:18; Test.py:87 Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ttyUSB0...)

Согласно Последовательный+ Async + RTU: Обратный вызов никогда не запускается # 160 , проблема вызова обратного вызова отложенного исправлена.Но в моем случае это все еще сохраняется.

Я пробовал этот код в обоих, Raspberry PI & Windows, получая одинаковый ответ в обоих случаях.

Не могу понять, почему этоэто происходит.Нужно ли что-то добавлять в мой код или я сделал что-то не так при подключении к устройству Modbus через последовательный порт?

Любая помощь будет признательна.

Ответы [ 2 ]

0 голосов
/ 07 февраля 2019

Ниже решение работает для меня.Конфигурации моего устройства приведены ниже:

Конфигурация устройства: Скорость передачи = 9600, четность = 'E', размер байта = 8 Тайм-аут = 0,2Протокол: RTUПЛК подключен к RaspberryPI с помощью преобразователя последовательного интерфейса в USB.

Рабочий код:

import logging
from threading import Thread
from time import sleep

from pymodbus.client.async.twisted import ModbusClientProtocol
from pymodbus.constants import Endian
from pymodbus.factory import ClientDecoder
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.transaction import ModbusRtuFramer
from serial import EIGHTBITS
from serial import PARITY_EVEN
from serial import STOPBITS_ONE
from twisted.internet import protocol
from twisted.internet import serialport, reactor

FORMAT = ('%(asctime)-15s %(threadName)-15s '
      '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.INFO)


def readDevices(modbusRTUDevice):
    deviceIP = modbusRTUDevice["ip"]
    devicePort = modbusRTUDevice["port"]
    logger.info("Connecting to Modbus RTU device at address {0}".format(deviceIP +    ":" + str(devicePort)))
    modbusClientFactory = CustomModbusClientFactory()
    modbusClientFactory.address = deviceIP
    modbusClientFactory.modbusDevice = modbusRTUDevice
    SerialModbusClient(modbusClientFactory, devicePort, reactor, baudrate=9600, bytesize=EIGHTBITS,
                   parity=PARITY_EVEN, stopbits=STOPBITS_ONE, timeout=0.2, xonxoff=0, rtscts=0)
    Thread(target=reactor.run, args=(False,)).start()  # @UndefinedVariable


class SerialModbusClient(serialport.SerialPort):

    def __init__(self, factory, *args, **kwargs):
        serialport.SerialPort.__init__(self, factory.buildProtocol(), *args, **kwargs)


class CustomModbusClientFactory(protocol.ClientFactory):
    modbusDevice = {}

    def buildProtocol(self, addr=None):
        modbusClientProtocol = CustomModbusClientProtocol()
        modbusClientProtocol.factory = self
        modbusClientProtocol.modbusDevice = self.modbusDevice
        return modbusClientProtocol

    def clientConnectionLost(self, connector, reason):
        modbusTcpDeviceIP = self.modbusDevice["ip"]
        modbusTcpDevicePort = self.modbusDevice["port"]
        logger.critical("Connection lost with device running on {0}:{1}.".format(modbusTcpDeviceIP, modbusTcpDevicePort))
        logger.critical("Root Cause : {0}".format(reason))
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        modbusTcpDeviceIP = self.modbusDevice["ip"]
        modbusTcpDevicePort = self.modbusDevice["port"]
        logger.critical("Connection failed with device running on {0}:{1}.".format(modbusTcpDeviceIP, modbusTcpDevicePort))
        logger.critical("Root Cause : {0}".format(reason))
        connector.connect()


class CustomModbusClientProtocol(ModbusClientProtocol):

    def connectionMade(self):
        framer = ModbusRtuFramer(ClientDecoder(), client=None)
        ModbusClientProtocol.__init__(self, framer)
        ModbusClientProtocol.connectionMade(self)
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        logger.info("Modbus RTU device connected at address logger{0}".format(deviceIP + ":" + str(devicePort)))
        reactor.callLater(5, self.read)  # @UndefinedVariable

    def read(self):
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        slaveAddress = self.modbusDevice["slaveAddress"]
        logger.info("Reading holding registers of Modbus RTU device at address {0}...".format(deviceIP + ":" + str(devicePort)))
        deferred = self.read_holding_registers(0, 1, unit=slaveAddress)
        deferred.addCallbacks(self.requestFetched, self.requestNotFetched)

    def requestNotFetched(self, error):
        logger.info("Error reading registers of Modbus RTU device : {0}".format(error))
        sleep(0.5)

    def requestFetched(self, response):
        logger.info("Inside request fetched...")
        decoder = BinaryPayloadDecoder.fromRegisters(response.registers, byteorder=Endian.Big, wordorder=Endian.Big)
        skipBytesCount = 0
        decoder.skip_bytes(skipBytesCount)
        registerValue = decoder.decode_16bit_int()
        skipBytesCount += 2
        logger.info("Sensor updated to value '{0}'.".format(registerValue))
        reactor.callLater(5, self.read)  # @UndefinedVariable


readDevices({"ip": "127.0.0.1", "port": "/dev/ttyUSB0", "slaveAddress": 1})

Выход:

2019-02-03 00:39:00,623 MainThread      INFO     TestRTU:26       Connecting to Modbus RTU device at address 127.0.0.1:/dev/ttyUSB0
2019-02-03 00:39:00,628 MainThread      INFO     TestRTU:73       Modbus RTU device connected at address logger127.0.0.1:/dev/ttyUSB0
2019-02-03 00:39:05,634 Thread-1        INFO     TestRTU:80       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ttyUSB0...
2019-02-03 00:39:05,676 Thread-1        INFO     TestRTU:89       Inside request fetched...
2019-02-03 00:39:05,677 Thread-1        INFO     TestRTU:95       Sensor updated to value '26'.

serialport.SerialPort. init (...) Конструктор отвечает за инициализацию всех необходимых параметров для последовательной связи с устройством.Вам просто нужно обновить настройки устройства здесь и запустить код.SerialModbusClient(modbusClientFactory, devicePort, reactor, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_EVEN, stopbits=STOPBITS_ONE, timeout=0.2, xonxoff=0, rtscts=0)

0 голосов
/ 31 января 2019

РЕДАКТИРОВАТЬ

Обновленный код и журналы


Проблема в том, что вы звоните self.read() непосредственно в connectionMade класса CustomModbusClientProtocol, до тогореактор фактически запущен.Вам придется подождать, пока реактор запустится, и запланировать read на более позднее время с reactor.callLater(<time-to-wait-in-seconds>, self.read).Модифицированный код CustomModbusClientProtocol будет выглядеть примерно так:

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

from pymodbus.transaction import ModbusRtuFramer
from threading import Thread
from time import sleep
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
          '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


def readDevices(modbusRTUDevice):
    deviceIP = modbusRTUDevice["ip"]
    devicePort = modbusRTUDevice["port"]
    logger.info("Connecting to Modbus RTU device at address {0}".format(deviceIP + ":" + str(devicePort)))
    modbusClientFactory = CustomModbusClientFactory()
    modbusClientFactory.address = deviceIP
    modbusClientFactory.modbusDevice = modbusRTUDevice
    SerialModbusClient(modbusClientFactory, devicePort, reactor)
    Thread(target=reactor.run, args=(False,)).start()

class SerialModbusClient(serialport.SerialPort):
    def __init__(self, factory, *args, **kwargs):
        serialport.SerialPort.__init__(self, factory.buildProtocol(), *args, **kwargs)

class CustomModbusClientFactory(protocol.ClientFactory):
    modbusDevice = {}

    def buildProtocol(self, addr=None):
        modbusClientProtocol = CustomModbusClientProtocol()
        modbusClientProtocol.factory = self
        modbusClientProtocol.modbusDevice = self.modbusDevice
        return modbusClientProtocol

class CustomModbusClientProtocol(ModbusClientProtocol):
    def connectionMade(self):
        framer = ModbusRtuFramer(ClientDecoder(), client=None)
        ModbusClientProtocol.__init__(self, framer, baudrate=9600, parity='E', bytesize=8, stopbits=1, timeout=0.2, retryOnEmpty=True, retries=3)
        ModbusClientProtocol.connectionMade(self)
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        logger.info("Modbus RTU device connected at address logger{0}".format(deviceIP + ":" + str(devicePort)))
        reactor.callLater(5, self.read)

    def read(self):
        deviceIP = self.modbusDevice["ip"]
        devicePort = self.modbusDevice["port"]
        slaveAddress = self.modbusDevice["slaveAddress"]
        logger.info("Reading holding registers of Modbus RTU device at address {0}...".format(deviceIP + ":" + str(devicePort)))
        deferred = self.read_holding_registers(0, 10, unit=slaveAddress)
        deferred.addCallbacks(self.requestFetched, self.requestNotFetched)

    def requestNotFetched(self, error):
        logger.info("Error reading registers of Modbus RTU device : {0}".format(error))
        sleep(0.5)

    def requestFetched(self, response):
        logger.info("Inside request fetched...")
        #Do some other stuff here
        reactor.callLater(0, self.read)


readDevices({"ip": "127.0.0.1", "port": "/dev/ptyp0", "slaveAddress": 1})

Журналы:

$ python scratch_118.py
2019-01-31 12:34:56,734 MainThread      INFO     scratch_118    :21       Connecting to Modbus RTU device at address 127.0.0.1:/dev/ptyp0
2019-01-31 12:34:56,735 MainThread      DEBUG    __init__       :80       Client connected to modbus server
2019-01-31 12:34:56,735 MainThread      INFO     scratch_118    :48       Modbus RTU device connected at address logger127.0.0.1:/dev/ptyp0
2019-01-31 12:35:01,737 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
2019-01-31 12:35:01,738 Thread-1        DEBUG    __init__       :112      send: 0x1 0x3 0x0 0x0 0x0 0xa 0xc5 0xcd
2019-01-31 12:35:01,738 Thread-1        DEBUG    transaction    :418      Adding transaction 1
2019-01-31 12:35:02,196 Thread-1        DEBUG    rtu_framer     :175      Getting Frame - 0x3 0x14 0x0 0x2d 0x0 0x2c 0x0 0x2e 0x0 0x2d 0x0 0x2d 0x0 0x2e 0x0 0x29 0x0 0x2d 0x0 0x2d 0x0 0x2c
2019-01-31 12:35:02,197 Thread-1        DEBUG    factory        :246      Factory Response[ReadHoldingRegistersResponse: 3]
2019-01-31 12:35:02,197 Thread-1        DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2019-01-31 12:35:02,197 Thread-1        INFO     scratch_118    :64       Inside request fetched...
2019-01-31 12:35:02,197 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
2019-01-31 12:35:02,198 Thread-1        DEBUG    __init__       :112      send: 0x1 0x3 0x0 0x0 0x0 0xa 0xc5 0xcd
2019-01-31 12:35:02,198 Thread-1        DEBUG    transaction    :418      Adding transaction 2
2019-01-31 12:35:03,202 Thread-1        DEBUG    rtu_framer     :175      Getting Frame - 0x3 0x14 0x0 0x2d 0x0 0x2c 0x0 0x2e 0x0 0x2d 0x0 0x2d 0x0 0x2e 0x0 0x29 0x0 0x2d 0x0 0x2d 0x0 0x2c
2019-01-31 12:35:03,202 Thread-1        DEBUG    factory        :246      Factory Response[ReadHoldingRegistersResponse: 3]
2019-01-31 12:35:03,202 Thread-1        DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2019-01-31 12:35:03,202 Thread-1        INFO     scratch_118    :64       Inside request fetched...
2019-01-31 12:35:03,203 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
2019-01-31 12:35:03,203 Thread-1        DEBUG    __init__       :112      send: 0x1 0x3 0x0 0x0 0x0 0xa 0xc5 0xcd
2019-01-31 12:35:03,203 Thread-1        DEBUG    transaction    :418      Adding transaction 3
2019-01-31 12:35:04,207 Thread-1        DEBUG    rtu_framer     :175      Getting Frame - 0x3 0x14 0x0 0x2d 0x0 0x2c 0x0 0x2e 0x0 0x2d 0x0 0x2d 0x0 0x2e 0x0 0x29 0x0 0x2d 0x0 0x2d 0x0 0x2c
2019-01-31 12:35:04,207 Thread-1        DEBUG    factory        :246      Factory Response[ReadHoldingRegistersResponse: 3]
2019-01-31 12:35:04,208 Thread-1        DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2019-01-31 12:35:04,208 Thread-1        INFO     scratch_118    :64       Inside request fetched...
2019-01-31 12:35:04,208 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
2019-01-31 12:35:04,208 Thread-1        DEBUG    __init__       :112      send: 0x1 0x3 0x0 0x0 0x0 0xa 0xc5 0xcd
2019-01-31 12:35:04,209 Thread-1        DEBUG    transaction    :418      Adding transaction 4
2019-01-31 12:35:05,213 Thread-1        DEBUG    rtu_framer     :175      Getting Frame - 0x3 0x14 0x0 0x2d 0x0 0x2c 0x0 0x2e 0x0 0x2d 0x0 0x2d 0x0 0x2e 0x0 0x29 0x0 0x2d 0x0 0x2d 0x0 0x2c
2019-01-31 12:35:05,213 Thread-1        DEBUG    factory        :246      Factory Response[ReadHoldingRegistersResponse: 3]
2019-01-31 12:35:05,214 Thread-1        DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2019-01-31 12:35:05,214 Thread-1        INFO     scratch_118    :64       Inside request fetched...
2019-01-31 12:35:05,214 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
2019-01-31 12:35:05,214 Thread-1        DEBUG    __init__       :112      send: 0x1 0x3 0x0 0x0 0x0 0xa 0xc5 0xcd
2019-01-31 12:35:05,215 Thread-1        DEBUG    transaction    :418      Adding transaction 5
2019-01-31 12:35:06,218 Thread-1        DEBUG    rtu_framer     :175      Getting Frame - 0x3 0x14 0x0 0x2d 0x0 0x2c 0x0 0x2e 0x0 0x2d 0x0 0x2d 0x0 0x2e 0x0 0x29 0x0 0x2d 0x0 0x2d 0x0 0x2c
2019-01-31 12:35:06,218 Thread-1        DEBUG    factory        :246      Factory Response[ReadHoldingRegistersResponse: 3]
2019-01-31 12:35:06,219 Thread-1        DEBUG    rtu_framer     :110      Frame advanced, resetting header!!
2019-01-31 12:35:06,219 Thread-1        INFO     scratch_118    :64       Inside request fetched...
2019-01-31 12:35:06,219 Thread-1        INFO     scratch_118    :55       Reading holding registers of Modbus RTU device at address 127.0.0.1:/dev/ptyp0...
...