-
Notifications
You must be signed in to change notification settings - Fork 4
/
serial_samples.py
62 lines (51 loc) · 2.17 KB
/
serial_samples.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
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Reads and parses lines from a serial device.
Typically from an Arduino. Lines are expected to follow the InfluxDB's line
protocol format (with the difference that the timestamp is allowed to be
missing).
"""
import logging
from typing import BinaryIO, Generator
def SkipUntilNewLine(handle: BinaryIO):
"""Skips data until a new-line character is received.
This is needed so that the first sample is read from a complete line.
"""
logging.debug("Skipping until the end of a new line.")
while not handle.readline(4096).endswith(b"\n"):
pass
class LineOverflowError(IOError):
"""Thrown when a line longer than a given limit is received."""
def __init__(self, line: bytes, max_line_length: int):
if not line:
message = "Timeout on device inactivity, no data received"
elif len(line) >= max_line_length:
message = "Read line overflow: {0!r}".format(line)
else:
message = "Read timeout; received incomplete line: {0!r}".format(
line)
super().__init__(message)
def SerialLines(handle: BinaryIO,
max_line_length: int) -> Generator[bytes, None, None]:
"""A generator that yields lines from a configured serial line.
Will never exit normally, only with an exception when there is an error
in the serial communication.
"""
SkipUntilNewLine(handle)
while True:
line: bytes = handle.readline(max_line_length)
logging.debug("Received line %r", line)
if not line.endswith(b"\n"):
raise LineOverflowError(line, max_line_length)
yield line.rstrip()