-
Notifications
You must be signed in to change notification settings - Fork 3
/
recoverserial.py
60 lines (50 loc) · 1.59 KB
/
recoverserial.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
#!/usr/bin/env python
import logging
import serial
import time
from threading import Lock
log = logging.getLogger("recoverserial")
class RecoverSerial(serial.Serial):
def log_error(self, e):
msg = str(e)
if msg != self.lasterror:
log.error(msg)
self.lasterror = msg
def __init__(self, *args, **kwargs):
self.object_nr = 0
self.recover_lock = Lock()
self.lasterror = None
while True:
try:
return super(RecoverSerial, self).__init__(*args, **kwargs)
except serial.SerialException as e:
self.log_error(e)
time.sleep(1)
def recover(self, nr):
self.recover_lock.acquire()
while nr == self.object_nr:
time.sleep(1)
try:
self.close()
self.open()
self.object_nr += 1
except serial.SerialException as e:
self.log_error(e)
pass
self.recover_lock.release()
def readline(self, *args, **kwargs):
nr = self.object_nr
try:
return super(RecoverSerial, self).readline(*args, **kwargs)
except serial.SerialException as e:
self.log_error(e)
self.recover(nr)
return ''
def write(self, *args, **kwargs):
while True:
nr = self.object_nr
try:
return super(RecoverSerial, self).write(*args, **kwargs)
except serial.SerialException as e:
self.log_error(e)
self.recover(nr)