Created
January 12, 2021 01:56
-
-
Save AlphaSierraHotel/bf5d02b0428e9101252c5c7c120e7f82 to your computer and use it in GitHub Desktop.
Read binary data from a serial port and output each byte in hexadecimal.
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/python | |
import serial | |
import signal | |
import sys | |
# Specify your communication parameters here | |
ser = serial.Serial( | |
port="/dev/ttyUSB0", | |
baudrate = 9600, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS, | |
timeout=0.004011 | |
) | |
# To handle keyboard interrupt | |
def sigint_handler(signal, frame): | |
print('\nInterrupted') | |
sys.exit(0) | |
signal.signal(signal.SIGINT, sigint_handler) | |
ser.reset_input_buffer() | |
while True: | |
s = ser.read(1) | |
if len(s) == 1: | |
print(format(ord(s),'02X')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment