6 def __init__(self
, serial
, timeout
=2, delimiters
=["\n", "CCC"]):
10 self
.set_delimiters(delimiters
)
12 def encode(self
, data
):
14 return data
.encode(encoding
)
18 self
.serial
.reset_input_buffer()
19 self
.buf
= bytearray()
21 def set_serial(self
, serial
):
24 def set_timeout(self
, timeout
):
25 self
.timeout
= timeout
27 def set_delimiters(self
, delimiters
):
29 bytes(d
, encoding
) if type(d
) == str else d
for d
in delimiters
]
31 def read_line(self
, timeout
=None):
32 if timeout
is None or timeout
<= 0.:
33 timeout
= self
.timeout
36 for d
in self
.delimiters
:
41 while not has_delimiter() and ((time
.time() - start
) < timeout
):
42 i
= max(0, min(2048, self
.serial
.in_waiting
))
43 data
= self
.serial
.read(i
)
49 for delimiter
in self
.delimiters
:
50 i
= buf
.find(delimiter
)
52 offset
= i
+len(delimiter
)
54 self
.buf
= buf
[offset
:]
55 return self
.__convert
_to
_str
(r
)
59 # Timeout, reset buffer and return empty string
60 #print("TIMEOUT! Got:\n>>>>>>\n{}\n<<<<<<\n".format(buf))
61 self
.buf
= bytearray()
64 def write(self
, data
):
66 data
= self
.encode(data
)
67 cnt
= serial
.write(data
)
70 def write_str(self
, data
):
71 self
.write(data
+'\r\n')
73 def __convert_to_str(self
, data
):
75 return data
.decode(encoding
)
76 except UnicodeDecodeError: