-
Notifications
You must be signed in to change notification settings - Fork 0
/
deccsv2serial.py
executable file
·51 lines (37 loc) · 1.27 KB
/
deccsv2serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
'''
A minimal script for writing binary data to serial devices.
- takes comma-separated decimal values on stdin (terminate lines with \n),
converts them to bytes and writes them to the serial device
- responses from the device are dumped to stdout
- runs infinitely until receiving the string "quit" instead of a CSV line
01/2015
'''
import sys
from serial import Serial
# parse arguments
if len(sys.argv) >= 3:
( device, baud ) = ( sys.argv[1], sys.argv[2] )
else:
print "Usage:", sys.argv[0], " DEVICE BAUD"
exit(1)
# open device
with Serial( device, baud, bytesize=8, parity='N', timeout=1 ) as serial:
while True:
# read next line from stdin
csv = sys.stdin.readline().strip()
# check if input is a command to quit
if csv == "quit": break
# get list from csv
decimals = csv.split( ',' )
# make binary string
data = ''
for decimal in decimals: data += chr(int(decimal))
# send to device
serial.write( data )
# read status
status = serial.readline().strip()
# print status to stdout and flush
sys.stdout.write( status + "\n" );
sys.stdout.flush();