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
25 from datetime
import datetime
,timezone
32 def __init__(self
, dev
, filename
, prefix
):
33 self
.filename
= filename
37 self
.f
= open(filename
, "wb+")
38 self
.serial
= serial
.Serial(dev
, 115200, timeout
=10)
40 self
.f
= open(filename
, "rb")
42 self
.byte_queue
= queue
.Queue()
43 self
.line_queue
= queue
.Queue()
45 self
.sentinel
= object()
48 self
.read_thread
= threading
.Thread(target
=self
.serial_read_thread_loop
, daemon
=True)
50 self
.read_thread
= threading
.Thread(target
=self
.serial_file_read_thread_loop
, daemon
=True)
51 self
.read_thread
.start()
53 self
.lines_thread
= threading
.Thread(target
=self
.serial_lines_thread_loop
, daemon
=True)
54 self
.lines_thread
.start()
56 # Thread that just reads the bytes from the serial device to try to keep from
57 # buffer overflowing it.
58 def serial_read_thread_loop(self
):
59 greet
= "Serial thread reading from %s\n" % self
.dev
60 self
.byte_queue
.put(greet
.encode())
64 self
.byte_queue
.put(self
.serial
.read())
65 except Exception as err
:
66 print(self
.prefix
+ str(err
))
67 self
.byte_queue
.put(self
.sentinel
)
70 # Thread that just reads the bytes from the file of serial output that some
71 # other process is appending to.
72 def serial_file_read_thread_loop(self
):
73 greet
= "Serial thread reading from %s\n" % self
.filename
74 self
.byte_queue
.put(greet
.encode())
77 line
= self
.f
.readline()
79 self
.byte_queue
.put(line
)
83 # Thread that processes the stream of bytes to 1) log to stdout, 2) log to
84 # file, 3) add to the queue of lines to be read by program logic
86 def serial_lines_thread_loop(self
):
89 bytes
= self
.byte_queue
.get(block
=True)
91 if bytes
== self
.sentinel
:
92 self
.read_thread
.join()
93 self
.line_queue
.put(self
.sentinel
)
96 # Write our data to the output file if we're the ones reading from
105 line
= line
.decode(errors
="replace")
107 time
= datetime
.now().strftime('%y-%m-%d %H:%M:%S')
108 print("{time} {prefix}{line}".format(time
=time
, prefix
=self
.prefix
, line
=line
), flush
=True, end
='')
110 self
.line_queue
.put(line
)
114 line
= self
.line_queue
.get()
115 if line
== self
.sentinel
:
116 self
.lines_thread
.join()
120 return iter(self
.get_line
, self
.sentinel
)
123 parser
= argparse
.ArgumentParser()
125 parser
.add_argument('--dev', type=str, help='Serial device')
126 parser
.add_argument('--file', type=str, help='Filename for serial output', required
=True)
127 parser
.add_argument('--prefix', type=str, help='Prefix for logging serial to stdout', nargs
='?')
129 args
= parser
.parse_args()
131 ser
= SerialBuffer(args
.dev
, args
.file, args
.prefix
or "")
132 for line
in ser
.lines():
133 # We're just using this as a logger, so eat the produced lines and drop
137 if __name__
== '__main__':