diff --git a/README.md b/README.md index cf212f8..ef36091 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,8 @@ This is a basic command line utility to automate the Saleae Logic software. This tool can be used to automatically take a series of shorter captures over an extended time period. For example, if a 24 hour capture can't be recorded in a single run due to memory limitations, this utility could be used to automatically take 24 captures of one hour each. ### Prerequisites -- Saleae Logic software running with the socket API enabled. [instructions](https://support.saleae.com/hc/en-us/articles/208667266-How-to-enable-the-socket-server-for-the-scripting-API) -- [python 3](https://www.python.org/downloads/) installed. -- [ppannuto python-saleae](https://github.com/ppannuto/python-saleae) library installed. +- Saleae Logic software running with the socket API enabled. [instructions](https://support.saleae.com/saleae-api-and-sdk/socket-api) +- [ppannuto python-saleae](https://github.com/ppannuto/python-saleae) library installed. ### Usage @@ -17,16 +16,17 @@ The utility allows the user to set the number of captures and the duration of ea ### Examples -**Just take 5 captures, each 0.1 seconds long, without saving anything.** +**Take 1 second long capture without saving anything.** ``` -python saleae_cli.py --capture-count 5 --capture-duration 0.1 +python saleae_cli.py ``` -Note: if python 2 and python 3 are installed, you may need to call python using `python3` +**Just take 5 captures, each 0.1 seconds long, without saving anything.** +``` +python saleae_cli.py --capture-count 5 --capture-duration 0.1 +``` **Take 24 captures, each 1 hour long, exporting the analyzers to an 'export' folder on the desktop.** -Note that the destination folder must already exist before running the utility. - (Windows) ``` python saleae_cli.py --capture-count 24 --capture-duration 3600 --export-analyzers \Users\\Desktop\export diff --git a/saleae_cli.py b/saleae_cli.py index 9bca8c6..d43f34e 100644 --- a/saleae_cli.py +++ b/saleae_cli.py @@ -1,60 +1,58 @@ +# -*- coding: utf-8 -*- import os import saleae import argparse -def validate_path( path, argument_name ): - if path != None: - if os.path.isdir(path) == False: - print('the specified ' + argument_name + ' directory does not exist or is invalid') - print('you specified: ' + path) - quit() -parser = argparse.ArgumentParser(description='Saleae Command Line Interface Capture Utility') -parser.add_argument('--capture-count', required=True, type=int, metavar='COUNT', help='number of captures to repeat') -parser.add_argument('--capture-duration', required=True, type=float, metavar='SECONDS', help='duration of each capture in seconds') -parser.add_argument('--save-captures', metavar='PATH', help='if specified, saves each capture to the specified directory') -parser.add_argument('--export-data', metavar='PATH', help='if specified, exports the raw capture to the sepcified directory') -parser.add_argument('--export-analyzers', metavar='PATH', help='if specified, exports each analyzer to the specified directory') -parser.add_argument('--ip', metavar='IP', default='localhost', help='optional, IP address to connect to. Default localhost') -parser.add_argument('--port', metavar='PORT', default=10429, help='optional, Port to connect to. Default 10429') +parser = argparse.ArgumentParser(description='Saleae Command Line Interface Capture Utility', formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument('--capture-count', type=int, default=1, metavar='COUNT', help='Number of captures to repeat') +parser.add_argument('--capture-duration', type=float, default=1.0, metavar='SECONDS', help='Duration of each capture in seconds') +parser.add_argument('--save-captures', metavar='PATH', help='Saves each capture to the specified directory') +parser.add_argument('--export-data', metavar='PATH', help='Exports the raw capture to the sepcified directory') +parser.add_argument('--export-analyzers', metavar='PATH', help='Exports each analyzer to the specified directory') +parser.add_argument('--ip', metavar='IP', default='localhost', help='IP address to connect to') +parser.add_argument('--port', metavar='PORT', default=10429, help='Port to connect to') args = parser.parse_args() -validate_path(args.save_captures, '--save-captures') -validate_path(args.export_data, '--export-data') -validate_path(args.export_analyzers, '--export-analyzers') - - s = saleae.Saleae(args.ip, args.port) for x in range(args.capture_count): #set capture duration s.set_capture_seconds(args.capture_duration) #start capture. Only save to disk if the --save-captures option was specified. - if args.save_captures != None: + if args.save_captures is not None: file_name = '{0}.logicdata'.format(x) - save_path = os.path.join(args.save_captures, file_name) - print('starting capture and saving to ' + save_path) + save_directory = os.path.abspath(args.save_captures) + if not os.path.exists(save_directory): + os.makedirs(save_directory) + save_path = os.path.join(save_directory, file_name) + print('Starting capture and saving to ' + save_path) s.capture_to_file(save_path) else: #currently, the python library doesn't provide a CAPTURE command that blocks until an ACK is received s._cmd('CAPTURE') #raw export - if args.export_data != None: + if args.export_data is not None: file_name = '{0}.csv'.format(x) - save_path = os.path.join(args.export_data, file_name) - print('exporting data to ' + save_path) + save_directory = os.path.abspath(args.export_data) + if not os.path.exists(save_directory): + os.makedirs(save_directory) + save_path = os.path.join(save_directory, file_name) + print('Exporting data to ' + save_path) s.export_data2(save_path) #analyzer export - if args.export_analyzers != None: + if args.export_analyzers is not None: analyzers = s.get_analyzers() if analyzers.count == 0: print('Warning: analyzer export path was specified, but no analyzers are present in the capture') for analyzer in analyzers: file_name = '{0}_{1}.csv'.format(x, analyzer[0]) - save_path = os.path.join(args.export_analyzers, file_name) - print('exporting analyzer ' + analyzer[0] + ' to ' + save_path) + save_directory = os.path.abspath(args.export_analyzers) + if not os.path.exists(save_directory): + os.makedirs(save_directory) + save_path = os.path.join(save_directory, file_name) + print('Exporting analyzer ' + analyzer[0] + ' to ' + save_path) s.export_analyzer(analyzer[1], save_path) -