From 93b33333569b58b426cb06001379d256f5ddb4fe Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 8 Jul 2019 15:28:46 +0200 Subject: [PATCH] add actual modbus communication --- snippets/test8.py | 12 ------------ src/CommunicationProcessor.py | 28 ++++++++++++++++++++++++---- src/Config.py | 2 ++ src/RegisterDatapoint.py | 23 ++++++++++++++++++++--- 4 files changed, 46 insertions(+), 19 deletions(-) delete mode 100644 snippets/test8.py diff --git a/snippets/test8.py b/snippets/test8.py deleted file mode 100644 index 4175200..0000000 --- a/snippets/test8.py +++ /dev/null @@ -1,12 +0,0 @@ -import queue -import datetime -import threading -import socketserver -import cmd -import io -import paho.mqtt.client as mqtt -import re - - - - diff --git a/src/CommunicationProcessor.py b/src/CommunicationProcessor.py index ca9492d..c479e87 100644 --- a/src/CommunicationProcessor.py +++ b/src/CommunicationProcessor.py @@ -1,5 +1,8 @@ import threading import datetime +import RS485Ext +import RegisterDatapoint +from pymodbus.client.sync import ModbusSerialClient class CommunicationProcessor(threading.Thread): def __init__(self, config, queue): @@ -8,10 +11,27 @@ class CommunicationProcessor(threading.Thread): self.queue = queue self.daemon = True + def __getSerial(self): + return RS485Ext.RS485Ext(port=self.config.serialPort, baudrate=self.config.serialBaudRate, stopbits=1, + timeout=1) + + def run(self): + client = ModbusSerialClient(method='rtu') + client.socket = self.getSerial() + client.connect() + while True: r = self.queue.get() - r.process() - r.lastContact = datetime.datetime.now() - print("Dequeued: {0!s}".format(r)) - r.enqueued = False + try: + print("Dequeued: {0!s}".format(r)) + r.enqueued = False + r.process(client) + except RegisterDatapoint.DatapointException as e: + print("ERROR when processing '{0}': {1!s}".format(r.label, e)) + if client.socket is None: + print("renew socket") + client.socket = self.getSerial() + + + diff --git a/src/Config.py b/src/Config.py index 8278f02..3356973 100644 --- a/src/Config.py +++ b/src/Config.py @@ -8,4 +8,6 @@ class Config(object): self.cmdAddress = '127.0.0.1' self.cmdPort = 9999 self.registerFile = 'registers.pkl' + self.serialPort = '/dev/ttyAMA0' + self.serialBaudRate = 9600 diff --git a/src/RegisterDatapoint.py b/src/RegisterDatapoint.py index 0691a83..82dce54 100644 --- a/src/RegisterDatapoint.py +++ b/src/RegisterDatapoint.py @@ -1,6 +1,9 @@ import datetime +from pymodbus.pdu import ExceptionResponse +from pymodbus.exceptions import ModbusIOException +class DatapointException(Exception): pass class AbstractModbusDatapoint(object): def __init__(self, label, unit, address, count, scanRate): @@ -19,7 +22,7 @@ class AbstractModbusDatapoint(object): def __str__(self): return "{0}, {1}: Unit: {2}, Address: {3}, Count: {4}".format(self.type, self.label, self.unit, self.address, self.count) - def process(self): + def process(self, client): raise NotImplementedError @@ -36,7 +39,7 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint): def __str__(self): return "[{0!s}, Read: {1}, Write: {2}, Feedback: {3}".format(super().__str__(), self.publishTopic, self.subscribeTopic, self.feedbackTopic) - def process(self): + def process(self, client): successFull = False giveUp = False if self.writeRequestValue: @@ -53,6 +56,13 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint): else: # perform read operation print("Holding register, perform read operation") + result = client.read_holding_registers(address=self.address, + count=self.count, + unit=self.unit) + if type(result) in [ExceptionResponse, ModbusIOException]: + raise DatapointException(result) + print("{0}: {1!s}".format(self.label, result.registers)) + if successFull: self.lastContact = datetime.datetime.now() # publish value @@ -79,10 +89,17 @@ class InputRegisterDatapoint(AbstractModbusDatapoint): def __str__(self): return "[{0!s}, {1}".format(super().__str__(), self.publishTopic) - def process(self): + def process(self, client): successFull = False giveUp = False # perform read operation + result = client.read_input_registers(address=self.address, + count=self.count, + unit=self.unit) + if type(result) in [ExceptionResponse, ModbusIOException]: + raise DatapointException(result) + print("{0}: {1!s}".format(self.label, result.registers)) + print("Input register, perform read operation") if successFull: self.lastContact = datetime.datetime.now()