【问题标题】:How to find the parent USB device of a serial port under macOS?macOS下如何查找串口的父USB设备?
【发布时间】:2018-08-18 11:40:59
【问题描述】:

以下是我尝试枚举 Mac 上找到的所有串行端口并遍历设备节点树以找到 USB 串行适配器的父 USB 设备的方法:

#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/serial/IOSerialKeys.h>
#import <IOKit/usb/IOUSBLib.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        CFMutableDictionaryRef keywordDict = IOServiceMatching(kIOSerialBSDServiceValue);
        io_object_t port = 0;
        io_iterator_t iterator = 0;
        kern_return_t result = IOServiceGetMatchingServices(kIOMasterPortDefault, keywordDict, &iterator);
        if (result)
        {
            NSLog(@"Failed to find any serial ports.");
            exit(EXIT_FAILURE);
        }

        while ((port = IOIteratorNext(iterator)))
        {
            @autoreleasepool
            {
                NSMutableArray<NSDictionary *> *array = [NSMutableArray array];
                CFMutableDictionaryRef dict = NULL;
                result = IORegistryEntryCreateCFProperties(port, &dict, kCFAllocatorDefault, 0);
                if (!result)
                    [array addObject:CFBridgingRelease(dict)];

                io_object_t parent = 0;
                io_object_t parents = port;
                while (IORegistryEntryGetParentEntry(parents, kIOServicePlane, &parent))
                {
                    result = IORegistryEntryCreateCFProperties(parent, &dict, kCFAllocatorDefault, 0);
                    if (!result)
                        [array addObject:CFBridgingRelease(dict)];

                    if (parents != port)
                        IOObjectRelease(parents);
                    parents = parent;
                }

                NSLog(@"0x%08lx = %@", (uintptr_t)port, array);
            }
        }
    }
    return 0;
}

但是,虽然此代码可以找到所有串行端口(USB 串行适配器和我的 Hackintosh 上的 Super IO 端口),但向上遍历 IOService 树并不起作用。帮助?目标操作系统为 macOS 10.13.6。

编辑

这是我的一个 USB 串行适配器的输出:

2018-08-18 19:05:39.860794+0800 lstty[98738:5197725] 0x00002a07 = (
        {
        CFBundleIdentifier = "com.apple.iokit.IOSerialFamily";
        IOCalloutDevice = "/dev/cu.usbmodem1751";
        IOClass = IOSerialBSDClient;
        IODialinDevice = "/dev/tty.usbmodem1751";
        IOGeneralInterest = "IOCommand is not serializable";
        IOMatchCategory = IODefaultMatchCategory;
        IOProbeScore = 1000;
        IOProviderClass = IOSerialStreamSync;
        IOResourceMatch = IOBSD;
        IOSerialBSDClientType = IOModemSerialStream;
        IOTTYBaseName = usbmodem;
        IOTTYDevice = usbmodem1751;
        IOTTYSuffix = 1751;
    }
)

这是同一设备的ioreg -itrc IOSerialBSDClient -w0 输出:

