-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr_scan_url.py
86 lines (76 loc) · 3.5 KB
/
qr_scan_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## Barcode Scanner in Virtual COM Port Mode
## Program Purpose = scan a QR code with a url and then open said url in a new browser tab
## L. Robertson, December 2022
##
## REQUIRED PACKAGES:
## pyserial (DANGER: Do not install serial separately from pyserial, conflicts will arise... ask me how I know)
## HELPFUL LINKS:
## I couldn't get this to work but I still found it useful: https://gist.github.com/maxwiese/db51e4a60d0c09727413e7f5f45af03f
## GOTCHAS:
## QR Code must terminate with a carriage return (aka: \r aka: 0x0D)
#------------------------------------------------------------------------------
from time import sleep
import serial
import serial.tools.list_ports
import webbrowser
#------------------------------------------------------------------------------
def search_for_ports():
list_of_ports = list(serial.tools.list_ports.comports())
return list_of_ports
#------------------------------------------------------------------------------
def reconnect_loop(com_port):
while True: #infinite loop until the connection is re-established
try:
serial_conn = serial.Serial(com_port) #returns an error if a connection is not made
break #once a connection is reestablished we can break the loop
except:
sleep(3) #delay before we try again
print("connection has been re-established")
return serial_conn
#------------------------------------------------------------------------------
if __name__ == "__main__":
print('\nAvailable ports:')
list_of_ports = search_for_ports()
index_count = 0 #a dumb counter to keep track of the number of available ports
for index, port in enumerate(list_of_ports):
print('[index: {}] {}'.format(index, port.description))
index_count = index
while True:
com_port_index = input("\nSelect the port you wish to use (use index number) > ") #caution, com_port_index will be a string.
if com_port_index.isnumeric():
if int(com_port_index) <= index_count:
com_port = list_of_ports[int(com_port_index)].device
break
else:
print("Invalid selection. Please try again.")
else:
if com_port_index == 'q':
print("Exiting program...goodbye")
exit(0)
else:
print("Invalid selection. Please try again.")
# while True:
# try:
# baudrate = input("\nEnter baudrate [9600 is typical] > ")
# break
# except:
# print('\ninvalid baudrate...try again')
try:
serial_conn = serial.Serial(com_port)
#serial_conn = serial.Serial(com_port, baudrate)
print("port details: {}" .format(serial_conn))
except:
print("error thrown trying to connect to the serial port... program will now exit")
exit(0)
print('\nconnection established... let\'s get scanning')
while serial_conn.is_open:
try:
qr_url = serial_conn.read_until(b'\r') #note: qr_url type will be <class 'bytes'>...assumes qr code ends is 0x0D (carriage return aka \r)
qr_url_string = qr_url.decode("utf-8")
print(qr_url_string)
b = webbrowser.open_new_tab(qr_url_string)
except:
print("connection lost... let's try to get it back.")
serial_conn = reconnect_loop(com_port)
exit(0)
#------------------------------------------------------------------------------