diff options
author | Marc Lasch <mlasch@mailbox.org> | 2017-05-01 21:24:54 +0200 |
---|---|---|
committer | Marc Lasch <mlasch@mailbox.org> | 2017-05-01 21:24:54 +0200 |
commit | 112f478b2fc9f5365a5e2111cde1decfb0ceff0d (patch) | |
tree | 87fbd25c5d34c85ca8eaf8a59ba18d0c8e082944 /usv | |
parent | 80d80c305eb3f1227ebd88bdfb2840deb9b43682 (diff) | |
download | usv_tools-112f478b2fc9f5365a5e2111cde1decfb0ceff0d.tar.gz usv_tools-112f478b2fc9f5365a5e2111cde1decfb0ceff0d.zip |
Major library update
Diffstat (limited to 'usv')
-rw-r--r-- | usv/__init__.py | 1 | ||||
-rw-r--r-- | usv/usv.py | 56 |
2 files changed, 57 insertions, 0 deletions
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 |