From 326669b6642e0e1a9485d0998fac78446cfcb49c Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Tue, 4 Feb 2014 00:52:37 +0000 Subject: [PATCH] Work on writing settings to devices --- vis/device.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/vis/device.py b/vis/device.py index 24860ba..a896375 100644 --- a/vis/device.py +++ b/vis/device.py @@ -1,3 +1,5 @@ +import time + from re import compile from serial import Serial from datetime import datetime @@ -32,6 +34,7 @@ class Device: bytesize = self.config.databits, \ stopbits = self.config.stopbits, \ timeout = 0) + self.control() self.serial.flushInput() except Exception as ex: return str(ex) @@ -48,7 +51,12 @@ class Device: return True def control(self): - pass + for key in list(self.config.enable.keys()): + state = self.config.enable[key] + rate = self.config.rate[key] + cmd = 'start' if state else 'stop' + self._write_ascii('%s %s' % (cmd, key)) + self._write_ascii('set %s int %d' % (key, rate)) def process(self): items = [] @@ -67,6 +75,7 @@ class Device: if len(self.inbuf) == 0: continue line = "".join(self.inbuf) + print('read: [' + line + ']') item = self._parse_ascii(line) items.append(item) self.inbuf = [] @@ -79,6 +88,14 @@ class Device: return items # Private methods + def _write_ascii(self, line): + if self.serial: + print('write: [' + line + ']') + data = bytes(line+'\n', 'UTF-8') + self.serial.write(data) + self.serial.flush() + time.sleep(0.1) + def _parse_ascii(self, line): acc_re = compile("\[ACC\] accX=(.*) accY=(.*) accZ=(.*)") mag_re = compile("\[MAG\] magX=(.*) magY=(.*) magZ=(.*)") -- 2.43.2