I am working an application where I read two sensor values in every 200ms and show values in Tkinter window . My issue is in my application I have to response request from master PC through serial port (So this is an interrupt driven application ).But as per my knowledge there is no serial interrupt detection in python.
So I am in confusion how to implement this logic. Any kind of suggestion will be heartily accepted .Thank you
I am trying with threading without any success.
Here is my threading code demo:
import serial
import tkinter as tk
import threading
serialport= serial.Serial(port='/dev/ttyS0',baudrate=9600)
i2c = busio.I2C(board.SCL,board.SDA)
rate = 32
ads = ADS.ADS1115(i2c,data_rate=rate)
ads.gain =2
chan = AnalogIn(ads,ADS.P0,ADS.P1)
chan_1 = AnalogIn(ads,ADS.P2)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("500x300")
def reqst_response():
global chan,chan_1
print("waiting for data")
while True:
if serialport.inWaiting():
request = serialport.readline(serialport.inWaiting())
request = request.decode('utf-8')
print(request)
if str(request) == "*00T%":
print("ok sending data")
response = str("*00Tx")+str(chan.value)+str("%")
print(response)
serialport.write(response)
def update():
global chan, chan_1
var.set(chan.value)
var1.set(chan_1.value)
root.after(200,update)
# Torque
"torque show label"
#rpm
" rpm show label"
#start Button
Bt1 = tk.Button(root, text="start", fg="green",
command=threading.Thread(target=reqst_response).start())
Bt1.pack()
Bt1.place(x=10, y=2)
root.after(200,update)
root.mainloop()