【问题标题】:Getting the BSD name of a USB device using IOKit to write to the device?使用 IOKit 获取 USB 设备的 BSD 名称以写入设备?
【发布时间】:2013-01-28 06:35:31
【问题描述】:

我正在尝试在 MacOS 下使用 IOKit 确定虚拟串口的 BSD 名称。

我有一个看起来像虚拟串行端口的 USB CDC 设备,我想获取 BSD 设备路径,以便我可以执行 fopen("/dev/tty.usbmodem123")。我有一个程序,它获取 VID 和 PID 并等待设备插入,然后我想使用 BSD 名称写入设备。该设备在每个系统上的安装方式都不同,我正在尝试将其用作教学工具,因此我需要在写入之前搜索设备,而无需手动检查 /dev/tty.* 以了解设备的安装位置。

我有 3 个问题。

首先,可以使用CFSTR(kIOBSDNameKey)获取虚拟串口的BSD名称吗? IORegistryEntrySearchCFProperty() 和 FindProp() 总是返回“null”。有谁知道 BSD 名称是否可以由非块设备返回? 我目前正在这样做:

bsdName = IORegistryEntrySearchCFProperty(p_usb_ref, kIOServicePlane, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, kIORegistryIterateRecursively );

其次,我已经能够获得服务平面名称: IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/OHC1@4/AppleUSBOHCI/Intro to Electronics@4100000 这对应于安装点:/dev/tty.usbmodem411 有谁知道如何将服务平面名称转换为开发树名称?

第三,我是不是把事情弄得太复杂了?我已经知道设备 io 句柄,有没有办法使用它将数据写入设备?我只需要发送一些 ASCII 字节来闪烁一些 LED。

任何建议将不胜感激。

EDIT:

花了更多时间查看此内容后,我发现我的问题是我在加载 CDC 驱动程序之前查询 BSD 名称。我目前正在获取 BSD 名称,然后整理出 VID 和 PID。

上面解决我问题的代码是:

matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue);
CFDictionarySetValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType));
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iter);

然后您遍历 iter 以找到具有正确 ID 的设备。

【问题讨论】:

    标签: macos core-foundation iokit


    【解决方案1】:

    这是我在添加 USB 串行设备时使用的 IONotification: 在 10.11 下它是空的。在尝试了很多事情之后,这是我的解决方案:

    while ((usbDevice = IOIteratorNext(iterator)))
    {
      //when hotplugging under OSX 10.11:
      sleep(1);//otherwise the property will be empty.
    
      CFStringRef deviceBSDName_cf = (CFStringRef) IORegistryEntrySearchCFProperty (usbDevice,
      kIOServicePlane,
      CFSTR (kIOCalloutDeviceKey),
      kCFAllocatorDefault,
      kIORegistryIterateRecursively );
    
      NSLog(@"device path: %@", deviceBSDName_cf);
    
    }
    

    它应该找到类似:/dev/cu.xxxxx
    希望它可以帮助某人。

    【讨论】:

      【解决方案2】:

      也许在 OS X 10.10 中情况发生了变化?您最后的代码 sn-p 似乎在我的系统上找不到/dev/tty.usbmodem00054741 设备:

      io_iterator_t devlisthndl = 0;
      CFMutableDictionaryRef matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue);
      CFIndex dict_count = CFDictionaryGetCount(matchingDictionary);
      CFDictionarySetValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType));
      kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &devlisthndl);
      
      (lldb) p matchingDictionary
      (CFMutableDictionaryRef) $3 = 0x0000610000267780 @"0 entries"
      

      你最终是如何获得挂载点字符串的?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 2015-04-02
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多