forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserio
More file actions
executable file
·90 lines (71 loc) · 2.46 KB
/
serio
File metadata and controls
executable file
·90 lines (71 loc) · 2.46 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python
# serio:
# A simple terminal communications program to talk over a serial line.
# This grew out of my ardmonitor script for talking to Arduinos.
# It's *just a proof of concept*; it doesn't work very well.
# Sometimes it randomly seems to skip characters or add delays.
# Copyright 2013 by Akkana Peck; share and enjoy under the GPLv2 or later.
import sys
import serial, select
import tty, termios
import signal
class SerIO() :
def __init__(self) :
self.old_settings = termios.tcgetattr(sys.stdin.fileno())
def run(self, baud=115200) :
# Port may vary, so look for it:
baseports = ['/dev/ttyUSB']
self.ser = None
for baseport in baseports :
if self.ser : break
for i in xrange(0, 8) :
try :
port = baseport + str(i)
self.ser = serial.Serial(port, baud, timeout=1)
print "Opened", port
break
except :
self.ser = None
pass
if not self.ser :
print "Couldn't open a serial port"
sys.exit(1)
signal.signal(signal.SIGINT, self.signal_handler)
tty.setraw(sys.stdin.fileno())
self.ser.flushInput()
while True :
# Check whether the user has typed anything:
inp, outp, err = select.select([sys.stdin, self.ser], [], [], .2)
# Check for user input:
if sys.stdin in inp :
line = sys.stdin.read(1)
self.ser.write(line)
# check for output from the other end:
if self.ser in inp :
line = self.ser.read(1)
sys.stdout.write(line)
sys.stdout.flush()
def signal_handler(self, signal, frame):
print 'You pressed Ctrl+C! Cleaning up.'
self.cleanup()
sys.exit(0)
def cleanup(self) :
print "Calling cleanup"
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN,
self.old_settings)
serio = SerIO()
try :
if len(sys.argv) > 1 :
print "Using", sys.argv[1], "baud"
serio.run(baud=sys.argv[1])
else :
serio.run()
except serial.SerialException :
print "Disconnected (Serial exception)"
except IOError :
print "Disconnected (I/O Error)"
except KeyboardInterrupt :
print "Interrupt"
finally :
print "finally: Cleaning up"
serio.cleanup()