РЕДАКТИРОВАТЬ
Обновленный код и журналы
Проблема в том, что вы звоните 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...