Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 使用IOKit获取USB设备的BSD名称以写入设备?_Macos_Core Foundation_Iokit - Fatal编程技术网

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

Macos 使用IOKit获取USB设备的BSD名称以写入设备?,macos,core-foundation,iokit,Macos,Core Foundation,Iokit,我正在尝试使用MacOS下的IOKit确定虚拟串行端口的BSD名称 我有一个USB CDC设备,看起来像一个虚拟串行端口,我想获取BSD设备路径,这样我就可以只做一个fopen(“/dev/tty.usbmodem123”)。我有一个程序,它接受VID和PID并等待设备插入,然后我想使用BSD名称写入设备。设备在每个系统上的安装方式都不同,我尝试将其用作教学工具,因此我需要在写入设备之前搜索设备,而无需手动检查/dev/tty.*设备的安装位置 我有三个问题 首先,可以使用CFSTR(kIOBS

我正在尝试使用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),kcfolcatordefault,kioregistryiterrecursive)

其次,我已经能够获得服务飞机名称: iService:/AppleACPIPlatformExpert/PCI0@0/苹果皮/OHC1@4/AppleUSBOHCI/Intro toElectronics@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的设备。

也许OSX10.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"

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

这是我在添加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
希望它能帮助别人