1

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?

my code:

import threading
import serial

connected = False
port = "COM3"
baud = 115200

serial_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()
murat
  • 11
  • 2
  • 1
    These articles will be relevant. [Python Serial port event](https://stackoverflow.com/q/62836625/9014308), [PySerial non-blocking read loop](https://stackoverflow.com/q/17553543/9014308), [Non-blocking, multi-threaded example:](https://stackoverflow.com/a/53344690/9014308) – kunif Sep 14 '20 at 14:44

0 Answers0