I have a Button in gui to take data from serial port.
here is my code:
def takeRead():
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM7" # "COM#" this must match with connected port
ser.open() #opening serial port
while True:
global data, x, y0
y0 = []
data = []
x = []
for j in range(8000):
line = ser.readline() # read a byte string
if line:
string = line.decode() # convert the byte string to a unicode string
res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
data.append(res) #append readings to store
# print(res)
x0= j*0.9
x.append(x0)
# print(x0)
if j<4000:
y = (5/4000)*j
else:
y = (-5/4000)*j + 10
y0.append(y)
data = [val for sublist in data for val in sublist]
# ax.plot(x,y0,color='r')
break
# print(y0)
ser.close()
takeread = Button(graph_frame,
text="Take Readings",
command=threading.Thread(target=takeRead).start,
font=20,
width=20)
takeread.pack(side=BOTTOM)
when I use this code then i can only run it one time as 'thread can only be started once'.
So, I tried another approach, that to run a thread and inside the thread one conditional statement to take data.
I want that, when the button is clicked then the data is taken. if button is pressed multiple time then function should work multiple time.
def takeRead():
global takereadings
def takereadings():
takereading = True
while takereading == True:
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM7" # "COM#" this must match with connected port
ser.open() #opening serial port
global data, x, y0
y0 = []
data = []
x = []
for j in range(8000):
line = ser.readline() # read a byte string
if line:
string = line.decode() # convert the byte string to a unicode string
res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
data.append(res) #append readings to store
# print(res)
x0= j*0.9
x.append(x0)
# print(x0)
if j<4000:
y = (5/4000)*j
else:
y = (-5/4000)*j + 10
y0.append(y)
data = [val for sublist in data for val in sublist]
# ax.plot(x,y0,color='r')
break
# print(y0)
ser.close()
takereading = False
threading.Thread(target=takeRead).start()
takeread = Button(graph_frame,
text="Take Readings",
command=takereadings,
font=20,
width=20)
takeread.pack()
I am using thread to stop GUI from freezing.