-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge.py
executable file
·67 lines (59 loc) · 2.16 KB
/
bridge.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Reliable version of pyserial
"""
from serial import Serial, SerialException
import time
class Connect:
"""
Serial port connection.
The interface is identical to the pyserial Serial class,
with the addition of a *retry* keyword which indicates that
the serial connection should be reopened automatically.
Use *retry="write"* to reopen on write, *retry="read"* to reopen
on read, or the default *retry="readwrite"* to reopen on read
and write failure.
This allows the user to unplug the serial cable and plug it
back in again, or equivalently, shut off the serial device
and turn it back on again, for example, as a way to reset
its state.
Only the *readline* and *write* methods are supported.
"""
def __init__(self, *args, **kw):
self.retry = kw.pop('retry','readwrite')
self.args = args
self.kw = kw
self.channel = None
def write(self, str):
while True:
try:
if self.channel is None:
self.channel = Serial(*self.args, **self.kw)
self.channel.flushInput()
return self.channel.write(str)
except SerialException, exc:
if self.channel is not None:
try: self.channel.close()
except: pass
self.channel = None
print exc
if "write" not in self.retry:
return 0
print "Restarting in 1s..."
time.sleep(1)
def readline(self):
while True:
try:
if self.channel is None:
self.channel = Serial(*self.args, **self.kw)
self.channel.flushOutput()
return self.channel.readline()
except SerialException, exc:
if self.channel is not None:
try: self.channel.close()
except: pass
self.channel = None
print exc
if "read" not in self.retry:
return ""
print "Restarting in 1s..."
time.sleep(1)