import sys import time import serial def conn_factory(devname='/dev/ttyUSB0'): conn = serial.Serial(devname, baudrate=460800, bytesize=7, parity='E', stopbits=1, timeout=2) return conn if __name__ == '__main__': try: fout = sys.argv[1] except IndexError: print('provide output filename (or - for STDOUT) as first argument') fout = '-' if fout == '-': fout = sys.stdout else: fout = open(fout, 'a') conn = conn_factory() buff = '' conn.write('XD') # X - start measurements, D - report cumulative dose values while True: buff = conn.readline() fout.write(time.strftime('%F %T ') + buff) fout.flush() try: time.sleep(0.1) except KeyboardInterrupt: break fout.close()