2016-08-17 13 views
3

Я пытаюсь отправить сообщение на мой USB-устройство (Silicon Labs USB-UART Bridge), используя этот код:Отправка сообщения на устройство USB с помощью usb4java - ошибка ввода/вывода

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, кто-нибудь знает РЕАС почему это так или есть подсказка?

Заранее благодарен!

Редактировать: Код работает, когда я меняю конечную точку на 0x01, но это EP 1 OUT, 0x81 - это EP 1 IN, так что, где я должен отправлять свои сообщения, нет?

Edit 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_ - который является трубкой _in_ ... – Fildor

ответ

0

Да Fidor правильно 0x01 для написания и 0x81 для чтения. Вход и выход с точки зрения главного контроллера.

Вы можете считывать из конечных точек IN и записывать в конечные точки OUT. Единственным исключением является Control Transfer, который не зависит от направления.

+0

Ну, когда я пытаюсь читать с 0x81, я получаю то же самое errormessage для моего readMessage. sendMessage, похоже, работает. – pilgrimm

+0

Точное значение означает также ошибку USB 1? Вы заглянули в endpointdescriptors этих конечных точек существуют? – dryman

+0

Да, такая же ошибка, как описано выше, я получил дескрипторы Endpoint, используя дамп устройства, это дескрипторы: – 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