@@ 2,31 2,27 @@
import sys
import serial
import argparse
+import contextlib
import compress_ref
-parser = argparse.ArgumentParser()
+parser = argparse.ArgumentParser(description='Capture raw stream from serial port')
parser.add_argument('--baud', '-b', type=int, default=115200, help='Serial baud rate')
-parser.add_argument('--output', '-o', help='Output pgms with this base name')
+parser.add_argument('--output', '-o', help='Output stream to file')
parser.add_argument('port', help='Serial port device')
args = parser.parse_args()
-frame_count = 0
-frame_buf = []
+with contextlib.ExitStack() as stack:
+ p = stack.enter_context(serial.Serial(args.port, args.baud, timeout=1))
+ if args.output:
+ f = stack.enter_context(open(args.output, 'wb'))
+ else:
+ f = sys.stdout.buffer
-codec = compress_ref.CODEC(160, 144, 4)
-
-with serial.Serial(args.port, args.baud) as p:
+ # Trigger capture
p.write(0xFF)
- def gen():
- while True:
- yield p.read()[0]
+
+ # Stream serial port directly to output
while True:
- for line in codec.decompress(gen(), verbose=True):
- if args.output:
- frame_buf.append(line)
- if len(frame_buf) == 144:
- compress_ref.dump_pgm('{}{:03d}.pgm'.format(args.output, frame_count), frame_buf)
- frame_count += 1
- frame_buf = []
- print('{:080x}'.format(line))
+ f.write(p.read(4096))
+ f.flush()