我拿了
/Developer/Examples/IOKit/usb/USBSimple Example
并对其进行了修改,使其能够将一些数据传输到(希望很快能从)设备。
void transferData(IOUSBInterfaceInterface245 **intf, UInt8 inPipeRef, UInt8 outPipeRef)
{
IOReturn err;
CFRunLoopSourceRef cfSource;
err = (*intf)->CreateInterfaceAsyncEventSource(intf, &cfSource);
if(err) {
printf("transferData: unable to create event source, err = %08x\n", err);
return;
}
// this is what I need to send to the device
outBuf[0] = 0;
outBuf[1] = 0;
outBuf[2] = 0x18;
[... snip ...]
// the following works, although I have no confirmation
// that the data actually arrives to the device but err is 0 afterwards
err = (*intf)->WritePipe(intf, outPipeRef, outBuf, 64);
if(err) {
printf("transferData: WritePipeFailed, err = %08x\n", err);
return;
}
UInt32 numBytesRead;
numBytesRead = sizeof(inBuf);
// this hangs until I disconnect the device
err = (*intf)->ReadPipe(intf, inPipeRef, inBuf, &numBytesRead);
if(err) {
printf("transferData: ReadPipeFailed, err = %08x\n", err);
return;
}
}
因此,代码的读取部分会一直挂起,直到我在设备输出时断开它
transferData: ReadPipeFailed, err = e00002ed
-这证实了我实际上是在说/听设备。
我选择了这种同步方法,而苹果的例子使用异步方法来简化事情,但没有任何效果。设备没有响应吗?但如果没有,该方法是否应该跳过并在numBytesRead中返回0?