#!/usr/bin/python import serial s = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=5) data = "" while True: chunk = s.read(16) if chunk: data += chunk elif data: # 5 second timeout after data means we stop break print "%5i bytes received\r" % len(data), print "\nReceived %i bytes total" % len(data) out = file("out.bin", "wb") for i in range(0, len(data), 2): val = int(data[i:i+2], 16) out.write(chr(val))