Function triggered when data comes to serial port in python

%3CLINGO-SUB%20id%3D%22lingo-sub-1664130%22%20slang%3D%22en-US%22%3EFunction%20triggered%20when%20data%20comes%20to%20serial%20port%20in%20python%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1664130%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22s-prose%20js-post-body%22%3E%3CP%3EI%20am%20checking%20the%20incoming%20data%20by%20reading%20continuously%20in%20while%20true%20in%20a%20separate%20thread.%20I%20don't%20want%20it%20to%20work%20constantly.%20Is%20there%20a%20function%20that%20will%20only%20be%20triggered%20when%20data%20arrives%20in%20%26nbsp%3Bpython%3F%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fbit.ly%2F3iZ3cZD%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fbit.ly%2F3iZ3cZD%3C%2FA%3E%3C%2FP%3E%3CP%3Emy%20code%3A%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22kwd%22%3Eimport%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20threading%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Eimport%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20serial%0A%0Aconnected%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3EFalse%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eport%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22COM3%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ebaud%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E115200%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eserial_port%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20serial%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3ESerial%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eport%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20baud%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20timeout%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%0A%0A%3CSPAN%20class%3D%22kwd%22%3Edef%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20handle_data%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Edata%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A(%3C%2Fimg%3E%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Eprint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Edata%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%0A%0A%3CSPAN%20class%3D%22kwd%22%3Edef%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20read_from_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eser%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A(%3C%2Fimg%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20select_read%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eserial_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20serin%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20ser%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eread%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20connected%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3ETrue%3C%2FSPAN%3E%0A%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Ewhile%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3ETrue%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20%20%20%20reading%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20serial_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ein_waiting%0A%20%20%20%20%20%20%20%20%20%20%20reading%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20ser%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ereadline%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E().%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Edecode%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Eif%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20len%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ereading%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26gt%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E5%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20handle_data%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ereading%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%0A%0A%0A%3CSPAN%20class%3D%22kwd%22%3Edef%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20set_data%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3A%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Ewhile%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3ETrue%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Eprint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22input%3A%20%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20num1%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20str%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Einput%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E())%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20serial_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ewrite%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Enum1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eencode%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E())%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ethread%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20threading%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EThread%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Etarget%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eread_from_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20args%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eserial_port%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C))%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ethread2%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20threading%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EThread%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Etarget%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eset_data%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ethread%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Estart%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ethread2%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Estart%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3C%2FSPAN%3E%3C%2FPRE%3E%3C%2FDIV%3E%3CDIV%20class%3D%22post-taglist%20grid%20gs4%20gsy%20fd-column%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Visitor

I am checking the incoming data by reading continuously in while true in a separate thread. I don't want it to work constantly. Is there a function that will only be triggered when data arrives in  python?

https://bit.ly/3iZ3cZD

my code:

import threadingimport serial

connected = Falseport = "COM3"baud = 115200serial_port = serial.Serial(port, baud, timeout=0)

def handle_data(data):
    print(data)

def read_from_port(ser):        select_read(serial_port)        serin = ser.read()        connected = True

        while True:           reading = serial_port.in_waiting
           reading = ser.readline().decode()
           if len(reading) > 5:       
                handle_data(reading)


def set_data():
    while True:
        print("input: ")        num1 = str(input())        serial_port.write(num1.encode());thread = threading.Thread(target=read_from_port, args=(serial_port,))thread2 = threading.Thread(target=set_data)thread.start()thread2.start()
0 Replies