-
Notifications
You must be signed in to change notification settings - Fork 0
/
microcontrollees.py
54 lines (46 loc) · 1.76 KB
/
microcontrollees.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
# https://stackpython.co/tutorial/python-gui-tkinter-ep1-introduction
# https://docs.python.org/3/library/tkinter.html
import tkinter as tk
import serial
from time import sleep
TERMINAL = '\r\n'
def SendCmd(text):
serialPort.write(bytes(text.encode()))
print(text)
def SendCmdGet(self, Info):
SendCmd(Info)
serialPort.flushInput()
sleep(0.01)
if(serialPort.in_waiting > 0):
serialString = serialPort.readline()
# serialString = serialPort.read_until('#')
# print(serialString.decode())
Cmds = serialString.decode().split(",")
# print(Cmds)
text.delete("1.0", tk.END)
text.insert("1.0", Cmds[0])
# while(1):
# for i in range(1000):
# if(serialPort.in_waiting > 0):
# # serialString = serialPort.readline() // Worked
# serialString = serialPort.read_until('#')
# print(serialString.decode())
# break
try:
serialPort = serial.Serial(port = "COM4", baudrate=115200,bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
# serialPort = serial.Serial(port = "COM23", baudrate=9600,bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
serialPort.flushInput()
serialPort.flushOutput()
app = tk.Tk()
BtON = tk.Button(text='Turn ON', command = lambda :SendCmd('1,S,1' + TERMINAL))
BtON.pack()
BtOFF = tk.Button(text='Turn OFF', command = lambda :SendCmd('1,S,0' + TERMINAL))
BtOFF.pack()
text = tk.Text(width = 10, height = 2)
text.insert("5.3", f"ADC Data")
text.pack(expand=True, fill = "both")
BtGet= tk.Button(text='Get A0', command = lambda :SendCmdGet(text, '1,G,0' + TERMINAL))
BtGet.pack()
app.mainloop()
except serial.SerialException as error:
print("Failed {}".format(error))