Created
July 3, 2022 15:42
-
-
Save InputBlackBoxOutput/944dd02cfef0463969c3f898670a1107 to your computer and use it in GitHub Desktop.
Print serial data sent over USB from a development board
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
import sys | |
import serial | |
import serial.tools.list_ports as port_list | |
def serial_communication(): | |
ports = list(port_list.comports()) | |
if ports == []: | |
print("No open ports found!") | |
sys.exit() | |
print(f"Connected to {ports[0].device}") | |
port = ports[0].device | |
baudrate = 115200 | |
serial_port = serial.Serial(port=port, baudrate=baudrate, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE) | |
serial_string = "" | |
while True: | |
try: | |
char = str(serial_port.read())[2:-1] | |
serial_string += char | |
if char == "\\n": | |
serial_string = serial_string.split('\\r')[0] | |
print(serial_string[:1]) | |
serial_string = "" | |
except serial.serialutil.SerialException: | |
print("Serial disconnected") | |
break | |
except KeyboardInterrupt: | |
break | |
serial_port.close() | |
if __name__ == '__main__': | |
serial_communication() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment