- reopenOnTimeout moved to write/read method

- UART read/write corrected

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@137 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-04-26 08:04:00 +00:00
parent 4cca1c2e6c
commit 7ab42064f4
3 changed files with 25 additions and 34 deletions

View File

@@ -32,7 +32,6 @@ public class USBDevice {
static { static {
dev = USB.getDevice(IdVendor, IdProduct); dev = USB.getDevice(IdVendor, IdProduct);
dev.setReopenOnTimeout(true);
dev.setResetOnFirstOpen(true); dev.setResetOnFirstOpen(true);
} }
@@ -49,19 +48,19 @@ public class USBDevice {
} }
public static void write_BDI(byte[] data, int length) throws USBException { public static void write_BDI(byte[] data, int length) throws USBException {
dev.bulkwrite(OUT_Endpoint_BDI, data, length, BDI_Timeout); dev.bulkwrite(OUT_Endpoint_BDI, data, length, BDI_Timeout, true);
} }
public static int read_BDI(byte[] data, int size) throws USBException { public static int read_BDI(byte[] data, int size) throws USBException {
return dev.bulkread(IN_Endpoint_BDI, data, size, BDI_Timeout); return dev.bulkread(IN_Endpoint_BDI, data, size, BDI_Timeout, true);
} }
public static void write_UART(byte[] data, int length) throws USBException { public static void write_UART(byte[] data, int length) throws USBException {
dev.bulkwrite(OUT_Endpoint_UART, data, length, UART_Timeout); dev.bulkwrite(OUT_Endpoint_UART, data, length, UART_Timeout, false);
} }
public static int read_UART(byte[] data, int size) throws USBException { public static int read_UART(byte[] data, int size) throws USBException {
return dev.bulkread(IN_Endpoint_UART, data, size, UART_Timeout); return dev.bulkread(IN_Endpoint_UART, data, size, UART_Timeout, false);
} }
public static int getMaxPacketSize() { public static int getMaxPacketSize() {

View File

@@ -24,11 +24,9 @@ public class Device {
private int usb_dev_handle; private int usb_dev_handle;
private boolean reopenOnTimeout, reopenDone, resetOnFirstOpen, resetDone; private boolean resetOnFirstOpen, resetDone;
protected Device(short idVendor, short idProduct) { protected Device(short idVendor, short idProduct) {
reopenOnTimeout = false;
reopenDone = false;
resetOnFirstOpen = false; resetOnFirstOpen = false;
resetDone = false; resetDone = false;
maxPacketSize = -1; maxPacketSize = -1;
@@ -172,7 +170,6 @@ public class Device {
/** /**
* Write data to the device using a bulk transfer.<br> * Write data to the device using a bulk transfer.<br>
* If reopenOnTimeout is set to true, it may take
* *
* @param out_ep_address * @param out_ep_address
* endpoint address to write to * endpoint address to write to
@@ -183,11 +180,14 @@ public class Device {
* @param timeout * @param timeout
* amount of time in ms the device will try to send the data * amount of time in ms the device will try to send the data
* until a timeout exception is thrown * until a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* send the data again before a timeout exception is thrown
* @return the actual number of bytes written * @return the actual number of bytes written
* @throws USBException * @throws USBException
*/ */
public int bulkwrite(int out_ep_address, byte[] data, int length, public int bulkwrite(int out_ep_address, byte[] data, int length,
int timeout) throws USBException { int timeout, boolean reopenOnTimeout) throws USBException {
if (usb_dev_handle <= 0) { if (usb_dev_handle <= 0) {
throw new USBException("invalid device handle"); throw new USBException("invalid device handle");
} }
@@ -202,14 +202,13 @@ public class Device {
if (lenWritten < 0) { if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) { if (lenWritten == TIMEOUT_ERROR_CODE) {
// try to reopen the device and send the data again // try to reopen the device and send the data again
if (reopenOnTimeout & !reopenDone) { if (reopenOnTimeout) {
logger.info("try to reopen"); logger.info("try to reopen");
reset(); reset();
open(configuration, interface_, altinterface); open(configuration, interface_, altinterface);
reopenDone = true; return bulkwrite(out_ep_address, data, length, timeout,
return bulkwrite(out_ep_address, data, length, timeout); false);
} }
reopenDone = false;
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbWin.usb_strerror());
} }
@@ -230,6 +229,8 @@ public class Device {
} }
/** /**
* Read data from the device using a bulk transfer.<br>
*
* @param in_ep_address * @param in_ep_address
* endpoint address to read from * endpoint address to read from
* @param data * @param data
@@ -239,11 +240,14 @@ public class Device {
* @param timeout * @param timeout
* amount of time in ms the device will try to receive data until * amount of time in ms the device will try to receive data until
* a timeout exception is thrown * a timeout exception is thrown
* @param reopenOnTimeout
* if set to true, the device will try to open the connection and
* receive the data again before a timeout exception is thrown
* @return the actual number of bytes read * @return the actual number of bytes read
* @throws USBException * @throws USBException
*/ */
public int bulkread(int in_ep_address, byte[] data, int size, int timeout) public int bulkread(int in_ep_address, byte[] data, int size, int timeout,
throws USBException { boolean reopenOnTimeout) throws USBException {
if (usb_dev_handle <= 0) { if (usb_dev_handle <= 0) {
throw new USBException("invalid device handle"); throw new USBException("invalid device handle");
} }
@@ -259,14 +263,13 @@ public class Device {
if (lenRead == TIMEOUT_ERROR_CODE) { if (lenRead == TIMEOUT_ERROR_CODE) {
if (lenRead == TIMEOUT_ERROR_CODE) { if (lenRead == TIMEOUT_ERROR_CODE) {
// try to reopen the device and send the data again // try to reopen the device and send the data again
if (reopenOnTimeout & !reopenDone) { if (reopenOnTimeout) {
logger.info("try to reopen"); logger.info("try to reopen");
reset(); reset();
open(configuration, interface_, altinterface); open(configuration, interface_, altinterface);
reopenDone = true; return bulkwrite(in_ep_address, data, size, timeout,
return bulkwrite(in_ep_address, data, size, timeout); false);
} }
reopenDone = false;
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
+ LibusbWin.usb_strerror()); + LibusbWin.usb_strerror());
} }
@@ -397,18 +400,6 @@ public class Device {
return maxPacketSize; return maxPacketSize;
} }
/**
* Before a timeout exception is thrown (after issuing a read or write
* command), the device will try to open the connection and send or receive
* the data again.<br>
* The default value is false.
*
* @param enable
*/
public void setReopenOnTimeout(boolean enable) {
reopenOnTimeout = enable;
}
/** /**
* If enabled, the device is reset when first opened. This will only happen * If enabled, the device is reset when first opened. This will only happen
* once. * once.

View File

@@ -69,7 +69,8 @@ public class TestImplementation {
static void write(byte[] data, int length) { static void write(byte[] data, int length) {
int lenWritten = 0; int lenWritten = 0;
try { try {
lenWritten = dev.bulkwrite(OUT_ENDPOINT, data, length, TIMEOUT); lenWritten = dev.bulkwrite(OUT_ENDPOINT, data, length, TIMEOUT,
false);
StringBuffer sb = new StringBuffer("write_bulkdata: " + lenWritten StringBuffer sb = new StringBuffer("write_bulkdata: " + lenWritten
+ " Bytes sent: "); + " Bytes sent: ");
for (int i = 0; i < lenWritten; i++) { for (int i = 0; i < lenWritten; i++) {
@@ -86,7 +87,7 @@ public class TestImplementation {
int lenRead = 0; int lenRead = 0;
try { try {
lenRead = dev.bulkread(IN_ENDPOINT, data, dev.getMaxPacketSize(), lenRead = dev.bulkread(IN_ENDPOINT, data, dev.getMaxPacketSize(),
TIMEOUT); TIMEOUT, false);
StringBuffer sb = new StringBuffer("read_bulkdata: " + lenRead StringBuffer sb = new StringBuffer("read_bulkdata: " + lenRead
+ " Bytes received: Data: "); + " Bytes received: Data: ");
for (int i = 0; i < lenRead; i++) { for (int i = 0; i < lenRead; i++) {