简体   繁体   English

在应用程序级别从 xcode 中的 USB 设备获取序列号

[英]Get serial number from usb device in xcode at application level

I am just starting out at hardware programming and have been trying to get the serial number from a USB device.我刚刚开始硬件编程,一直在尝试从 USB 设备获取序列号。 I am implementing everything at the application level rather than the kernel level.我正在应用程序级别而不是内核级别实现所有内容。

I have a dictionary already that matches to the usb device so I know I have the correct device.我已经有一本与 USB 设备匹配的字典,所以我知道我有正确的设备。 What I am not sure about is the data that I'm getting back and how to interpret it and whether or not I'm going about this correctly.我不确定的是我要返回的数据以及如何解释它以及我是否正确地进行了处理。

So here's some code I have that I've been trying to work with to get the serial number from the device.所以这是我一直在尝试使用的一些代码,以从设备获取序列号。

IOReturn retSerialIndex;
UInt8 snsi;
retSerialIndex = (*usbDevice)->USBGetSerialNumberStringIndex(usbDevice, &snsi);
if (retSerialIndex != kIOReturnSuccess)
{
    printf("Could not get serial number string index (error: %x)\n", retSerialIndex);
}
printf("Serial string index is %x\n", retSerialIndex);
IOReturn retSerial;
IOUSBConfigurationDescriptorPtr *desc;
if (retSerialIndex)
{
    retSerial = (*usbDevice)->GetConfigurationDescriptorPtr(usbDevice,
                                                            retSerialIndex,
                                                            desc);
    if (retSerial != kIOReturnSuccess)
    {
        printf("Could not get serial number using string index (error: %x)\n", retSerial);
    }
}
printf("Serial number is %x\n", retSerial);
char serialString;
serialString = getUSBStringDescriptor(usbDevice, retSerial);
printf("Serial string is %c\n", serialString);

And here is my getUSBStringDescriptor implementation:这是我的 getUSBStringDescriptor 实现:

char MyClass::getUSBStringDescriptor( IOUSBDeviceInterface300** usbDevice, int idx )
{
   assert( usbDevice );
   UInt32 buffer[32];
   IOUSBDevRequest request;
   request.bmRequestType = USBmakebmRequestType(
                                             kUSBIn,
                                             kUSBStandard,
                                             kUSBDevice );
   request.bRequest = kUSBRqGetDescriptor;
   request.wValue = (3 << 8) | idx;
   request.wIndex = 0x0904; // english
   request.wLength = sizeof( buffer );
   request.pData = buffer;
   kern_return_t err = (*usbDevice)->DeviceRequest( usbDevice, &request );
   if ( err != 0 )
   {
       // the request failed.
       return NULL;
   }
   static char charstring[33];
   int count = ( request.wLenDone - 1 ) / 2;
   int i;
   for ( i = 0; i < count; i++ )
        charstring[i] = buffer[i+1];
    charstring[i] = '\0';
    return charstring[33 - 1];
}

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

So I figured it out.所以我想通了。 I was getting the wrong information at the wrong places and not paying enough attention to where the values were being stored.我在错误的地方得到了错误的信息,而没有对值的存储位置给予足够的关注。 Here is the amended code that returns the serial number for the selected device.这是返回所选设备序列号的修改后的代码。

//My call to get serial number
NSString * serialString;
serialString = getUSBStringDescriptor(usbDevice);
printf("Serial string is %s\n", [serialString UTF8String]);

//My function that gets the serial number for me
NSString * MyUSBClass::getUSBStringDescriptor( IOUSBDeviceInterface300** usbDevice)
{
    UInt8 snsi;
    UInt8 buffer[256];
    IOUSBDevRequest request;
    IOReturn err;
    err = (*usbDevice)->USBGetSerialNumberStringIndex(usbDevice, &snsi);
    if (err != kIOReturnSuccess)
    {
        printf("Could not get serial number string index (error: %x)\n", err);
        return 0;
    }
    request.bmRequestType = USBmakebmRequestType(
                                             kUSBIn,
                                             kUSBStandard,
                                             kUSBDevice );
    request.bRequest = kUSBRqGetDescriptor;
    request.wValue = (kUSBStringDesc << 8) | snsi;
    request.wIndex = 0x409; // english
    request.wLength = sizeof( buffer );
    request.pData = &buffer[0];
    bzero(&buffer[0], sizeof(buffer));
    err = (*usbDevice)->DeviceRequest( usbDevice, &request );
    if ( err != kIOReturnSuccess )
    {
        printf("Could not get serial number string using index\n");
        // the request failed.
        return 0;
    }
    int strLength;
    CFStringRef serialNumberString;
    strLength = buffer[0] -2;
    serialNumberString = CFStringCreateWithBytes(kCFAllocatorDefault, &buffer[2], strLength, kCFStringEncodingUTF16LE, FALSE);
    CFShow(serialNumberString);
    NSString *stringBuffer = (__bridge NSString *)serialNumberString;
    return stringBuffer;
}

I hope this saves someone lots of time!我希望这可以节省很多时间! I wish I had this answer when I started trying to figure this out.我希望当我开始尝试解决这个问题时我有这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM