mirror of
https://github.com/brmlab/ledbar.git
synced 2025-06-09 13:24:01 +02:00
Added script which sends its stdin to serial
* It groups its input into batches of 3*(# of boxes) bytes
This commit is contained in:
parent
1bba88fe52
commit
bc08a0cb8c
1 changed files with 70 additions and 0 deletions
70
host_python/send_to_serial.py
Executable file
70
host_python/send_to_serial.py
Executable file
|
@ -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())
|
Loading…
Add table
Add a link
Reference in a new issue