8ea9051fe1a3bab08e6170b16c71f5ae33d41318
3 # Copyright © 2020 Google LLC
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 def __init__(self
, dev
, filename
, prefix
):
31 self
.f
= open(filename
, "wb+")
33 self
.serial
= serial
.Serial(dev
, 115200, timeout
=10)
34 self
.byte_queue
= queue
.Queue()
35 self
.line_queue
= queue
.Queue()
37 self
.sentinel
= object()
39 self
.read_thread
= threading
.Thread(target
=self
.serial_read_thread_loop
, daemon
=True)
40 self
.read_thread
.start()
41 self
.lines_thread
= threading
.Thread(target
=self
.serial_lines_thread_loop
, daemon
=True)
42 self
.lines_thread
.start()
44 # Thread that just reads the bytes from the serial device to try to keep from
45 # buffer overflowing it.
46 def serial_read_thread_loop(self
):
47 greet
= "Serial thread reading from %s\n" % self
.dev
48 self
.byte_queue
.put(greet
.encode())
52 self
.byte_queue
.put(self
.serial
.read())
53 except Exception as err
:
54 print(self
.prefix
+ str(err
))
55 self
.byte_queue
.put(self
.sentinel
)
58 # Thread that processes the stream of bytes to 1) log to stdout, 2) log to
59 # file, 3) add to the queue of lines to be read by program logic
61 def serial_lines_thread_loop(self
):
64 bytes
= self
.byte_queue
.get(block
=True)
66 if bytes
== self
.sentinel
:
67 self
.read_thread
.join()
68 self
.line_queue
.put(self
.sentinel
)
71 # Write our data to the output file
78 line
= line
.decode(errors
="replace")
79 print(self
.prefix
+ line
, flush
=True, end
='')
80 self
.line_queue
.put(line
)
84 line
= self
.line_queue
.get()
85 if line
== self
.sentinel
:
86 self
.lines_thread
.join()
90 return iter(self
.get_line
, self
.sentinel
)
93 parser
= argparse
.ArgumentParser()
95 parser
.add_argument('--dev', type=str, help='Serial device', required
=True)
96 parser
.add_argument('--file', type=str, help='Filename to output our serial data to')
97 parser
.add_argument('--prefix', type=str, help='Prefix for logging serial to stdout', nargs
='?')
99 args
= parser
.parse_args()
101 ser
= SerialBuffer(args
.dev
, args
.file, args
.prefix
or "")
102 for line
in ser
.lines():
103 # We're just using this as a logger, so eat the produced lines and drop
107 if __name__
== '__main__':