Skip to content

Commit

Permalink
Merge pull request #13 from hoihu/master
Browse files Browse the repository at this point in the history
OSX: list_ports - add interface string if present
  • Loading branch information
zsquareplusc committed Sep 25, 2015
2 parents 56a506d + 1813fa8 commit 41cb401
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions serial/tools/list_ports_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# Also see the 'IORegistryExplorer' for an idea of what we are actually searching

import ctypes
from ctypes import util

from serial.tools import list_ports_common

iokit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('IOKit'))
Expand Down Expand Up @@ -180,28 +182,60 @@ def GetIOServicesByType(service_type):
return services


def location_to_string(location_number):
loc = ['{}-'.format(location_number >> 24)]
while location_number & 0xf00000:
def location_to_string(locationID):
"""
helper to calculate port and bus number from locationID
"""
loc = ['{}-'.format(locationID >> 24)]
while locationID & 0xf00000:
if len(loc) > 1:
loc.append('.')
loc.append('{}'.format((location_number >> 20) & 0xf))
location_number <<= 4
loc.append('{}'.format((locationID >> 20) & 0xf))
locationID <<= 4
return ''.join(loc)


class SuitableSerialInterface(object):
pass

def scan_interfaces():
"""
helper function to scan USB interfaces
returns a list of SuitableSerialInterface objects with name and id attributes
"""
interfaces = []
for service in GetIOServicesByType('IOSerialBSDClient'):
device = get_string_property(service, "IOCalloutDevice")
if device:
usb_device = GetParentDeviceByType(service, "IOUSBInterface")
if usb_device:
name = get_string_property(usb_device, "USB Interface Name") or None
locationID = get_int_property(usb_device, "locationID",kCFNumberSInt32Type) or ''
i = SuitableSerialInterface()
i.id = locationID
i.name = name
interfaces.append(i)
return interfaces

def search_for_locationID_in_interfaces(serial_interfaces, locationID):
for interface in serial_interfaces:
if (interface.id == locationID):
return interface.name
return None

def comports():
# Scan for all iokit serial ports
services = GetIOServicesByType('IOSerialBSDClient')
ports = []
serial_interfaces = scan_interfaces()
for service in services:
# First, add the callout device file.
device = get_string_property(service, "IOCalloutDevice")
if device:
info = list_ports_common.ListPortInfo(device)
# If the serial port is implemented by IOUSBDevice
usb_device = GetParentDeviceByType(service, "IOUSBDevice")
if usb_device is not None:
if usb_device:
# fetch some useful informations from properties
info.vid = get_int_property(usb_device, "idVendor", kCFNumberSInt16Type)
info.pid = get_int_property(usb_device, "idProduct", kCFNumberSInt16Type)
Expand All @@ -210,8 +244,8 @@ def comports():
info.manufacturer = get_string_property(usb_device, "USB Vendor Name")
locationID = get_int_property(usb_device, "locationID", kCFNumberSInt32Type)
info.location = location_to_string(locationID)
#~ bcd = get_int_property(usb_device, "bcdDevice", kCFNumberSInt16Type)
info.hwid = info.usb_info()
info.interface = search_for_locationID_in_interfaces(serial_interfaces, locationID)
ports.append(info)
return ports

Expand Down

0 comments on commit 41cb401

Please sign in to comment.