Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / tools / btaddon / hc06_console_AT.py
blob4b0d7fd9628df0e9a2b44deb1aa6f00da0b7445a
1 #!/usr/bin/env python3
3 import sys
4 import time
5 try:
6 import serial
7 except ModuleNotFoundError:
8 print("Please install pyserial module first.")
9 sys.exit(1)
11 if len(sys.argv) < 2:
12 print('Usage: %s <baudrate>' % sys.argv[0])
13 sys.exit(1)
14 baudrate = int(sys.argv[1])
15 ser = serial.Serial(
16 port='/dev/ttyUSB0',
17 baudrate=baudrate,
18 parity=serial.PARITY_NONE,
19 stopbits=serial.STOPBITS_ONE,
20 bytesize=serial.EIGHTBITS
23 ser.isOpen()
24 ser.write(b'AT')
25 out = b''
26 time.sleep(1)
27 while ser.inWaiting() > 0:
28 out += ser.read(1)
29 if out != b'OK':
30 ser.close()
31 print("HC-06 dongle not found. Abort.")
32 exit(1)
33 print('Enter your commands below.\r\nInsert "exit" to leave the application.')
35 while 1 :
36 # get keyboard input
37 inp = input(">> ")
38 inp = inp.encode('utf8')
39 if inp == 'exit':
40 ser.close()
41 exit()
42 else:
43 ser.write(inp)
44 out = b''
45 wait = 1
46 ti = time.perf_counter()
47 while True:
48 time.sleep(0.05)
49 if (ser.in_waiting > 0):
50 # When receiving data, reset timer and shorten timeout
51 ti = time.perf_counter()
52 wait = 0.05
53 out += ser.read(1)
54 continue
55 # We stop either after 1s if no data or 50ms after last data received
56 if time.perf_counter() - ti > wait:
57 break
58 if out != b'':
59 print("<< " + out.decode('utf8'))