From 112f478b2fc9f5365a5e2111cde1decfb0ceff0d Mon Sep 17 00:00:00 2001 From: Marc Lasch Date: Mon, 1 May 2017 21:24:54 +0200 Subject: Major library update --- usv/__init__.py | 1 + usv/usv.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 usv/__init__.py create mode 100644 usv/usv.py (limited to 'usv') diff --git a/usv/__init__.py b/usv/__init__.py new file mode 100644 index 0000000..d48dae4 --- /dev/null +++ b/usv/__init__.py @@ -0,0 +1 @@ +from .usv import Usv diff --git a/usv/usv.py b/usv/usv.py new file mode 100644 index 0000000..14b8735 --- /dev/null +++ b/usv/usv.py @@ -0,0 +1,56 @@ +import re +from serial import Serial + +parameters = { + 'acvoltsin': (1, '01 V In +([0-9]+[.]?[0-9]*)'), + 'acvoltsout': (2, '02 V Out +([0-9]+[.]?[0-9]*)'), + 'vaout': (5, '05 VA Out +([0-9]+[.]?[0-9]*)'), + 'vbattery': (7, '07 V Batt +([0-9]+[.]?[0-9]*)'), + 'frequency': (8, '08 Freq ([0-9]+[.]?[0-9]*) Hz'), + 'fullload': (16, '16 FullLoad% ([0-9]+[.]?[0-9]*)'), + 'watts': (17, '17 Watts +([0-9]+[.]?[0-9]*)'), + 'powerfactor': (18, '18 PF +([0-9]+[.]?[0-9]*) ----'), + 'crestfactor': (19, '19 CrestF +([0-9]+[.]?[0-9]*)'), + 'powerout': (20, '20 #PwrOut +([0-9]+[.]?[0-9]*)'), + 'inverterminutes': (23, '23 InvMin +([0-9]+[.]?[0-9]*)') + } + + +class UsvSerialHandler(object): + def __init__(self, device_path): + """ Handles the serial connection to the usv """ + + self._serial_port = Serial(device_path, + baudrate=600, + timeout=0.8) + + # force clean prompt + self._serial_port.write('\r') + self._serial_port.read(100) + + def request(self, index): + """ Write and read until prompt """ + + self._serial_port.write('d {}\r'.format(index)) + + return self._serial_port.read(21) + + +class Usv(UsvSerialHandler): + def __init__(self, device_path): + super().__init__(device_path) + + def read(self): + fields = {} + + for name, (index, regex) in parameters.items(): + result = self.request(index) + + match = re.search(regex, result, re.MULTILINE) + + if match: + value = float(match.group(1)) + + fields[name] = value + + return fields -- cgit v1.2.1