11

I'm trying to connect a USB-Device ( build by myself ) to communicate with my development board ( ODROID-X ) Unfortunately, the examples are very little, as far as the asynchronous communication. I'd some problems with the interrupt driven data exchange - how to build the connection by using the asynchronous interrupt mode? In one direction, the transmission was possible ... but in both it doesn't work. Is there an example like this:

  • send a ByteBuffer with endpoint_OUT
  • get a message from device on endpoint_IN

both in interrupt mode.

Thanks a lot for your support.

Hardy

4

1 回答 1

20

也许我误解了这里的问题。从级别 12 开始,作为 API 包一部分的示例导弹发射器应用程序使用queue()requestWait()方法来处理中断类型的端点。请求是 In 或 Out,取决于 EndPoint 的方向。

一个相当点头的请求->回复的代码看起来像这样。您可能希望以不同的方式构造真实代码,但这为您提供了需要发生的事情的要点(我希望)

public void run() {
    int bufferMaxLength=mEndpointOut.getMaxPacketSize();
    ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
    UsbRequest request = new UsbRequest(); // create an URB
    request.initialize(mConnection, mEndpointOut);
    buffer.put(/* your payload here */;

    // queue the outbound request
    boolean retval = request.queue(buffer, 1); 
        if (mConnection.requestWait() == request) { 
             // wait for confirmation (request was sent)
             UsbRequest inRequest = new UsbRequest(); 
             // URB for the incoming data
             inRequest.initialize(mConnection, mEndpointIn); 
             // the direction is dictated by this initialisation to the incoming endpoint.
             if(inRequest.queue(buffer, bufferMaxLength) == true){
                  mConnection.requestWait(); 
                  // wait for this request to be completed
                  // at this point buffer contains the data received
             }
        }
}

如果您实际上正在寻找一种以异步方式运行此 IO 而不将线程绑定到它的方法,那么我认为您需要考虑使用该DeviceConnection.getFilehandle()方法返回一个标准文件句柄,理论上您可以像使用它一样使用它任何其他文件类型资源。但是我会注意到我没有尝试过这个。

如果这些都不能解决问题,请修改问题以澄清您正在努力寻找的示例。我希望这有帮助。

于 2012-09-12T08:41:20.847 回答