How can I detect if a device is removed? I think this is one thing you really should know when f.i. one is trying to send data to the device to avoid exceptions. If this isn't included (yet), you might want to have a look at this: http://osdir.com/ml/lib.libusb.devel.windows/2004-09/msg00002.html.
Best regards,
RoDeNtJe
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, there is the possibility to detect device removal on Windows. But there are several reasons why that has not been implemented:
- not defined in the standard libusb API
- not portable
- with exceptions you can detect device removals (of course just when reading/writing to/from the device)
It's not planned to implement this feature, but of course you can extend the shared library code with the code from the testlibusb_win.c and write some kind of Java listener thing.
Regards, Spandi
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The goal for the device removal detection was to be able to send and receive data from the moment the device is replugged without a hanging application or so. So here maybe a little contribution (the instantiationFlag is meant to make a Singleton class (only one instance of a class is possible at the same time):
public int receiveData(byte[] data) throws USBException{
int ret = -1;
if(data.length<=64){
if(dev==null && instantiationFlag) initUSBComm();
if(dev==null) throw new USBException("Not Connected");
try {
// read some data from the device
// 0x81 is the endpoint address of the IN endpoint 4 (from PC to device)
// bit 7 (0x80) is set in case of an IN endpoint
ret = dev.readBulk(0x81, data, data.length, 2000, false);
} catch (USBTimeoutException e1){
} catch (USBException e){
reconnect();
try {
ret = dev.readBulk(0x81, data, data.length, 2000, false);
} catch (USBException e2) {}
}
}
return ret;
}
This is quite helpful indeed.
I just amend the initUSBComm(). I call initUSBComm() again in exception to keep looking for the device(a scanner).
public void initUSBComm() {
try{
dev = USB.getDevice(vendorID, productID);
dev.open(1, 0, -1);
String msg = "Device Open : "+ dev.isOpen();
System.out.println(msg);
}
catch(USBException ue)
{
System.out.println("Re-initUSBComm().....");
initUSBComm();//<-- To keep looping
}
}
It is working fine but few problem still occur.
(but problems only concern with Barcode Scanner as I am currently working on it)
1)-For normal case without running my app, when scan.. keyPressed() event is triggered and
Digits from barcode is shown in the text-field where the cursor is.
-For normal case while running my app, when scan.. keyPressed() event is NOT triggered
and Digits from barcode is NOT shown in the text-field where the cursor is. And I
recieved complete number of bytes.
-However sometimes while running my app, when scan.. keyPressed() event is
PARTIALLY/TOTALLY triggered and Digits from barcode is PARTIALLY/TOTALLY shown. And I
fail to recieve the bytes of those shown Digits.
-Worst case is sometimes Non-Stop keyPressed() event occurs and it never stops though I
shut down my app.
Regards,
Swe Han
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
How can I detect if a device is removed? I think this is one thing you really should know when f.i. one is trying to send data to the device to avoid exceptions. If this isn't included (yet), you might want to have a look at this: http://osdir.com/ml/lib.libusb.devel.windows/2004-09/msg00002.html.
Best regards,
RoDeNtJe
Hi RoDeNtJe
Yes, there is the possibility to detect device removal on Windows. But there are several reasons why that has not been implemented:
- not defined in the standard libusb API
- not portable
- with exceptions you can detect device removals (of course just when reading/writing to/from the device)
It's not planned to implement this feature, but of course you can extend the shared library code with the code from the testlibusb_win.c and write some kind of Java listener thing.
Regards, Spandi
Hi,
The goal for the device removal detection was to be able to send and receive data from the moment the device is replugged without a hanging application or so. So here maybe a little contribution (the instantiationFlag is meant to make a Singleton class (only one instance of a class is possible at the same time):
import ch.ntb.usb.Device;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.USBTimeoutException;
public class USBComm {
private Device dev;
private static boolean instantiationFlag = false;
private USBComm(){}
static public USBComm instantiate(){
if (!instantiationFlag){
instantiationFlag = true;
return new USBComm();
} else
return null;
}
public void finalize(){
instantiationFlag = false;
try {
if(dev!=null){
dev.close();
dev = null;
}
} catch (USBException e) {}
}
public void initUSBComm() throws USBException{
dev = USB.getDevice((short) 0x1FF1, (short) 0x1FF1);
dev.open(1, 0, -1);
}
public void sendData(byte[] data) throws USBException{
if(data.length<=64){
if(dev==null && instantiationFlag) initUSBComm();
if(dev==null) throw new USBException("Not Connected");
try {
dev.writeInterrupt(0x01, data, data.length, 2000, false);
} catch (USBException e1) {
reconnect();
try {
dev.writeInterrupt(0x01, data, data.length, 2000, false);
} catch (USBException e2) {}
}
}
}
public int receiveData(byte[] data) throws USBException{
int ret = -1;
if(data.length<=64){
if(dev==null && instantiationFlag) initUSBComm();
if(dev==null) throw new USBException("Not Connected");
try {
// read some data from the device
// 0x81 is the endpoint address of the IN endpoint 4 (from PC to device)
// bit 7 (0x80) is set in case of an IN endpoint
ret = dev.readBulk(0x81, data, data.length, 2000, false);
} catch (USBTimeoutException e1){
} catch (USBException e){
reconnect();
try {
ret = dev.readBulk(0x81, data, data.length, 2000, false);
} catch (USBException e2) {}
}
}
return ret;
}
private void reconnect(){
if(dev!=null){
try {
dev.close();
} catch (USBException e1) {
} finally{
try {
initUSBComm();
} catch (USBException e2) {}
}
}
}
}
______________________________________________
so setting-up the communication:
USBComm usbComm = USBComm.instantiate();
usbComm.initUSBComm();
______________________________________________
sending data:
try {
usbComm.sendData(arrayOfBytes);
} catch (USBException e) {}
______________________________________________
receiving data (I made a seperate class for it which runs in a seperate thread):
public void run() {
byte[] data = new byte[64];;
while(enabled){
if(usbComm != null){
try {
int numberOfBytesRead = usbComm.receiveData(data);
for(int i=0;i<numberOfBytesRead;i++)
if(enabled)
System.out.println((char)data[i]);
} catch (USBException e) {}
}
}
}
Hope this contributes to this nice project.
If you notice any errors or have any suggestions, please reply.
Best regards,
RoDeNtJe
This is quite helpful indeed.
I just amend the initUSBComm(). I call initUSBComm() again in exception to keep looking for the device(a scanner).
public void initUSBComm() {
try{
dev = USB.getDevice(vendorID, productID);
dev.open(1, 0, -1);
String msg = "Device Open : "+ dev.isOpen();
System.out.println(msg);
}
catch(USBException ue)
{
System.out.println("Re-initUSBComm().....");
initUSBComm();//<-- To keep looping
}
}
It is working fine but few problem still occur.
(but problems only concern with Barcode Scanner as I am currently working on it)
1)-For normal case without running my app, when scan.. keyPressed() event is triggered and
Digits from barcode is shown in the text-field where the cursor is.
-For normal case while running my app, when scan.. keyPressed() event is NOT triggered
and Digits from barcode is NOT shown in the text-field where the cursor is. And I
recieved complete number of bytes.
-However sometimes while running my app, when scan.. keyPressed() event is
PARTIALLY/TOTALLY triggered and Digits from barcode is PARTIALLY/TOTALLY shown. And I
fail to recieve the bytes of those shown Digits.
-Worst case is sometimes Non-Stop keyPressed() event occurs and it never stops though I
shut down my app.
Regards,
Swe Han