使用IOKit获取USB设备的BSD名称以写入设备?

7
我正在尝试在MacOS下使用IOKit确定虚拟串口的BSD名称。我有一个看起来像虚拟串口的USB CDC设备,我想获取BSD设备路径,这样我就可以执行fopen(“/dev/tty.usbmodem123”)。 我有一个程序,它获取VID和PID并等待设备插入,然后我想使用BSD名称向设备写入数据。该设备在每个系统上都会以不同的方式挂载,我正试图将其用作教学工具,因此我需要在手动检查/dev/tty.*之前搜索该设备的位置。
我的问题有三个。首先,是否可以使用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。是否有人知道如何将服务平面名称转换为dev树名称?
第三,我是否使这个问题过于复杂?我已经知道设备io句柄,是否有一种方法可以使用它来向设备写入数据?我只需要发送一些ASCII字节以闪烁一些LED。
任何建议都将不胜感激。
编辑: 在花费更多时间查看此问题后,我发现我的问题是我在CDC驱动程序加载之前查询BSD名称。我目前正在获取BSD名称,然后为VID和PID进行排序。
解决我上述问题的代码是:
matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue);
CFDictionarySetValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType));
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iter);

然后你遍历iter,以找到正确ID的设备。

2个回答

2

当我添加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 的东西。

0

或许在OS X 10.10中有所改变?您最后的代码片段似乎无法在我的系统上找到/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"

你是怎么得到挂载点字符串的?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接