Last active
April 26, 2021 10:17
-
-
Save bluesquall/68d78ea0bc55249616b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """Player for general text log files.""" | |
| import serial | |
| import time | |
| __author__ = "M Jordan Stanway" | |
| __license__ = "MIT" | |
| def replay_text_file(infile, port='/dev/ttyS0', baudrate=9600, | |
| period=0.1, loop=False, verbosity=0 ): | |
| """Replay a text log file on a serial port. | |
| """ | |
| sio = serial.Serial(port, baudrate) | |
| with open(infile, 'rt', newline='') as f: | |
| for line in f: | |
| line = line.strip() + '\r\n' | |
| if verbosity > 0: print(line.encode()) | |
| sio.write(line.encode()) | |
| sio.flush() | |
| time.sleep(period) | |
| if __name__ == "__main__": | |
| import argparse | |
| parser = argparse.ArgumentParser(description='Text log file replayer') | |
| parser.add_argument('-V', '--version', action='version', | |
| version='%(prog)s 0.0.1', | |
| help='display version information and exit') | |
| parser.add_argument('infile', metavar='filename', | |
| type=str, help='input file (.txt)') | |
| parser.add_argument('-p', '--port', default='/dev/ttyS0', | |
| type=str, help='serial port to replay on') | |
| parser.add_argument('-b','--baudrate', default=9600, | |
| type=int, help='baud rate for serial replay') | |
| parser.add_argument('-P', '--period', default=0.1, | |
| type=float, help='approximate period for output (in seconds)') | |
| parser.add_argument('-L', '--loop', default=False, action='store_true', | |
| help='whether to loop indefinitely') | |
| parser.add_argument('-v','--verbosity', default=0, action='count', | |
| help='increase output') | |
| args = parser.parse_args() | |
| replay_text_file(**args.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment