#!/usr/bin/python # -*- coding: utf-8 -*- # vim: set ts=4 sw=4 et : # # grabserial - program to read a serial port and send the data to stdout # # Copyright 2006,2020 Sony Corporation # # This program is provided under the Gnu General Public License (GPL) # version 2 ONLY. This program is distributed WITHOUT ANY WARRANTY. # See the LICENSE file, which should have accompanied this program, # for the text of the license. # # 2020-02-08 by Tim Bird # Started 2006-09-07 by Tim Bird # # To do: # * support interrupting capture with Ctrl-C # * somewhere along the line, this feature got lost # * (maybe check the exception handler and threading in the main loop?) # * buffer output chars?? # * add optional value to -a to limit number of restarts? # * restart based on received bytes? # # CHANGELOG: # 2021.01.13 - Version 2.1.6 # - make it so that --nodelta applies to regular timing as well # 2021.01.12 - Version 2.1.5 # - fixed issue with possibly flushing the wrong output file # 2021.01.12 - Version 2.1.4 # - add support for hex output # 2020.05.22 - Version 2.1.3 # - remove delay_start feature (not needed by user using rotation rounding) # 2020.05.14 - Version 2.1.2 # - add rotation time rounding feature. If a units suffix is added # to the rotation time, then rotate on an even multiple of that time. # 2020.05.04 - Version 2.1.1 # - add split-lines feature (-z) # 2020.05.01 - Version 2.1.0 # - add explicit log rotation feature (-R). This should help # avoid data loss when doing a log rotation. Previous method using # a restart (e.g. 'e 3600 -a') closed and reopend the serial port. # 2020.02.08 - Version 2.0.4 # - de-reference symlinks in device_exists() # - use stderr for error output, handle more errors, don't show usage on # errors (it's too big now) # - fix byte-string conversion bug introduced by command_mode changes # (Dang this unicode/bytecode handling in Python 3.0 is a pain!) # - rename instantpat to inlinepat (all logic is the same) # - preserve old --instantpat argument name for compatibility # 2020.02.07 - Version 2.0.0 # - add command mode (-C) to support using grabserial for showing # output from executing a single command over the serial port # 2019.09.03 - Version 1.9.9 # - fixed a bunch of pylint errors, and disabled some false positives # with inline pylint directives # - this included replacing some bare exceptions with UnicodeEncodeError # 2018.08.20 - Version 1.9.8 # - try to fix unicode handling (yet again) # - some work based on pull request submitted by 'modbw' on github # - handle EOFError during read_input, in case of pipe closure # 2018.01.03 - Version 1.9.6 # - add patches from Ilya Kuzmich to fix python3 issues, # - update test.sh with python linters and other improvements # - fix code to remove flake8 and pylint issues # 2017.06.13 - Version 1.9.5 - add -a to restart after time expired or # pattern matched. # - add strftime arguments to -o. # - add -Q to silence stdout when -o is active. # 2016.09.29 - Version 1.9.4 - add thread for sending user input to target # by zqb-all on github # 2016.09.06 - clean up tabs, and add vim modeline for 4-column tabs # grabserial should always run with python -tt grabserial # 2016.08.31 - add microsecond precision when using system Time (-T) option # 2016.08.30 - Version 1.9.3 - allow forcing the baudrate with -B # 2016.07.01 - Version 1.9.2 - change how version is stored # 2016.05.10 - Version 1.9.1 - allow skipping the tty check with -S # 2016.05.10 - Version 1.9.0 - support use as a python module # Note that the main module routine will be grabserial.grab(args,[outputfd]) # where args is a list of command-line-style args # as they would be passed using the standalone program. e.g. # grabserial.grab(None, ["-d", "/dev/ttyUSB0", "-v"]) # output from the serial port (with timing data) is sent to outputfd # 2015.04.23 - Version 1.8.1 - remove instructions for applying LICENSE text # to new files, and add no-warranty language to grabserial. # 2015.03.10 - Version 1.8.0 - add -o option for saving output to a file # add -T option for absolute times. Both contributed by ramaxlo # 2015.03.10 - Version 1.7.1 - add line feed to inlinepat result line # 2014.09.28 - Version 1.7.0 - add option for force reset for USB serial # contributed by John Mehaffey # 2014.01.07 - Version 1.6.0 - add option for exiting based on a # mid-line pattern (quitpat). Simeon Miteff # 2013.12.19 - Version 1.5.2 - verify Windows ports w/ serial.tools.list_ports # (thanks to Yegor Yefromov for the idea and code) # 2013.12.16 - Version 1.5.1 - Change my e-mail address # 2011.12.19 - Version 1.5.0 - add options for mid-line time capture # (inlinepat) and base time from launch of program instead of # first char seen (launchtime) - contributed by Kent Borg # 2011-09-24 - better time output and time delta # Constantine Shulyupin # 2008-06-02 - Version 1.1.0 add support for sending a command to # the serial port before grabbing output # Use the module docstring as the usage text for the program """Grabserial reads data from a serial port, processes it, and outputs it. It is much more flexible than a simple 'cat' command. It keeps track of timing data, and can record when a pattern is seen in the data. It can show timing data for every line received. It can save the data to a file and can quit or restart based on a timeout or when a pattern is seen. It can split the data into multiple files based on timeout or a pattern. It supports interactive writing to the serial port during data collection. It is useful for things like: 1. capturing and annotating data from a serial port 2. logging data to multiple files 3. timing events in a stream of data (such as how long it takes a Linux kernel to boot) 4. executing commands to a serial console. Options: -h, --help Print this message -d, --device= Set the device to read (default '/dev/ttyS0') -b, --baudrate= Set the baudrate (default 115200) -B Force the baudrate to the indicated value (grabserial won't check if the baudrate is legal) -w, --width= Set the data bit width (default 8) -p, --parity= Set the parity (default N) -s, --stopbits= Set the stopbits (default 1) -x, --xonxoff Enable software flow control (default off) -r, --rtscts Enable RTS/CTS flow control (default off) --rts= Explicitly set RTS to 'True' (default) or 'False' --dtr= Explicitly set DTR to 'True' (default) or 'False' -f, --force-reset Force pyserial to reset device parameters -e, --endtime= End the program after the specified seconds have elapsed. -c, --command= Send a command to the port before reading -t, --time Print time for each line received. The time is when the first character of each line is received by grabserial. -a, --again Restart application after -e expires, -q is triggered, or the serial device is disconnected. -R, --rotate=