Created
July 30, 2017 03:29
-
-
Save electrickite/2a9f6b72982f47a33ce5c80eac2fcd77 to your computer and use it in GitHub Desktop.
Write lines from a serial device to stdout
This file contains 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/env python | |
# Writes lines from a serial device to stdout. Use Ctrl+C to exit. | |
# Requires pySerial | |
# | |
# Usage: | |
# python serialwriter.py DEVICE [BAUD_RATE] | |
# | |
# Example: | |
# $ python serialwriter.py /dev/serial.device 115200 | |
import sys | |
import serial | |
serial_port = sys.argv[1] | |
if len(sys.argv) < 3: | |
baud_rate = 115200 | |
else: | |
baud_rate = sys.argv[2] | |
ser = serial.Serial(serial_port, baud_rate) | |
try: | |
while True: | |
line = ser.readline() | |
line = line.decode("utf-8") | |
sys.stdout.write(line) | |
sys.stdout.flush() | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment