Wentong Wu | b4449a0 | 2020-02-04 04:04:32 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019 Intel Corporation. |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | """ |
| 7 | Script to capture tracing data with UART backend. |
| 8 | """ |
| 9 | |
| 10 | import sys |
| 11 | import serial |
| 12 | import argparse |
| 13 | |
| 14 | def parse_args(): |
| 15 | global args |
| 16 | parser = argparse.ArgumentParser( |
| 17 | description=__doc__, |
Jamie McCrae | ec70444 | 2023-01-04 16:08:36 +0000 | [diff] [blame] | 18 | formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) |
Wentong Wu | b4449a0 | 2020-02-04 04:04:32 +0800 | [diff] [blame] | 19 | parser.add_argument("-d", "--serial_port", required=True, |
| 20 | help="serial port") |
| 21 | parser.add_argument("-b", "--serial_baudrate", required=True, |
| 22 | help="serial baudrate") |
| 23 | parser.add_argument("-o", "--output", default='channel0_0', |
| 24 | required=False, help="tracing data output file") |
| 25 | args = parser.parse_args() |
| 26 | |
| 27 | def main(): |
| 28 | parse_args() |
| 29 | serial_port = args.serial_port |
| 30 | serial_baudrate = args.serial_baudrate |
| 31 | output_file = args.output |
| 32 | try: |
| 33 | ser = serial.Serial(serial_port, serial_baudrate) |
| 34 | ser.isOpen() |
| 35 | except serial.SerialException as e: |
| 36 | sys.exit("{}".format(e)) |
| 37 | |
| 38 | print("serial open success") |
| 39 | |
| 40 | #enable device tracing |
| 41 | ser.write("enable\r".encode()) |
| 42 | |
| 43 | with open(output_file, "wb") as file_desc: |
| 44 | while True: |
| 45 | count = ser.inWaiting() |
| 46 | if count > 0: |
| 47 | while count > 0: |
| 48 | data = ser.read() |
| 49 | file_desc.write(data) |
| 50 | count -= 1 |
| 51 | |
| 52 | ser.close() |
| 53 | |
| 54 | if __name__=="__main__": |
| 55 | try: |
| 56 | main() |
| 57 | except KeyboardInterrupt: |
| 58 | print('Data capture interrupted, data saved into {}'.format(args.output)) |
| 59 | sys.exit(0) |