rework rx timings in python scripts

This commit is contained in:
Philippe Teuwen 2019-05-23 00:05:45 +02:00
commit 86ee0658cf
2 changed files with 30 additions and 10 deletions

View file

@ -38,8 +38,18 @@ while 1 :
else: else:
ser.write(inp) ser.write(inp)
out = b'' out = b''
time.sleep(1) wait = 1
while ser.inWaiting() > 0: 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) 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'': if out != b'':
print("<< " + out.decode('utf8')) print("<< " + out.decode('utf8'))

View file

@ -6,7 +6,7 @@ import serial
name = b'PM3_RDV4.0' name = b'PM3_RDV4.0'
pin = b'1234' pin = b'1234'
role = b'M' role = b'M'
#role = b'S' role = b'S'
ser = None ser = None
@ -28,13 +28,23 @@ p2c={
serial.PARITY_EVEN:b'E' serial.PARITY_EVEN:b'E'
} }
def send(cmd): def send(cmd, timeout=1):
print("<<" + cmd.decode('utf8')) print("<<" + cmd.decode('utf8'))
ser.write(cmd) ser.write(cmd)
out = b'' out = b''
time.sleep(1) wait = timeout
while ser.inWaiting() > 0: 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) 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'': if out != b'':
print(">>" + out.decode('utf8')) print(">>" + out.decode('utf8'))
return out return out
@ -58,7 +68,7 @@ def usart_bt_testcomm(baudrate, parity):
if __name__ == '__main__': if __name__ == '__main__':
print("WARNING: process only if strictly needed!") print("WARNING: process only if strictly needed!")
print("This requires HC-06 dongle turned ON and NOT connected!") 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.") print("Aborting.")
exit(1) exit(1)
@ -101,7 +111,7 @@ if __name__ == '__main__':
bytesize=serial.EIGHTBITS bytesize=serial.EIGHTBITS
) )
ser.isOpen() ser.isOpen()
if (send(b'AT') == b'OK'): if (send(b'AT', timeout=2) == b'OK'):
print("HC-06 dongle successfully reset") print("HC-06 dongle successfully reset")
else: else:
print("Lost contact with add-on, please try again") print("Lost contact with add-on, please try again")