-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (89 loc) · 2.58 KB
/
app.py
File metadata and controls
114 lines (89 loc) · 2.58 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
import eel
import serial
from serial.tools import list_ports
import threading
import requests
import webbrowser
import atexit
from pydantic.v1.typing import Optional
REPOS_OWNER = "Grigoriy457"
REPOS_NAME = "python-serial-projector"
VERSION = (1, 2)
event: Optional[threading.Event] = None
@eel.expose
def check_for_update():
response = requests.get(f"https://api.github.com/repos/{REPOS_OWNER}/{REPOS_NAME}/tags")
if response.status_code == 200:
last_ver = tuple(map(int, response.json()[0]["name"][1:].split(".")))
print("Latest version:", last_ver)
return last_ver > VERSION, last_ver
return False, VERSION
@eel.expose
def open_last_version(version: tuple):
webbrowser.open_new(f"https://github.com/{REPOS_OWNER}/{REPOS_NAME}/releases/tag/v{'.'.join(map(str, version))}")
@eel.expose
def get_com_list():
com_list = [i.name for i in list_ports.comports()]
return com_list
def serial_reader(port, bit_rate, data_bits, parity_bit, stop_bits, event):
bytesize = serial.EIGHTBITS
if data_bits == "7":
bytesize = serial.SEVENBITS
parity = serial.PARITY_NONE
if parity_bit == "odd":
parity = serial.PARITY_ODD
elif parity_bit == "even":
parity = serial.PARITY_EVEN
stopbits = serial.STOPBITS_ONE
if stop_bits == "two":
stopbits = serial.STOPBITS_TWO
try:
s = serial.Serial(port=port.split(" - ")[0], baudrate=int(bit_rate), parity=parity, stopbits=stopbits, bytesize=bytesize, timeout=0)
except serial.serialutil.SerialException:
eel.sleep(0.05)
eel.cant_connect()
return
while True:
try:
string = ""
while True:
if event.is_set():
s.close()
return
if s.in_waiting != 0:
symbol = s.readline()
try:
symbol = symbol.decode()
string += symbol
if symbol.endswith("\n"):
break
except UnicodeDecodeError:
print("[ERROR]:", symbol)
string = string.strip()
if string != "":
print("[SERIAL]:", string)
eel.set_text(string)
except serial.serialutil.SerialException:
eel.device_lost()
break
@eel.expose
def py_connect(port, bit_rate, data_bits, parity_bit, stop_bits):
global event
if port == "":
return
event = threading.Event()
threading.Thread(target=serial_reader, args=(port, bit_rate, data_bits, parity_bit, stop_bits, event)).start()
return 1
@eel.expose
def py_disconnect():
if event is not None:
event.set()
eel.sleep(0.05)
return 1
def close_callback(route, websockets):
if not websockets:
exit()
if __name__ == '__main__':
eel.init('web')
eel.start('main.html', size=(800, 500), close_callback=close_callback, port=18080)
atexit.register(lambda: eel.close_window())