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()

    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')
                if str(request) == "*00T%":
                    print("ok sending data")
                    response = str("*00Tx")+str(chan.value)+str("%")
    def update():
        global chan, chan_1
    # Torque
     "torque show label"

    " rpm show label"

    #start Button
    Bt1 = tk.Button(root, text="start", fg="green", 
    Bt1.place(x=10, y=2)

  • 2
    Not Pi specific. Have a thread for the the serial link. It just needs to loop reading a message and setting a flag to say the message has arrived. The main thread can loop checking and clearing the flag. – joan Oct 22 '20 at 19:48
  • @joan,what you told I am not able to understand it. Can you please elaborate little bit – ram Oct 23 '20 at 3:42

This is very rough but a bit long for a comment so I understand if it gets voted down as an answer :-)

I'm also unsure how the Tkinter processes may impact the threads below.

Create a first in - first out queue (FIFO Queue) using the built in class.

Create a thread using the built in class to monitor the serial port. Once it has valid data, store that value on the queue using Queue.put

I would use a Python dictionary (simpler than JSON ) to pass both details at once onto the queue personally

Create another thread that waits till data is on the queue (make sure you have a 'sleep' if this is a loop so other processing will happen) using Queue.empty() and once it's got a value on the queue, extract it using Queue.get then process it.

Have a look at one of the old discussions I helped in on the RPF forum here on a similar issue for links to demo code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.