summaryrefslogtreecommitdiff
path: root/usv_driver.py
diff options
context:
space:
mode:
authorJack the Ripper <jack@x4.2.localnet.cc>2015-10-24 18:49:10 +0200
committerJack the Ripper <jack@x4.2.localnet.cc>2015-10-24 18:49:10 +0200
commit8d6ccf54a7c1e763e016a82bdce31a19ed94a444 (patch)
tree0fd0b375aaa16cd4f438f6977c63a83cb409041b /usv_driver.py
parentfd78184a68c60008b98b4a525565b7ab56f26b45 (diff)
downloadusv_tools-8d6ccf54a7c1e763e016a82bdce31a19ed94a444.tar.gz
usv_tools-8d6ccf54a7c1e763e016a82bdce31a19ed94a444.zip
first working versionN,
Diffstat (limited to 'usv_driver.py')
-rwxr-xr-xusv_driver.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/usv_driver.py b/usv_driver.py
new file mode 100755
index 0000000..c8ba8bc
--- /dev/null
+++ b/usv_driver.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+
+import serial
+import json
+import re
+
+class Usv(object):
+ def __init__(self, device, baudrate, json_file):
+ self._serial_port = serial.Serial(device,
+ baudrate=baudrate)
+
+ # force a clean prompt
+ self._serial_port.write(b"\r")
+ self._read_until_prompt()
+ #print("cleaned prompt")
+ with open(json_file) as fhandle:
+ self.json_params = json.load(fhandle)
+
+
+ def _read_until_prompt(self):
+ # match on "\n=>"
+ prompt = False
+ inbuf = []
+ while not prompt:
+ inbuf.append(self._serial_port.read(1))
+ if len(inbuf) > 2:
+ if inbuf[-3] == b"\n" and \
+ inbuf[-2] == b"=" and inbuf[-1] == b">":
+ prompt = True
+ return inbuf
+
+ def get_parameters(self):
+ # [{'name': 'acvoltsin', 'index': 1},
+ results = {}
+
+ for parameter in self.json_params:
+ outbuf = "d %s\r" % parameter['num']
+ self._serial_port.write(outbuf.encode())
+ inbuf = self._read_until_prompt()
+
+ sbuf = ""
+ for char in inbuf:
+ sbuf += char.decode("utf-8")
+ #print("-->%s<--" % sbuf)
+
+ regex = parameter['regex']
+ match = re.search(regex, sbuf, re.MULTILINE)
+
+ if match:
+ name = parameter['name']
+ value = float(match.group(1))
+
+ results.setdefault(name, value)
+ #print("MATCH: %s: %f" % (name,value))
+
+ return results
+
+
+
+if __name__ == "__main__":
+ import pprint
+
+ usv = Usv("/dev/ttyU0", 600, "usv_param.json")
+ parameters = usv.get_parameters()
+ pprint.pprint(parameters)
+