add actual modbus communication

This commit is contained in:
2019-07-08 15:28:46 +02:00
parent 303f4b50f1
commit 93b3333356
4 changed files with 46 additions and 19 deletions

View File

@ -1,12 +0,0 @@
import queue
import datetime
import threading
import socketserver
import cmd
import io
import paho.mqtt.client as mqtt
import re

View File

@ -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()

View File

@ -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

View File

@ -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()