add actual modbus communication
This commit is contained in:
@ -1,12 +0,0 @@
|
|||||||
import queue
|
|
||||||
import datetime
|
|
||||||
import threading
|
|
||||||
import socketserver
|
|
||||||
import cmd
|
|
||||||
import io
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
|||||||
import threading
|
import threading
|
||||||
import datetime
|
import datetime
|
||||||
|
import RS485Ext
|
||||||
|
import RegisterDatapoint
|
||||||
|
from pymodbus.client.sync import ModbusSerialClient
|
||||||
|
|
||||||
class CommunicationProcessor(threading.Thread):
|
class CommunicationProcessor(threading.Thread):
|
||||||
def __init__(self, config, queue):
|
def __init__(self, config, queue):
|
||||||
@ -8,10 +11,27 @@ class CommunicationProcessor(threading.Thread):
|
|||||||
self.queue = queue
|
self.queue = queue
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
|
|
||||||
|
def __getSerial(self):
|
||||||
|
return RS485Ext.RS485Ext(port=self.config.serialPort, baudrate=self.config.serialBaudRate, stopbits=1,
|
||||||
|
timeout=1)
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
client = ModbusSerialClient(method='rtu')
|
||||||
|
client.socket = self.getSerial()
|
||||||
|
client.connect()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
r = self.queue.get()
|
r = self.queue.get()
|
||||||
r.process()
|
try:
|
||||||
r.lastContact = datetime.datetime.now()
|
|
||||||
print("Dequeued: {0!s}".format(r))
|
print("Dequeued: {0!s}".format(r))
|
||||||
r.enqueued = False
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,4 +8,6 @@ class Config(object):
|
|||||||
self.cmdAddress = '127.0.0.1'
|
self.cmdAddress = '127.0.0.1'
|
||||||
self.cmdPort = 9999
|
self.cmdPort = 9999
|
||||||
self.registerFile = 'registers.pkl'
|
self.registerFile = 'registers.pkl'
|
||||||
|
self.serialPort = '/dev/ttyAMA0'
|
||||||
|
self.serialBaudRate = 9600
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
from pymodbus.pdu import ExceptionResponse
|
||||||
|
from pymodbus.exceptions import ModbusIOException
|
||||||
|
|
||||||
|
|
||||||
|
class DatapointException(Exception): pass
|
||||||
|
|
||||||
class AbstractModbusDatapoint(object):
|
class AbstractModbusDatapoint(object):
|
||||||
def __init__(self, label, unit, address, count, scanRate):
|
def __init__(self, label, unit, address, count, scanRate):
|
||||||
@ -19,7 +22,7 @@ class AbstractModbusDatapoint(object):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{0}, {1}: Unit: {2}, Address: {3}, Count: {4}".format(self.type, self.label, self.unit, self.address, self.count)
|
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
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
@ -36,7 +39,7 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "[{0!s}, Read: {1}, Write: {2}, Feedback: {3}".format(super().__str__(), self.publishTopic, self.subscribeTopic, self.feedbackTopic)
|
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
|
successFull = False
|
||||||
giveUp = False
|
giveUp = False
|
||||||
if self.writeRequestValue:
|
if self.writeRequestValue:
|
||||||
@ -53,6 +56,13 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
else:
|
else:
|
||||||
# perform read operation
|
# perform read operation
|
||||||
print("Holding register, 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:
|
if successFull:
|
||||||
self.lastContact = datetime.datetime.now()
|
self.lastContact = datetime.datetime.now()
|
||||||
# publish value
|
# publish value
|
||||||
@ -79,10 +89,17 @@ class InputRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "[{0!s}, {1}".format(super().__str__(), self.publishTopic)
|
return "[{0!s}, {1}".format(super().__str__(), self.publishTopic)
|
||||||
|
|
||||||
def process(self):
|
def process(self, client):
|
||||||
successFull = False
|
successFull = False
|
||||||
giveUp = False
|
giveUp = False
|
||||||
# perform read operation
|
# 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")
|
print("Input register, perform read operation")
|
||||||
if successFull:
|
if successFull:
|
||||||
self.lastContact = datetime.datetime.now()
|
self.lastContact = datetime.datetime.now()
|
||||||
|
Reference in New Issue
Block a user