12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #!/usr/bin/python3 -u
- #
- import serial
- import sys
- import argparse
- from datetime import datetime
- serialPort='/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0'
- serialBaud=57600
- serialTimeout=1
- doReset = False
- enableMonitor = False
- ser = serial.Serial(port=serialPort,
- baudrate = serialBaud,
- parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS,
- timeout=serialTimeout)
- parser = argparse.ArgumentParser()
- parser.add_argument('--reset',
- help='reset board via serial command, parameter is timeout in ms to reset.')
- parser.add_argument('--monitor', default=False,
- help='Monitor serial output.', action='store_true')
- parser.add_argument('--addts', default=False,
- help='Add timestamp to monitor output.', action='store_true')
- args = parser.parse_args()
-
- if args.reset:
- if int(args.reset) >= 1:
- doReset = True
- delayTime = args.reset
- if doReset:
- cmdStr = 'reset ' + str(delayTime) + '\n'
- #print('cmd: >' + cmdStr + '<')
- cmdStr = cmdStr.encode('utf-8')
- ser.write(cmdStr)
- if args.monitor:
- while True:
- #ser.flushInput()
- #read buffer until cr/lf
- serLine = ser.readline().strip()
- # catch exception on invalid char coming in: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf4 in position 6: ordinal not in range(128)
- try:
- serLine = serLine.decode('ascii')
- except:
- serLine = ""
-
- if(serLine):
- if args.addts:
- #print (datetime.now().isoformat(), serLine) #Echo the serial buffer bytes up to the CRLF back to screen
- print ("[" + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]) + "]:", serLine)
- else:
- print (serLine) #Echo the serial buffer bytes up to the CRLF back to screen
-
|