From 86ee0658cf601ba5e5a7263218f3b6fc1f4271a8 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 23 May 2019 00:05:45 +0200 Subject: [PATCH] rework rx timings in python scripts --- tools/btaddon/hc06_console_AT.py | 16 +++++++++++++--- tools/btaddon/hc06_factory.py | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tools/btaddon/hc06_console_AT.py b/tools/btaddon/hc06_console_AT.py index d6405ebec..6520aa9aa 100755 --- a/tools/btaddon/hc06_console_AT.py +++ b/tools/btaddon/hc06_console_AT.py @@ -38,8 +38,18 @@ while 1 : else: ser.write(inp) out = b'' - time.sleep(1) - while ser.inWaiting() > 0: - out += ser.read(1) + wait = 1 + ti = time.perf_counter() + while True: + time.sleep(0.05) + if (ser.in_waiting > 0): + # When receiving data, reset timer and shorten timeout + ti = time.perf_counter() + wait = 0.05 + out += ser.read(1) + continue + # We stop either after 1s if no data or 50ms after last data received + if time.perf_counter() - ti > wait: + break if out != b'': print("<< " + out.decode('utf8')) diff --git a/tools/btaddon/hc06_factory.py b/tools/btaddon/hc06_factory.py index e4e460289..f6d0f25b2 100755 --- a/tools/btaddon/hc06_factory.py +++ b/tools/btaddon/hc06_factory.py @@ -6,7 +6,7 @@ import serial name = b'PM3_RDV4.0' pin = b'1234' role = b'M' -#role = b'S' +role = b'S' ser = None @@ -28,13 +28,23 @@ p2c={ serial.PARITY_EVEN:b'E' } -def send(cmd): +def send(cmd, timeout=1): print("<<" + cmd.decode('utf8')) ser.write(cmd) out = b'' - time.sleep(1) - while ser.inWaiting() > 0: - out += ser.read(1) + wait = timeout + ti = time.perf_counter() + while True: + time.sleep(0.05) + if (ser.in_waiting > 0): + # When receiving data, reset timer and shorten timeout + ti = time.perf_counter() + wait = 0.05 + out += ser.read(1) + continue + # We stop either after 1s if no data or 50ms after last data received + if time.perf_counter() - ti > wait: + break if out != b'': print(">>" + out.decode('utf8')) return out @@ -58,7 +68,7 @@ def usart_bt_testcomm(baudrate, parity): if __name__ == '__main__': print("WARNING: process only if strictly needed!") print("This requires HC-06 dongle turned ON and NOT connected!") - if input("Is the HC-06 dingle LED blinking? (Say 'n' if you want to abort) [y/n] ") != 'y': + if input("Is the HC-06 dongle LED blinking? (Say 'n' if you want to abort) [y/n] ") != 'y': print("Aborting.") exit(1) @@ -101,7 +111,7 @@ if __name__ == '__main__': bytesize=serial.EIGHTBITS ) ser.isOpen() - if (send(b'AT') == b'OK'): + if (send(b'AT', timeout=2) == b'OK'): print("HC-06 dongle successfully reset") else: print("Lost contact with add-on, please try again")