-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_gui.py
More file actions
240 lines (199 loc) · 7.88 KB
/
main_gui.py
File metadata and controls
240 lines (199 loc) · 7.88 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#! /usr/bin/env python3.7
# -*- coding: utf-8 -*-
# Created by kayrlas on August 17, 2019 (https://github.com/kayrlas)
# main_gui.py
from threading import Thread
import time
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from serialcompy import SerialCom
class Application(ttk.Frame):
def __init__(self, master=None):
super().__init__(master=master)
self.master = master
self.pack()
self.serialcom = SerialCom(baudrate=9600, timeout=0.1, writemode=True)
self.create_wedgets()
def create_wedgets(self):
self._wedget_statusbar()
self._wedget_com()
self._wedget_bps()
self._wedget_send()
self._wedget_txrx()
def _wedget_statusbar(self):
# StatusBar
lf_status = ttk.LabelFrame(self.master, text="Status Bar")
lf_status.pack(expand=False, side="top")
self.la_status = ttk.Label(
master=lf_status,
width=62,
text="No Connection",
background="lightgray")
self.la_status.pack(expand=False, side="left")
def _wedget_com(self):
# COM list pulldown, Reload button
lf_com = ttk.LabelFrame(self.master, text="COM")
lf_com.pack(expand=False, side="top")
self.cb_com = ttk.Combobox(
master=lf_com,
state="readonly",
values=self.serialcom.find_comports(),
width=46)
self.cb_com.current(0)
self.cb_com.pack(expand=False, side="left")
self.btn_reload = ttk.Button(
master=lf_com,
text="Reload",
command=self.reload_com_btn)
self.btn_reload.pack(expand=False, side="left")
def _wedget_bps(self):
# Baudrate list pulldown, Open button, Close button
lf_bps = ttk.LabelFrame(self.master, text="Baudrate (bps)")
lf_bps.pack(expand=False, side="top")
self.en_bps = ttk.Entry(
master=lf_bps,
width=36)
self.en_bps.pack(expand=False, side="left")
self.btn_open = ttk.Button(
master=lf_bps,
text="Open",
command=self.open_com_btn)
self.btn_open.pack(expand=False, side="left")
self.btn_close = ttk.Button(
master=lf_bps,
text="Close",
command=self.close_com_btn,
state="disable")
self.btn_close.pack(expand=False, side="left")
def _wedget_send(self):
# String entry
lf_send = ttk.LabelFrame(self.master, text="Text")
lf_send.pack(expand=False, side="top")
self.en_send = ttk.Entry(
master=lf_send,
width=49,
state="disable")
self.en_send.pack(expand=False, side="left")
self.btn_send = ttk.Button(
master=lf_send,
text="Send",
command=self.send_text_btn,
state="disable")
self.btn_send.pack(expand=False, side="left")
def _wedget_txrx(self):
# TX RX
pw_txrx = ttk.PanedWindow(self.master, orient="horizontal")
pw_txrx.pack(expand=False, side="top")
## TX
lf_tx = ttk.LabelFrame(pw_txrx, text="TX")
lf_tx.pack(expand=False, side="left")
self.lb_tx = tk.Listbox(
master=lf_tx,
height=20,
width=30,
state="disable")
self.lb_tx.pack(expand=False)
self.btn_txexp = ttk.Button(
master=lf_tx,
text="TX Export",
command=self.exp_tx_btn,
state="disable")
self.btn_txexp.pack(expand=False, side="right")
## RX
lf_rx = ttk.LabelFrame(pw_txrx, text="RX")
lf_rx.pack(expand=False, side="right")
self.lb_rx = tk.Listbox(
lf_rx,
height=20,
width=30,
state="disable")
self.lb_rx.pack(expand=False)
self.btn_rxexp = ttk.Button(
lf_rx,
text="RX Export",
command=self.exp_rx_btn,
state="disable")
self.btn_rxexp.pack(expand=False, side="right")
def reload_com_btn(self):
self.cb_com.config(values=self.serialcom.find_comports())
def open_com_btn(self):
_device = self.serialcom.devices[self.cb_com.current()].device
_baudrate = self.en_bps.get()
if not self.serialcom.register_comport(device=_device):
print("Cannot specify the comport. Please try again.")
elif _baudrate.isdecimal() or _baudrate is "":
if _baudrate.isdecimal():
self.serialcom.serial.baudrate = int(_baudrate)
self.serialcom.serial.open()
self.la_status.config(text="Opening", background="lightgreen")
self.en_send.config(state="normal")
self.btn_send.config(state="normal")
self.btn_close.config(state="normal")
self.lb_rx.config(state="normal")
self.btn_rxexp.config(state="normal")
self.cb_com.config(state="disable")
self.btn_reload.config(state="disable")
self.en_bps.config(state="disable")
self.btn_open.config(state="disable")
if self.serialcom.writemode:
self.lb_tx.config(state="normal")
self.btn_txexp.config(state="normal")
self._start_serialread()
else:
print("Text in the baudrate entry is not a number.")
def _start_serialread(self):
self._th_sread = Thread(target=self._serial_read)
self._th_sread.start()
def _serial_read(self):
while self.serialcom.serial.is_open:
try:
_recv_data = self.serialcom.serial.readline()
except (TypeError, AttributeError):
print("Comport disconnected while reading")
else:
if _recv_data != b'':
self.lb_rx.insert(tk.END, _recv_data.strip().decode("utf-8"))
time.sleep(1)
def close_com_btn(self):
self.serialcom.close_comport()
self._th_sread.join()
self.cb_com.config(state="readonly")
self.btn_reload.config(state="normal")
self.en_bps.config(state="normal")
self.btn_open.config(state="normal")
self.la_status.config(text="Disconnected", background="lightgray")
self.en_send.config(state="disable")
self.btn_send.config(state="disable")
self.btn_close.config(state="disable")
self.lb_rx.config(state="disable")
self.btn_rxexp.config(state="disable")
if self.serialcom.writemode:
self.lb_tx.config(state="disable")
self.btn_txexp.config(state="disable")
def send_text_btn(self):
_send_data = self.en_send.get()
self.serialcom.serial.write(_send_data.encode("utf-8"))
self.lb_tx.insert(tk.END, _send_data)
self.en_send.delete(0, tk.END)
def exp_tx_btn(self):
_fname = filedialog.asksaveasfilename(
initialdir="/",
title="Save as",
filetypes=[("text file", "*.txt"), ("all files", "*.*")])
with open(_fname, 'w') as f:
for i in range(self.lb_tx.size()):
f.write(str(self.lb_tx.get(i)) + "\n")
def exp_rx_btn(self):
_fname = filedialog.asksaveasfilename(
initialdir="/",
title="Save as",
filetypes=[("text file", "*.txt"), ("all files", "*.*")])
with open(_fname, 'w') as f:
for i in range(self.lb_rx.size()):
f.write(str(self.lb_rx.get(i)) + "\n")
if __name__ == "__main__":
root = tk.Tk()
root.title("SerialComPython")
Application(master=root)
root.mainloop()