From bc08a0cb8c0cfc1921dfceafc56f5b070fc9bb9f Mon Sep 17 00:00:00 2001 From: Jakub Zika Date: Wed, 21 Sep 2011 16:42:27 +0200 Subject: [PATCH] Added script which sends its stdin to serial * It groups its input into batches of 3*(# of boxes) bytes --- host_python/send_to_serial.py | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 host_python/send_to_serial.py diff --git a/host_python/send_to_serial.py b/host_python/send_to_serial.py new file mode 100755 index 0000000..f9b4d3b --- /dev/null +++ b/host_python/send_to_serial.py @@ -0,0 +1,70 @@ +#!/usr/bin/python +# vim:et:sw=4:ts=4:sts=4 + +import sys +import serial +import getopt + +def print_usage(): + print '''\ +USAGE: + %s [-h | [-n number] [-b speed] serial] +OPTIONS: + serial write output to serial device + -b speed speed of the serial device + -n number number of controlled boxes + -h --help show this help +''' % sys.argv[0] + +def main(): + try: + opts, args = getopt.getopt(sys.argv[1:], 's:b:n:h', ['help']) + except getopt.GetOptError: + print_usage() + return 1 + speed = 38400 + number = 10 + show_help = False + for k, v in opts: + if k == '-n': + if not v.isdigit(): + print_usage() + return 1 + number = int(v) + elif k == '-b': + if not v.isdigit(): + print_usage() + return 1 + speed = int(v) + elif k == '-h' or k == '--help': show_help = True + if show_help: + print_usage() + return 0 + if len(args) != 1: + print_usage() + return 1 + + try: + output_stream = serial.Serial(args[0], speed) + except serial.serialutil.SerialException: + print 'Could not open the serial device' + return 1 + + try: + while True: + data = '' + to_read = number*3 + while to_read > 0: + read = sys.stdin.read(to_read) + if len(read) == 0: break + to_read -= len(read) + data += read + if len(read) == 0: break + output_stream.write(data) + output_stream.flush() + except IOError: + pass + + return 0 + +sys.exit(main())