Created
April 10, 2017 17:06
-
-
Save a-andreyev/1455f8cc024780188a6687b8399f7015 to your computer and use it in GitHub Desktop.
pyserial non-block read to stdout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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