Hi,
I'm trying to read continuously data from a device in a thread. When I switch it on the thread calls dev.open(..) and dev.readInterrupt(..). When I switch the device off the thread stops.But the second time I switch it on it use the same handle and we can't read data from the device.
Does anyone khow how to proceed or how to refresh the handle with the class Device?
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can you reproduce the error with some simple single threaded code? For example: open device, read data, close device, open device, read data, close device
After closing a device you always have to open it again because the device handle will get invalid. You can also try to reset the device instead of closing it. Note that this will also invalidate the device handle.
Hope that helps...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Thanks for replying so quickly. Here is my problem : I can't close the device if I've already switch it off. So I first open the device and the thread repeats the action of reading but I don't close the device.
I've tried to do open device, read data and close device in a single thread but sometimes there is a time-out exception for readInterrupt and the device can't be closed; besides I can also switch the device off during the thread and it doesn't run anymore. Is there any way to disable the handle without closing the device?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need to distinguish between a timeout USB exception and 'normal' USB exception.
In the first case (timeout) the device is still open and working properly. There is just no data returned by the device when reading or no data can be written to the device when writing (e.g. if the devices buffers are full). You may want to try to read/write some data later. There's no need to reopen the device for that.
The 'normal' USB Exception indicates an IO problem with the device (e.g. the device has been removed or switched off). In this case you can try to reopen the device (check the error string).
So you need to handle the different exceptions and act accordingly.
You can always try to close the device and ignore exceptions which are caused by closing the device.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
When no data can be written to the device, is there any way to clear the device buffers? I tried to write somme data to the device but it always return the same time out exception.
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This highly depends on the device firmware. So there is no general rule how devices process their data, how long they keep the data in the buffer(s), how large the buffer(s) are, ...
If the device doesn't accept data after having it switched on it probably will not accept data after a reset either.
Hi,
I'm trying to read continuously data from a device in a thread. When I switch it on the thread calls dev.open(..) and dev.readInterrupt(..). When I switch the device off the thread stops.But the second time I switch it on it use the same handle and we can't read data from the device.
Does anyone khow how to proceed or how to refresh the handle with the class Device?
Thanks.
Hi
Can you reproduce the error with some simple single threaded code? For example: open device, read data, close device, open device, read data, close device
After closing a device you always have to open it again because the device handle will get invalid. You can also try to reset the device instead of closing it. Note that this will also invalidate the device handle.
Hope that helps...
Hi,
Thanks for replying so quickly. Here is my problem : I can't close the device if I've already switch it off. So I first open the device and the thread repeats the action of reading but I don't close the device.
I've tried to do open device, read data and close device in a single thread but sometimes there is a time-out exception for readInterrupt and the device can't be closed; besides I can also switch the device off during the thread and it doesn't run anymore. Is there any way to disable the handle without closing the device?
Thanks
Hi
You need to distinguish between a timeout USB exception and 'normal' USB exception.
In the first case (timeout) the device is still open and working properly. There is just no data returned by the device when reading or no data can be written to the device when writing (e.g. if the devices buffers are full). You may want to try to read/write some data later. There's no need to reopen the device for that.
The 'normal' USB Exception indicates an IO problem with the device (e.g. the device has been removed or switched off). In this case you can try to reopen the device (check the error string).
So you need to handle the different exceptions and act accordingly.
You can always try to close the device and ignore exceptions which are caused by closing the device.
Hi
When no data can be written to the device, is there any way to clear the device buffers? I tried to write somme data to the device but it always return the same time out exception.
Thanks
Hi
This highly depends on the device firmware. So there is no general rule how devices process their data, how long they keep the data in the buffer(s), how large the buffer(s) are, ...
If the device doesn't accept data after having it switched on it probably will not accept data after a reset either.
You can try to reset the device with http://libusbjava.sourceforge.net/wp/res/doc/ch/ntb/usb/Device.html#reset\()
This should bring the device to an initial state (how a device handles the reset is also device dependant).
Note that you need to reopen the device after a reset.