-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_setup.py
More file actions
84 lines (80 loc) · 2.14 KB
/
serial_setup.py
File metadata and controls
84 lines (80 loc) · 2.14 KB
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
from dependency import *
import keyboard
import datetime
global a
a ={}
no_of_ports = 0
def scan_ports():
global no_of_ports
global a
global port
global pf
port = ""
a ={}
b ={}
pf = 1
ports= list(port_list.comports())
no_of_ports = len(ports)
cnfg_file = open("config.json","r+")
dic = eval((cnfg_file.read()))
baud = dic["baud"]
vf = dic["vf"]
port = dic["port"]
cnfg_file.close()
for i in range(no_of_ports):
a[i] = ports[i].device
print(" ","\U0001F449","Active Ports :")
for i in range(no_of_ports):
cprint (" >",'white',end="", flush=True)
cprint(a[i],'cyan')
if a[i] == port:
c = port
else:
c = a[i]
b["port"] = c
b["baud"] = baud
b["vf"] = vf
cnfg_file = open("config.json","w")
cnfg_file.write(str(b))
cnfg_file.close()
return a
def serial_open():
if (len(scan_ports()) == 0):
cprint("No ports available",'red')
mm()
else :
i = 1
cnfg_file = open("config.json","r+")
dic = eval((cnfg_file.read()))
port = dic["port"]
baud = dic["baud"]
vf = dic["vf"]
cnfg_file.close()
print("Connected to", port)
ser = serial.Serial(port,baud)
verbos_file = open("output.txt","w")
verbos_file.write(str(datetime.datetime.now()))
verbos_file.write("\n")
verbos_file.close()
verbos_file = open("output.txt","a")
if ser.is_open == False:
ser.open()
while True:
msg = str(ser.readline())
if vf == 1:
verbos_file.write(str_process(msg))
verbos_file.write("\n")
if (i%2) == 0:
cprint(str_process(msg),'white')
else:
cprint(str_process(msg),'cyan')
verbos_file.write(str_process(msg))
verbos_file.write("\n")
if keyboard.is_pressed('Esc'):
break
i = i+1
def str_process(msg):
msg = msg.strip()
msg = msg.replace("b'","")
msg = msg.replace("\\r\\n'","")
return msg