Skip to content

Instantly share code, notes, and snippets.

@ilonajulczuk
Last active December 28, 2015 11:29
Show Gist options
  • Save ilonajulczuk/7493698 to your computer and use it in GitHub Desktop.
Save ilonajulczuk/7493698 to your computer and use it in GitHub Desktop.
import serial
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=str,
help="name of port",
default='/dev/ttyUSB0')
parser.add_argument("-o", "--output_filename", type=str,
help="filename of output file",
default='data.txt')
def main():
args = parser.parse_args()
port = args.port
output = args.output_filename
ser = serial.Serial(port, 9600, timeout=1)
with open(output, 'a+') as f:
while True:
line = ser.readline() # read a '\n' terminated line
f.write(line)
ser.close()
if __name__ == '__main__':
main()
@ilonajulczuk
Copy link
Author

Simple script for reading from serial.

Reads from serial on selected port and writes to file.

usage:

$ python from_serial_to_file.py [-h] [-p PORT] [-o OUTPUT_FILENAME]

optional arguments:
-h, --help show this help message and exit
-p PORT, --port PORT name of port
-o OUTPUT_FILENAME, --output_filename OUTPUT_FILENAME
filename of output file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment