gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:04:33 25 4
gpt4 key购买 nike

我刚刚开始硬件编程,一直在尝试从 USB 设备获取序列号。我在应用程序级别而不是内核级别实现所有内容。

我已经有一本与 USB 设备匹配的字典,所以我知道我拥有正确的设备。我不确定的是我得到的数据以及如何解释它以及我是否正确地处理了这个问题。

这是我一直在尝试使用的一些代码,用于从设备获取序列号。

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);

这是我的 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];
}

任何帮助将不胜感激。谢谢!

最佳答案

所以我想通了。我在错误的位置获取了错误的信息,并且没有足够关注值的存储位置。以下是修改后的代码,用于返回所选设备的序列号。

//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;
}

我希望这可以节省人们很多时间!我希望当我开始尝试解决这个问题时我就能得到这个答案。

关于xcode - 在应用程序级别的 Xcode 中从 USB 设备获取序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com