diff options
author | Marc Lasch <mlasch@mailbox.org> | 2017-04-16 17:08:31 +0200 |
---|---|---|
committer | Marc Lasch <mlasch@mailbox.org> | 2017-04-16 17:08:31 +0200 |
commit | d50ca49bb9dcf5640487df2044b62d63432b6764 (patch) | |
tree | 5ba8de4732f2a2143ba704aa67ff2ac534ac5df0 /usv_driver.py | |
parent | b4c39e0ea54791d3c56d9d42b1368bd3dbce5be3 (diff) | |
download | usv_tools-d50ca49bb9dcf5640487df2044b62d63432b6764.tar.gz usv_tools-d50ca49bb9dcf5640487df2044b62d63432b6764.zip |
Minor changes
Diffstat (limited to 'usv_driver.py')
-rwxr-xr-x | usv_driver.py | 49 |
1 files changed, 28 insertions, 21 deletions
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'] |