-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
168 lines (162 loc) · 6.69 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding:utf-8 -*-
import time
import logging
import threading
import platform
from UI import UITool
from SerialHelper import Read6050
from IOProcess.DataSave import *
from IOProcess.DataSave import RecordThreadToOneFile
import tkinter as tk
from tkinter import ttk
import gc
logging.basicConfig(level=logging.DEBUG,\
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',\
datefmt='%a, %d %b %Y %H:%M:%S')
class MainSerialToolUI(UITool.SerialToolUI):
def __init__(self,master = None):
self.master=master
super(MainSerialToolUI,self).__init__()
self.ser = None
self.port_list = list()
self.ser_list = list()
self.thread_read = list()
self.ser_all_alive = False
self.thread_record = None
self.serdic={}
def _get_sers(self):
self.port_list = list()
#得到端口号
for index,item in enumerate(self.frm_up_setport_combobox):
ser_num = index
name = item.get()
if name == '-1' :
self.port_list.append("NONE")
else:
self.port_list.append("COM"+name)
def Open(self):
'''
打开关闭串口
'''
if self.frm_up_btn["text"] == "Open":
try:
ser_num = 0 #记录当前处理的com
self.port_list = list()
#得到端口号
for index,item in enumerate(self.frm_up_setport_combobox):
ser_num = index
name = item.get()
if name == '-1' :
self.port_list.append("NONE")
else:
self.port_list.append("COM"+name)
#print (self.port_list)
#开启串口
success_com =""
success_count = 0
self.ser_list=list()
for index,port in enumerate(self.port_list):
ser_num = index
if port == "NONE":
#print ("NONE PORT")
continue
#print (type(port))
self.ser_list.append(Read6050(Port=port))
#print (type(self.ser_list[index]))
for index,ser in enumerate(self.ser_list):
ser_num = index
ser.open()
# ser.setDaemon(True)
if ser.alive:
success_com = success_com + ser.port[3:] + " "
success_count = success_count + 1
if success_count == len(self.ser_list):
self.ser_all_alive = True
#print ("success com:",success_com)
#开启读取线程
if self.ser_all_alive is True:
self.frm_status_label["text"] = "[{0}] Opened!".format(success_com)
self.frm_status_label["fg"] = "#66CD00"
self.frm_up_btn["text"] = "Close"
self.frm_up_btn["bg"] = "#F08080"
for index,ser in enumerate(self.ser_list):
ser_num = index
ser.start()
except Exception as e:
logging.error(e)
try:
self.frm_status_label["text"] = "Open [{0}] Failed!".format(self.ser_list[ser_num])
self.frm_status_label["fg"] = "#DC143C"
except Exception as ex:
logging.error(ex)
elif self.frm_up_btn["text"] == "Close":
try:
for ser in self.ser_list:
ser.stop()
self.ser_all_alive = False
except Exception as e:
logging.error(e)
self.frm_up_btn["text"] = "Open"
self.frm_up_btn["bg"] = "#008B8B"
self.frm_status_label["text"] = "Close Serial!"
self.frm_status_label["fg"] = "#8DEEEE"
def Record(self):
'''记录和停止记录'''
if self.frm_down_btn1["text"] == "Record":
#print(self.frm_up_btn["text"],self.ser_all_alive)
if self.frm_up_btn["text"] == "Close" and self.ser_all_alive and self.frm_down_btn2["text"]=="One Time":#串口打开状态
# self.thread_record = RecordThread(self.ser_list)
name="default"
if(len(self.frm_up_entry.get())):
name=self.frm_up_entry.get()
self.thread_record=RecordThreadToOneFile(self.ser_list,name)
self.thread_record.start()
self.frm_status_label["text"] = "Recording Data..."
self.frm_down_btn1["text"] = "Stop"
self.frm_down_btn1["bg"] = "#F08080"
elif self.frm_down_btn1["text"] == "Stop":
self.thread_record.stop()
del self.thread_record
# gc.collect()
self.frm_status_label["text"] = "Record Stopped"
self.frm_down_btn1["text"] = "Record"
self.frm_down_btn1["bg"] = "#008B8B"
def RecordOnce(self):
'''记录一次 按时间'''
if self.frm_down_btn2["text"]=="One Time":
if self.frm_up_btn["text"] == "Close" and self.ser_all_alive and self.frm_down_btn1["text"]=="Record":
self.thread_record=RecordThreadToOneFile(self.ser_list,self.outputname)
self.thread_record.start()
self.frm_status_label["text"] = "One time Recording ..."
self.frm_down_btn2["text"] = "Recording"
self.frm_down_btn2["bg"] = "#F08080"
delay=-1
try:
delay=int(self.frm_down_entry.get())
except:
delay=5000
if(delay<0):
delay=5000
self.master.after(delay,self.ChangeBtn2)
def ChangeBtn2(self):
self.thread_record.stop()
del self.thread_record
self.frm_down_btn2["text"] = "One Time"
self.frm_status_label["text"] = "One Time Record Stopped"
self.frm_down_btn2["text"] = "One Time"
self.frm_down_btn2["bg"] = "#008B8B"
if __name__ == '__main__':
'''
main loop
'''
root = tk.Tk()
root.title("Serial Tool")
if UITool.g_default_theme == "dark":
root.configure(bg="#292929")
combostyle = ttk.Style()
combostyle.theme_use('alt')
combostyle.configure("TCombobox", selectbackground="#292929", fieldbackground="#292929",\
background="#292929", foreground="#FFFFFF")
MainSerialToolUI(master=root)
root.resizable(False, False)
root.mainloop()