From d50ca49bb9dcf5640487df2044b62d63432b6764 Mon Sep 17 00:00:00 2001 From: Marc Lasch Date: Sun, 16 Apr 2017 17:08:31 +0200 Subject: Minor changes --- usv_driver.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'usv_driver.py') diff --git a/usv_driver.py b/usv_driver.py index 5cb1167..bdf8656 100755 --- a/usv_driver.py +++ b/usv_driver.py @@ -8,27 +8,40 @@ class Usv(object): def __init__(self, device, baudrate, json_file): self._serial_port = serial.Serial(device, baudrate=baudrate, - timeout=10) + timeout=0.8) # force a clean prompt - self._serial_port.write(b"\r") - self._read_until_prompt() + #self._serial_port.write(b"\r") + self._read_until_prompt("\r") #print("cleaned prompt") with open(json_file) as fhandle: self.json_params = json.load(fhandle) - def _read_until_prompt(self): + def _read_until_prompt(self, outbuf): # 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 + self._serial_port.write(outbuf.encode()) + + inbuf = self._serial_port.read(100) + sbuf = inbuf.decode("utf-8") + + #prompt = False + #inbuf = [] + #while not prompt: + # pending = self._serial_port.read(1) + # if len(pending) == 0: + # self._serial_port.write(b"\r") + # else: + # inbuf.append(pending) + # print(pending) + #if len(sbuf) > 2: + # if sbuf[-3] == "\n" and \ + # sbuf[-2] == "=" and sbuf[-1] == ">": + # return sbuf + # else: + # sbuf = self._read_until_prompt(outbuf) + + return sbuf def get_parameters(self): # [{'name': 'acvoltsin', 'index': 1}, @@ -36,16 +49,10 @@ class Usv(object): for parameter in self.json_params: outbuf = "d %s\r" % parameter['num'] - self._serial_port.write(outbuf.encode()) - inbuf = self._read_until_prompt() + inbuf = self._read_until_prompt(outbuf) - sbuf = "" - for char in inbuf: - sbuf += char.decode("utf-8") - #print("-->%s<--" % sbuf) - regex = parameter['regex'] - match = re.search(regex, sbuf, re.MULTILINE) + match = re.search(regex, inbuf, re.MULTILINE) if match: name = parameter['name'] -- cgit v1.2.1