2017-01-10 4 views
1

JavaでIllegalStateExceptionのヘルプが必要です。 USBデバイスからデータを読み出すソースコードがあります。 libusbはコンテキストを初期化できません

このコード

はまだ終わっていませんが、私はすでに LibUsb.exit(null)

コードが

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.IntBuffer; 

import javax.usb.UsbConfiguration; 
import javax.usb.UsbDevice; 
import javax.usb.UsbDeviceDescriptor; 
import javax.usb.UsbDisconnectedException; 
import javax.usb.UsbException; 
import javax.usb.UsbHostManager; 
import javax.usb.UsbHub; 
import javax.usb.UsbInterface; 
import javax.usb.UsbInterfacePolicy; 
import javax.usb.UsbNotActiveException; 
import javax.usb.event.UsbPipeDataEvent; 
import javax.usb.event.UsbPipeErrorEvent; 
import javax.usb.event.UsbPipeListener; 

import org.usb4java.BufferUtils; 
import org.usb4java.DeviceHandle; 
import org.usb4java.LibUsb; 
import org.usb4java.LibUsbException; 

public class testnew { 

private final static short VENDOR_ID = 0x0403; 
private final static short PRODUCT_ID = 0x6001; 
private static byte IN_ENDPOINT = (byte) 0x81; 
private static long TIMEOUT = 5000; 
private final static int INTERFACE = 0; 
private final static Object CONNECT_HEADER = 000; 
private final static Object CONNECT_BODY = 000; 

public static UsbDevice getHygrometerDevice(UsbHub hub) { 
    UsbDevice launcher = null; 

    for (Object object : hub.getAttachedUsbDevices()) { 
     UsbDevice device = (UsbDevice) object; 
     if (device.isUsbHub()) { 
      launcher = getHygrometerDevice((UsbHub) device); 
      if (launcher != null) 
       return launcher; 
     } else { 
      UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor(); 
      if (desc.idVendor() == VENDOR_ID && desc.idProduct() == PRODUCT_ID) 
       return device; 
     } 
    } 
    return null; 
} 

public static char readKey() { 
    try { 
     String line = new BufferedReader(new InputStreamReader(System.in)).readLine(); 
     if (line.length() > 0) 
      return line.charAt(0); 
     return 0; 
    } catch (IOException e) { 
     throw new RuntimeException("Unable to read key", e); 
    } 
} 

public static ByteBuffer read(DeviceHandle handle, int size) { 
    ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(ByteOrder.LITTLE_ENDIAN); 
    IntBuffer transferred = BufferUtils.allocateIntBuffer(); 
    int result = LibUsb.bulkTransfer(handle, IN_ENDPOINT, buffer, transferred, TIMEOUT); 
    if (result != LibUsb.SUCCESS) { 
     throw new LibUsbException("Unable to read data", result); 
    } 
    System.out.println(transferred.get() + " bytes read from device"); 
    return buffer; 
} 

public static void main(String[] args) { 
    // Search for the missile launcher USB device and stop when not found 
    UsbDevice device; 
    try { 
     device = getHygrometerDevice(UsbHostManager.getUsbServices().getRootUsbHub()); 

     if (device == null) { 
      System.err.println("Missile launcher not found."); 
      System.exit(1); 
      return; 
     } 

     // Claim the interface 
     UsbConfiguration configuration = device.getUsbConfiguration((byte) 1); 
     UsbInterface iface = configuration.getUsbInterface((byte) INTERFACE); 

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

     iface.getUsbEndpoint(IN_ENDPOINT).getUsbPipe().addUsbPipeListener(new UsbPipeListener() { 

      @Override 
      public void errorEventOccurred(UsbPipeErrorEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void dataEventOccurred(UsbPipeDataEvent arg0) { 
       for (byte b : arg0.getData()) 
        System.out.print(b); 
       System.out.print("\n"); 
      } 
     }); 
     ; 
    } catch (UsbNotActiveException | UsbDisconnectedException | UsbException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    // Deinitialize the libusb context 
    LibUsb.exit(null); 
} 

} 

任意の提案を下回っているライン122次のエラー報告

Exception in thread "main" java.lang.IllegalStateException: default context is not initialized at org.usb4java.Libusb.exit(Native Method) at testnew.main(testnew.java:122) 

を得ましたか。

答えて

1

あなたがパイプを開く方法を

http://www.programcreek.com/java-api-examples/index.php?api=org.usb4javaLibUsb例17

public static void main(String[] args) 
{ 
    //Initialize the libusb context 

int result = LibUsb.Init(null); 
if (result != LibUsb.SUCCESS){ 
    throw new LibUsbException("Unable to initialze libusb",result); 
} 
[...] 
を参照してください、あなたはそれをdeinitializeする際にエラーが発生した理由ですコンテキストを初期化しませんでしたが(あなたが選択したい転送タイプによって異なりますバルク転送、sychronous転送、非同期転送)、

UsbEndpoint endpoint = iface.getUsbEndpoint((byte) 0x83); 
UsbPipe pipe = endpoint.getUsbPipe(); 
pipe.open(); 
try 
{ 
    byte[] data = new byte[8]; 
    int received = pipe.syncSubmit(data); 
    System.out.println(received + " bytes received"); 
} 
finally 
{ 
    pipe.close(); 
} 
http://usb4java.org/quickstart/javax-usb.htmlからコピー(あなたが使用することができます同期転送のために http://usb4java.org/quickstart/javax-usb.html

を見ます

INエンドポイントとOUTエンドポイントがあり、OUTに書き込み、INから読み取ります。制御転送はEP0に進みます。すべてのUSB通信はホストデバイスによって開始されます。つまり、USBデバイスは通信を開始することすらできません。

USBプロトコルの詳細については、http://www.beyondlogic.org/usbnutshell/usb1.shtml

+0

を参照してください。それは簡単な間違いでした.. どうすればいいですか?パイプからデータを読みたいのですが、最初にパイプを開く必要があります。どこで?そして、私はどのようにデータを読みますか? – Xinren

+0

編集の回答... –

関連する問題