Skip to content

Instantly share code, notes, and snippets.

@AlphaSierraHotel
Created January 12, 2021 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlphaSierraHotel/bf5d02b0428e9101252c5c7c120e7f82 to your computer and use it in GitHub Desktop.
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.
#!/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