From 13378579b3ebe2b36358a0cd5d2ba15466397a14 Mon Sep 17 00:00:00 2001 From: Vasco Tenner Date: Wed, 11 Jul 2018 13:56:23 +0200 Subject: [PATCH 1/3] Restructured find_devices: it can now be imported as a module --- find_port.py | 70 ++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/find_port.py b/find_port.py index 1e435a9..9043998 100755 --- a/find_port.py +++ b/find_port.py @@ -1,18 +1,14 @@ #!/usr/bin/env python """Program which detects USB serial ports. - This program will search for a USB serial port using a search criteria. In its simplest form, you can use the -l (--list) option to list all of the detected serial ports. - You can also add the following filters: - --vid 2341 Will only match devices with a Vendor ID of 2341. --pid 0001 Will only match devices with a Product ID of 0001 --vendor Micro Will only match devices whose vendor name starts with Micro --seral 00123 Will only match devices whose serial number stats with 00123 - If you use -l or --list then detailed information about all of the matches will be printed. If you don't use -l (or --list) then only the name of the device will be printed (i.e. /dev/ttyACM0). This is useful in scripts @@ -21,48 +17,37 @@ from __future__ import print_function -import select import pyudev -import serial import sys -import tty -import termios -import traceback -import syslog import argparse -import time -EXIT_CHAR = chr(ord('X') - ord('@')) # Control-X -def is_usb_serial(device, args): +def is_usb_serial(device, vid=None, pid=None, vendor=None, serial=None): """Checks device to see if its a USB Serial device. - The caller already filters on the subsystem being 'tty'. - If serial_num or vendor is provided, then it will further check to see if the serial number and vendor of the device also matches. """ if 'ID_VENDOR' not in device: return False - if not args.vid is None: - if device['ID_VENDOR_ID'] != args.vid: + if not vid is None: + if device['ID_VENDOR_ID'] != vid: return False - if not args.pid is None: - if device['ID_MODEL_ID'] != args.pid: + if not pid is None: + if device['ID_MODEL_ID'] != pid: return False - if not args.vendor is None: + if not vendor is None: if not 'ID_VENDOR' in device: - return False - if not device['ID_VENDOR'].startswith(args.vendor): return False - if not args.serial is None: + if not device['ID_VENDOR'].startswith(vendor): + return False + if not serial is None: if not 'ID_SERIAL_SHORT' in device: return False - if not device['ID_SERIAL_SHORT'].startswith(args.serial): + if not device['ID_SERIAL_SHORT'].startswith(serial): return False return True - def extra_info(device): extra_items = [] if 'ID_VENDOR' in device: @@ -73,11 +58,23 @@ def extra_info(device): return ' with ' + ' '.join(extra_items) return '' +def list_devices(vid=None, pid=None, vendor=None, serial=None): + devs = [] + context = pyudev.Context() + for device in context.list_devices(subsystem='tty'): + if is_usb_serial(device, vid=vid, pid=pid, vendor=vendor, + serial=serial): + devs.append([device['ID_VENDOR_ID'], device['ID_MODEL_ID'], + extra_info(device), device.device_node]) + return devs + +def print_list_devices(*args, **kwargs): + '''Print all USB Serial devices''' + for device in list_devices(*args, **kwargs): + print('USB Serial Device {}:{}{} found @{}'.format(*device)) def main(): """The main program.""" - - default_baud = 115200 parser = argparse.ArgumentParser( prog="find-port.py", usage="%(prog)s [options] [command]", @@ -127,25 +124,16 @@ def main(): if args.verbose: print('pyudev version = %s' % pyudev.__version__) - context = pyudev.Context() if args.list: - detected = False - for device in context.list_devices(subsystem='tty'): - if is_usb_serial(device, args): - print('USB Serial Device %s:%s%s found @%s\r' % ( - device['ID_VENDOR_ID'], device['ID_MODEL_ID'], - extra_info(device), device.device_node)) - detected = True - if not detected: - print('No USB Serial devices detected.\r') - return + print_list_devices(**args) + context = pyudev.Context() for device in context.list_devices(subsystem='tty'): - if is_usb_serial(device, args): + if is_usb_serial(device, **args): print(device.device_node) return sys.exit(1) - -main() +if __name__ == "__main__": + main() From 2574f07cca0a7c0e8b70932395855aa0348c3142 Mon Sep 17 00:00:00 2001 From: Vasco Tenner Date: Wed, 11 Jul 2018 13:58:16 +0200 Subject: [PATCH 2/3] Made find_port pep8 compatible --- find_port.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/find_port.py b/find_port.py index 9043998..f476723 100755 --- a/find_port.py +++ b/find_port.py @@ -30,24 +30,25 @@ def is_usb_serial(device, vid=None, pid=None, vendor=None, serial=None): """ if 'ID_VENDOR' not in device: return False - if not vid is None: + if vid is not None: if device['ID_VENDOR_ID'] != vid: return False - if not pid is None: + if pid is not None: if device['ID_MODEL_ID'] != pid: return False - if not vendor is None: - if not 'ID_VENDOR' in device: + if vendor is not None: + if 'ID_VENDOR' not in device: return False if not device['ID_VENDOR'].startswith(vendor): return False - if not serial is None: - if not 'ID_SERIAL_SHORT' in device: + if serial is not None: + if 'ID_SERIAL_SHORT' not in device: return False if not device['ID_SERIAL_SHORT'].startswith(serial): return False return True + def extra_info(device): extra_items = [] if 'ID_VENDOR' in device: @@ -58,6 +59,7 @@ def extra_info(device): return ' with ' + ' '.join(extra_items) return '' + def list_devices(vid=None, pid=None, vendor=None, serial=None): devs = [] context = pyudev.Context() @@ -68,11 +70,13 @@ def list_devices(vid=None, pid=None, vendor=None, serial=None): extra_info(device), device.device_node]) return devs + def print_list_devices(*args, **kwargs): '''Print all USB Serial devices''' for device in list_devices(*args, **kwargs): print('USB Serial Device {}:{}{} found @{}'.format(*device)) + def main(): """The main program.""" parser = argparse.ArgumentParser( @@ -124,7 +128,6 @@ def main(): if args.verbose: print('pyudev version = %s' % pyudev.__version__) - if args.list: print_list_devices(**args) @@ -135,5 +138,6 @@ def main(): return sys.exit(1) + if __name__ == "__main__": main() From 131e3860335280243de35339db9c5aacd028acb2 Mon Sep 17 00:00:00 2001 From: Vasco Tenner Date: Thu, 12 Jul 2018 12:36:27 +0200 Subject: [PATCH 3/3] Fixed namespace to dict error --- find_port.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/find_port.py b/find_port.py index f476723..9c3e0f8 100755 --- a/find_port.py +++ b/find_port.py @@ -22,7 +22,8 @@ import argparse -def is_usb_serial(device, vid=None, pid=None, vendor=None, serial=None): +def is_usb_serial(device, vid=None, pid=None, vendor=None, serial=None, *args, + **kwargs): """Checks device to see if its a USB Serial device. The caller already filters on the subsystem being 'tty'. If serial_num or vendor is provided, then it will further check to @@ -60,7 +61,8 @@ def extra_info(device): return '' -def list_devices(vid=None, pid=None, vendor=None, serial=None): +def list_devices(vid=None, pid=None, vendor=None, serial=None, *args, + **kwargs): devs = [] context = pyudev.Context() for device in context.list_devices(subsystem='tty'): @@ -129,11 +131,11 @@ def main(): print('pyudev version = %s' % pyudev.__version__) if args.list: - print_list_devices(**args) + print_list_devices(**vars(args)) context = pyudev.Context() for device in context.list_devices(subsystem='tty'): - if is_usb_serial(device, **args): + if is_usb_serial(device, **vars(args)): print(device.device_node) return sys.exit(1)