Skip to content

Instantly share code, notes, and snippets.

@a-andreyev
Created April 10, 2017 17:06
Show Gist options
  • Select an option

  • Save a-andreyev/1455f8cc024780188a6687b8399f7015 to your computer and use it in GitHub Desktop.

Select an option

Save a-andreyev/1455f8cc024780188a6687b8399f7015 to your computer and use it in GitHub Desktop.
pyserial non-block read to stdout
#!/usr/bin/python
import serial
import threading
port = '/dev/ttyUSB0'
baud = 9600
serial_port = serial.Serial(port, baud, timeout=120)
def handle_data(data):
if len(data):
print(data)
def read_from_port(ser):
connected = False
while not connected:
#serin = ser.read()
connected = True
while True:
reading = ser.readline().decode(errors='replace')
handle_data(reading)
thread = threading.Thread(target=read_from_port, args=(serial_port,))
thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment