2016-08-17 490 views
3

我試圖將消息發送到使用此代碼我的USB設備(Silicon Labs的USB-UART橋接):使用usb4java發送消息到USB設備 - 輸入/輸出錯誤

public void sendMessage(UsbInterface iface, String message, 
     int i){ 

    UsbPipe pipe = null; 

    try { 
     iface.claim(new UsbInterfacePolicy() { 
      @Override 
      public boolean forceClaim(UsbInterface usbInterface) { 
       return true; 
      } 
     }); 

     UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoint((byte) i); 
     pipe = endpoint.getUsbPipe(); 
     pipe.open(); 

     int sent = pipe.syncSubmit(message.getBytes()); 

     System.out.println(sent + " bytes sent"); 
     pipe.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
     try { 
      iface.release(); 
     } catch (UsbClaimException e) { 
      e.printStackTrace(); 
     } catch (UsbNotActiveException e) { 
      e.printStackTrace(); 
     } catch (UsbDisconnectedException e) { 
      e.printStackTrace(); 
     } catch (UsbException e) { 
      e.printStackTrace(); 
     } 
    } 
}//sendMessage 

當我執行它:

public static void main(final String[] args) throws UsbException { 

    Usb4JavaHigh usb4java = new Usb4JavaHigh(); 
    UsbDevice usbDevice = usb4java.findDevice((short) (0x10C4), (short) (0xEA60)); 

    usb4java.sendMessage(usb4java.getDeviceInterface(usbDevice, 0), "01FF05FB13", 0x81); 
    usb4java.readMessage(usb4java.getDeviceInterface(usbDevice, 0), 0x01); 
} 

我得到這個錯誤:

javax.usb.UsbPlatformException: USB error 1: Transfer error on bulk endpoint: Input/Output Error 
at org.usb4java.javax.ExceptionUtils.createPlatformException(ExceptionUtils.java:39) 
at org.usb4java.javax.IrpQueue.transferBulk(IrpQueue.java:239) 
at org.usb4java.javax.IrpQueue.transfer(IrpQueue.java:197) 
at org.usb4java.javax.IrpQueue.read(IrpQueue.java:126) 
at org.usb4java.javax.IrpQueue.processIrp(IrpQueue.java:76) 
at org.usb4java.javax.AbstractIrpQueue.process(AbstractIrpQueue.java:104) 
at org.usb4java.javax.AbstractIrpQueue$1.run(AbstractIrpQueue.java:73) 
at java.lang.Thread.run(Unknown Source) 

但是我沒有得到任何ERRORMESSAGE我readMessage,沒有人知道一個REAS爲什麼這是或有暗示?

在此先感謝!

編輯:當我將端點更改爲0x01,但那一個是EP 1 OUT,0x81是EP 1 IN,因此我應該發送消息的地方不是?

編輯2:readMessage的代碼:

public void readMessage(UsbInterface iface, 
     int j){ 

    UsbPipe pipe = null; 

    try { 
     iface.claim(new UsbInterfacePolicy() { 
      @Override 
      public boolean forceClaim(UsbInterface usbInterface) { 
       return true; 
      } 
     }); 

     UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoint((byte) j); // there can be more 1,2,3.. 
     pipe = endpoint.getUsbPipe(); 
     pipe.open(); 

     /*pipe.addUsbPipeListener(new UsbPipeListener() 
     {    
      @Override 
      public void errorEventOccurred(UsbPipeErrorEvent event) 
      { 
       UsbException error = event.getUsbException(); 
       error.printStackTrace(); 
      } 

      @Override 
      public void dataEventOccurred(UsbPipeDataEvent event) 
      { 
       byte[] data = event.getData(); 

       System.out.println(data + " bytes received"); 
       for(int i =0 ; i<data.length; i++){System.out.print(data[i]+" ");} 
      } 
     });*/ 

     byte[] data = new byte[8]; 
     int received = pipe.syncSubmit(data); 
     System.out.println(received + " bytes received"); 
     for(int i =0 ; i<data.length; i++){System.out.print(data[i]+" ");}//*/ 

     pipe.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
     try { 
      iface.release(); 
     } catch (UsbClaimException e) { 
      e.printStackTrace(); 
     } catch (UsbNotActiveException e) { 
      e.printStackTrace(); 
     } catch (UsbDisconnectedException e) { 
      e.printStackTrace(); 
     } catch (UsbException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
+0

「代碼工作時,我改變端點爲0x01,但一個是EP 1 OUT」 - 我懷疑術語的混亂。我想從客戶的角度來看「out」。這是**你** _out_ - 這是管道的_in_ ... – Fildor

回答

0

是Fidor是正確的0x01爲寫作和0×81的讀數。 IN和OUT是來自主機控制器的角度。

您可以從IN端點讀取數據並寫入OUT端點。只有方向無關的控制轉移纔是例外。

+0

好吧,當我嘗試從0x81讀取時,我得到了完全相同的錯誤消息,爲我的readMessage。 sendMessage似乎工作。 – pilgrimm

+0

完全相同也意味着USB錯誤1?你看過這些端點的端點描述符是否存在? – dryman

+0

是的,與上述相同的錯誤,我使用設備轉儲得到了我的端點描述符,這些描述符是: – pilgrimm

0

對不起這裏是我的描述:

Endpoint Descriptor: 
    bLength     7 
    bDescriptorType   5 
    bEndpointAddress  0x01 EP 1 OUT 
    bmAttributes    2 
    Transfer Type    Bulk 
    Synch Type    None 
    Usage Type    Data 
    wMaxPacketSize   64 
    bInterval    0 

Endpoint Descriptor: 
    bLength     7 
    bDescriptorType   5 
    bEndpointAddress  0x81 EP 1 IN 
    bmAttributes    2 
    Transfer Type    Bulk 
    Synch Type    None 
    Usage Type    Data 
    wMaxPacketSize   64 
    bInterval    0