-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial_Com_ctrl.py
147 lines (135 loc) · 5.28 KB
/
Serial_Com_ctrl.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# This software and its documentation are created by Houssem-eddine Lahmer
# Feel free to use, modify, and distribute them for any purpose.
import threading
import serial.tools.list_ports
# Secure the UART serial communication with MCU
import time
class SerialCtrl():
def __init__(self):
'''
Initializing the main varialbles for the serial data
'''
self.sync_cnt = 200
def getCOMList(self):
'''
Method that get the lost of available coms in the system
'''
ports = serial.tools.list_ports.comports()
self.com_list = [com[0] for com in ports]
self.com_list.insert(0, "-")
def SerialOpen(self, ComGUI):
'''
Method to setup the serial connection and make sure to go for the next only
if the connection is done properly
'''
try:
self.ser.is_open
except:
PORT = ComGUI.clicked_com.get()
BAUD = ComGUI.clicked_bd.get()
self.ser = serial.Serial()
self.ser.baudrate = BAUD
self.ser.port = PORT
self.ser.timeout = 0.1
try:
if self.ser.is_open:
print("Already Open")
self.ser.status = True
else:
PORT = ComGUI.clicked_com.get()
BAUD = ComGUI.clicked_bd.get()
self.ser = serial.Serial()
self.ser.baudrate = BAUD
self.ser.port = PORT
self.ser.timeout = 0.01
self.ser.open()
self.ser.status = True
except:
self.ser.status = False
def SerialClose(self, ComGUI):
'''
Method used to close the UART communication
'''
try:
self.ser.is_open
self.ser.close()
self.ser.status = False
except:
self.ser.status = False
def SerialStop(self, gui):
self.ser.write(gui.data.StopStream.encode())
def SerialSync(self, gui):
self.threading = True
cnt = 0
while self.threading:
try:
self.ser.write(gui.data.sync.encode())
gui.conn.sync_status["text"] = "..Sync.."
gui.conn.sync_status["fg"] = "orange"
gui.data.RowMsg = self.ser.readline()
# print(f"RowMsg: {gui.data.RowMsg}")
gui.data.DecodeMsg()
if gui.data.sync_ok in gui.data.msg[0]:
if int(gui.data.msg[1]) > 0:
gui.conn.btn_start_stream["state"] = "active"
gui.conn.btn_add_chart["state"] = "active"
gui.conn.btn_kill_chart["state"] = "active"
gui.conn.save_check["state"] = "active"
gui.conn.sync_status["text"] = "OK"
gui.conn.sync_status["fg"] = "green"
gui.conn.ch_status["text"] = gui.data.msg[1]
gui.conn.ch_status["fg"] = "green"
gui.data.SynchChannel = int(gui.data.msg[1])
gui.data.GenChannels()
gui.data.buildYdata()
gui.data.FileNameFunc()
print(gui.data.Channels, gui.data.YData)
self.threading = False
break
if self.threading == False:
break
except Exception as e:
print(e)
cnt += 1
if self.threading == False:
break
if cnt > self.sync_cnt:
cnt = 0
gui.conn.sync_status["text"] = "failed"
gui.conn.sync_status["fg"] = "red"
time.sleep(0.5)
if self.threading == False:
break
def SerialDataStream(self, gui):
self.threading = True
cnt = 0
while self.threading:
try:
self.ser.write(gui.data.StartStream.encode())
gui.data.RowMsg = self.ser.readline()
gui.data.DecodeMsg()
gui.data.StreamDataCheck()
if gui.data.StreamData:
gui.data.SetRefTime()
break
except Exception as e:
print(e)
gui.UpdateChart()
while self.threading:
try:
gui.data.RowMsg = self.ser.readline()
gui.data.DecodeMsg()
gui.data.StreamDataCheck()
if gui.data.StreamData:
gui.data.UpdataXdata()
gui.data.UpdataYdata()
# Ysam = [Ys[len(gui.data.XData)-1] for Ys in gui.data.YData]
gui.data.AdjustData()
# print(
# f"X Len: {len(gui.data.XData)}, Xstart:{gui.data.XData[0]} Xend : {gui.data.XData[len(gui.data.XData)-1]}, Xrange: {gui.data.XData[len(gui.data.XData)-1] - gui.data.XData[0]} Ydata len: {len(gui.data.YData[0])} Yval: : {Ysam} ")
if gui.save:
t1 = threading.Thread(
target=gui.data.SaveData, args=(gui,), daemon=True)
t1.start()
except Exception as e:
print(e)