+-o Root  <class IORegistryEntry, id 0x100000100, retain 15>
  +-o iMac14,2  <class IORegistryEntry:IOService:IOPlatformExpertDevice, id 0x100000110, registered, matched, active, busy 0 (51894 ms), retain 45>
    +-o AppleACPIPlatformExpert  <class IORegistryEntry:IOService:IOPlatformExpert:IODTPlatformExpert:IOACPIPlatformExpert:AppleACPIPlatformExpert, id 0x100000111, registered, matched, active, busy 0 (41509 ms), retain 50>
      +-o PCI0@0  <class IORegistryEntry:IOService:IOPlatformDevice:IOACPIPlatformDevice, id 0x10000013e, registered, matched, active, busy 0 (41377 ms), retain 54>
        +-o AppleACPIPCI  <class IORegistryEntry:IOService:IOPCIBridge:AppleACPIPCI, id 0x100000216, registered, matched, active, busy 0 (41332 ms), retain 31>
          +-o XHC@14  <class IORegistryEntry:IOService:IOPCIDevice, id 0x1000001f0, registered, matched, active, busy 0 (35582 ms), retain 11>
            +-o XHC@14000000  <class IORegistryEntry:IOService:AppleUSBHostController:AppleUSBXHCI:AppleUSBXHCIPCI:AppleUSBXHCILPT:AppleUSBXHCILPTH:AppleUSBXHCILPTHB, id 0x10000023d, registered, matched, active, busy 0 (35072 ms), retain 1249>
              +-o HS10@14a00000  <class IORegistryEntry:IOService:AppleUSBHostPort:AppleUSBXHCIPort:AppleUSB20XHCIPort, id 0x100000277, registered, matched, active, busy 0 (22180 ms), retain 17>
                +-o IOUSBHostDevice@14a00000  <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x100007a40, registered, matched, active, busy 0 (1604 ms), retain 31>
                  +-o AppleUSB20Hub@14a00000  <class IORegistryEntry:IOService:AppleUSBHub:AppleUSB20Hub, id 0x100007a4e, registered, matched, active, busy 0 (1600 ms), retain 23>
                    +-o AppleUSB20HubPort@14a10000  <class IORegistryEntry:IOService:AppleUSBHostPort:AppleUSBHubPort:AppleUSB20HubPort, id 0x100007a52, registered, matched, active, busy 0 (964 ms), retain 17>
                      +-o IOUSBHostDevice@14a10000  <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x1000082bb, registered, matched, active, busy 0 (323 ms), retain 32>
                        +-o AppleUSB20Hub@14a10000  <class IORegistryEntry:IOService:AppleUSBHub:AppleUSB20Hub, id 0x1000082d4, registered, matched, active, busy 0 (316 ms), retain 23>
                          +-o AppleUSB20HubPort@14a12000  <class IORegistryEntry:IOService:AppleUSBHostPort:AppleUSBHubPort:AppleUSB20HubPort, id 0x1000082d8, registered, matched, active, busy 0 (269 ms), retain 17>
                            +-o ComboDebug USB Serial@14a12000  <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x10000837e, registered, matched, active, busy 0 (268 ms), retain 28>
                              +-o IOUSBHostInterface@1  <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x100008390, registered, matched, active, busy 0 (259 ms), retain 8>
                                +-o AppleUSBACMData  <class IORegistryEntry:IOService:IOSerialDriverSync:AppleUSBACMData, id 0x1000083a0, registered, matched, active, busy 0 (1 ms), retain 6>
                                  +-o IOModemSerialStreamSync  <class IORegistryEntry:IOService:IOSerialStreamSync:IOModemSerialStreamSync, id 0x100008417, registered, matched, active, busy 0 (1 ms), retain 6>
                                    +-o IOSerialBSDClient  <class IORegistryEntry:IOService:IOSerialBSDClient, id 0x100008418, registered, matched, active, busy 0 (0 ms), retain 5>
                                        {
                                          "IOClass" = "IOSerialBSDClient"
                                          "CFBundleIdentifier" = "com.apple.iokit.IOSerialFamily"
                                          "IOProviderClass" = "IOSerialStreamSync"
                                          "IOTTYBaseName" = "usbmodem"
                                          "IOSerialBSDClientType" = "IOModemSerialStream"
                                          "IOProbeScore" = 1000
                                          "IOCalloutDevice" = "/dev/cu.usbmodem1751"
                                          "IODialinDevice" = "/dev/tty.usbmodem1751"
                                          "IOMatchCategory" = "IODefaultMatchCategory"
                                          "IOTTYDevice" = "usbmodem1751"
                                          "IOResourceMatch" = "IOBSD"
                                          "IOGeneralInterest" = "IOCommand is not serializable"
                                          "IOTTYSuffix" = "1751"
                                        }

【问题讨论】:

  • 如何从遍历循环(例如IORegistryEntryGetNameIOObjectGetClass)中为您期望找到USB 设备的端口之一提供一些调试输出。然后发布来自ioreg -itrc IOSerialBSDClient 的相应输出,以便我们查看它们的分歧点。
  • @pmdj 添加了一台设备的输出。步行的深度从来没有深入到露出 USB 设备。

标签: objective-c macos usb iokit


【解决方案1】:

提前终止的问题是循环的退出条件:

     while (IORegistryEntryGetParentEntry(parents, kIOServicePlane, &parent))

IORegistryEntryGetParentEntry() 返回 kern_return_t - KERN_SUCCESS 为 0,因此这意味着 while 条件的计算结果为 false,并且循环永远不会运行。试试

     while (KERN_SUCCESS == IORegistryEntryGetParentEntry(parents, kIOServicePlane, &parent))

【讨论】:

  • 该行的工作方式如下:while (!IORegistryEntryGetParentEntry(parents, kIOServicePlane, &amp;parent)) 我设法从循环中转储了相同级别的详细信息和更多信息。现在它只是找到USB设备并提取信息。
  • 是的,你的功能与我的建议相同。 (我只是更喜欢我的清晰,特别是考虑到您的原始符号使错误不明显。)
  • 我不得不承认,我是出于对每个时钟周期都很重要的小型设备编程的习惯而编写了那些不明显的代码。一些编译器为if (!func()) {}if (func() == 0) {} 发出不同的代码,而前者通常更短一些指令。
  • @MaxthonChan 如果启用任何优化,现代版本的 clang 和 GCC 会为这两个变体发出相同的代码:godbolt.org/z/vYWAnI
  • 当时我不得不处理 MPLAB XC8 v1.35 一段时间,而那东西不是两者的派生产品。也是 AVR 处理器常用的 GCC 4.3.5 版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2021-11-07
  • 2022-11-27
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多