|
From: Dara M. <d....@ca...> - 2015-08-13 16:53:14
|
Hi,
I'm new in development using javax-usb and I've got a problem which I don't understand.
I'm using javax-usb to connect a card reader to a printer.
Here is my code to claim my UsbInterface and to open an UsbPipe
protected void init() throws UsbException {
UsbConfiguration config = device.getActiveUsbConfiguration();
UsbEndpoint endptIn = null;
UsbEndpoint ep = null;
if (config != null) {
interf = config.getUsbInterface((byte) 0);
}
/** Claim the UsbInterface. */
try {
if (!interf.isClaimed()) {
interf.claim();
}
} catch (UsbClaimException e) {
e.printStackTrace();
} catch (UsbNotActiveException e) {
e.printStackTrace();
} catch (UsbDisconnectedException e) {
e.printStackTrace();
}
/** Get the endpoint. */
List eps = interf.getUsbEndpoints();
boolean foundEp = false;
for (int j = 0; j < eps.size(); j++) {
ep = (UsbEndpoint) eps.get(j);
int t = ep.getType();
Logs.debug(className, functionName, "endpointaddress: " + ep.getUsbEndpointDescriptor().bEndpointAddress());
if (t == UsbConst.ENDPOINT_TYPE_INTERRUPT
&& ep.getDirection() == UsbConst.ENDPOINT_DIRECTION_IN
&& ep.getUsbEndpointDescriptor().bEndpointAddress() == SCHOMAKER_ENDPOINT_ADDR) {
endptIn = ep;
foundEp = true;
break;
}
}
if (!foundEp) {
for (int j = 0; j < eps.size(); j++) {
ep = (UsbEndpoint) eps.get(j);
int t = ep.getType();
if (t == UsbConst.ENDPOINT_TYPE_INTERRUPT
&& ep.getDirection() == UsbConst.ENDPOINT_DIRECTION_IN) {
endptIn = ep;
foundEp = true;
break;
}
}
}
/** Get and open the UsbPipe. */
pipe = endptIn.getUsbPipe();
if (!pipe.isOpen()) {
pipe.open();
}
}
Here is how I'm reading the data:
public void startReader() {
/** We need to create the Thread-class in this method. */
new Thread() {
public void run() {
stop_flag = true;
while (stop_flag) {
try {
byte[] temp = new byte[buffer_size];
irpIn = pipe.createUsbIrp();
irpIn.setData(temp);
pipe.asyncSubmit(irpIn);
irpIn.waitUntilComplete();
byte[] buffer = new byte[irpIn.getActualLength()];
//reading the data
Thread.sleep(50);
} catch (Exception e) {
//in case of disconnect error occurs.
Logs.error(className, functionName, "exception of send command " + e);
}//end try catch
}//end while
}//end run()
}.start();
}
And when I'm trying to release:
public void releaseUsbInterface() throws UsbException, UsbDisconnectedException {
if (interf != null) {
pipe.close();
interf.release();
}
}
When I'm doing this, the code is blocked on the line interf.release() and I can't do anything.
I have to unplug manually the card reader.
Do you have any idea what is the problem?
Best regards,
Dara Mom
|