我在Qt(Yosemite)环境中使用libusb 0.1和Mac OS X v10.10。我的目标是获取所有连接的USB密钥的序列号(usbclass = 8)。
通常第一次读数正确,我无法理解为什么,但有时从后续读数中,不再检测到设备。有时我在usb_get_string_simple(handle, device->descriptor.iSerialNumber)
函数上得到-60错误代码。我该如何解决这个问题?
我尝试过使用FAT,FAT32和NTFS文件系统的USB密钥,但它们都有同样的问题。
在同一个项目中,我使用libusb来读取/写入连接到Mac的打印机设备,并读取序列号。此类设备没有问题。太奇怪了。
在.pro文件中:
INCLUDEPATH += /Users/sborfedor/Desktop/root/current/includes
DEPENDPATH += /Users/sborfedor/Desktop/root/current/includes
LIBS += -L/Users/sborfedor/Desktop/root/current/bin_osx/libs/ -lusb-legacy
DESTDIR = /Users/sborfedor/Desktop/root/current/bin_osx
这是我的代码:
void MainWindow::run() {
struct usb_bus* bus = NULL;
struct usb_device* device = NULL;
const int MASSSTORAGE_CLASS = 8;
usb_init();
usb_set_debug(3);
usb_find_busses();
usb_find_devices();
int ret;
for ( bus = usb_get_busses(); bus; bus = bus->next ) {
for ( device = bus->devices; device; device = device->next ) {
if (device->descriptor.bDeviceClass != MASSSTORAGE_CLASS && device->descriptor.bDeviceClass != 0) {
continue;
}
for ( int cid = 0; cid < device->descriptor.bNumConfigurations; ++cid ) {
struct usb_config_descriptor* config = &( device->config[cid] );
if (config == NULL)
continue;
for ( int iid = 0; iid < config->bNumInterfaces; ++iid ) {
struct usb_interface* interface = &( config->interface[iid] );
if (interface == NULL)
continue;
usb_dev_handle* handle = usb_open( device );
if (handle == NULL)
continue;
for ( int sid = 0; sid < interface->num_altsetting; sid++ ) {
struct usb_interface_descriptor* settings = &( interface->altsetting[sid] );
if (settings == NULL)
continue;
int usbClass = device->descriptor.bDeviceClass;
if (usbClass == 0) {
usbClass = settings->bInterfaceClass;
}
if (usbClass != MASSSTORAGE_CLASS)
continue;
char device_serial_number[255];
ret = usb_get_string_simple( handle, device->descriptor.iSerialNumber, device_serial_number, sizeof(device_serial_number) );
if (ret > 0 ) {
qDebug() << "SERIAL_NUMBER="<<QString(device_serial_number);
} else {
qDebug() << "*** usb_get_string_simple( handle, device->descriptor.iSerialNumber, device_serial_number, USBX_DEVICE_SERIAL_NUMBER_MAX_SIZE ) RET="<<ret;
}
}
usb_close( handle );
}
}
}
}
}
答案 0 :(得分:1)
我对libusb并不是特别熟悉,但OSX已经读出序列号并将它们存储在IOKit设备小块的相关属性中。所以不需要向硬件发送请求,这可能会使活动驱动程序混乱? (Libusb可能会在设备上创建用户客户端驱动程序实例)
磁盘仲裁框架是枚举系统中磁盘并查询其属性的好方法 - 这比直接使用IOKit要容易一些。