diff --git a/mcdp/src/ch/ntb/mcdp/usb/USBDevice.java b/mcdp/src/ch/ntb/mcdp/usb/USBDevice.java index af526cb..005b795 100644 --- a/mcdp/src/ch/ntb/mcdp/usb/USBDevice.java +++ b/mcdp/src/ch/ntb/mcdp/usb/USBDevice.java @@ -32,7 +32,6 @@ public class USBDevice { static { dev = USB.getDevice(IdVendor, IdProduct); - dev.setReopenOnTimeout(true); dev.setResetOnFirstOpen(true); } @@ -49,19 +48,19 @@ public class USBDevice { } 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 { - 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 { - 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 { - 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() { diff --git a/mcdp/src/ch/ntb/usb/Device.java b/mcdp/src/ch/ntb/usb/Device.java index 2a65a6a..6d54795 100644 --- a/mcdp/src/ch/ntb/usb/Device.java +++ b/mcdp/src/ch/ntb/usb/Device.java @@ -24,11 +24,9 @@ public class Device { private int usb_dev_handle; - private boolean reopenOnTimeout, reopenDone, resetOnFirstOpen, resetDone; + private boolean resetOnFirstOpen, resetDone; protected Device(short idVendor, short idProduct) { - reopenOnTimeout = false; - reopenDone = false; resetOnFirstOpen = false; resetDone = false; maxPacketSize = -1; @@ -172,7 +170,6 @@ public class Device { /** * Write data to the device using a bulk transfer.
- * If reopenOnTimeout is set to true, it may take * * @param out_ep_address * endpoint address to write to @@ -183,11 +180,14 @@ public class Device { * @param timeout * amount of time in ms the device will try to send the data * 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 * @throws USBException */ 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) { throw new USBException("invalid device handle"); } @@ -202,14 +202,13 @@ public class Device { if (lenWritten < 0) { if (lenWritten == TIMEOUT_ERROR_CODE) { // try to reopen the device and send the data again - if (reopenOnTimeout & !reopenDone) { + if (reopenOnTimeout) { logger.info("try to reopen"); reset(); 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: " + LibusbWin.usb_strerror()); } @@ -230,6 +229,8 @@ public class Device { } /** + * Read data from the device using a bulk transfer.
+ * * @param in_ep_address * endpoint address to read from * @param data @@ -239,11 +240,14 @@ public class Device { * @param timeout * amount of time in ms the device will try to receive data until * 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 * @throws USBException */ - public int bulkread(int in_ep_address, byte[] data, int size, int timeout) - throws USBException { + public int bulkread(int in_ep_address, byte[] data, int size, int timeout, + boolean reopenOnTimeout) throws USBException { if (usb_dev_handle <= 0) { throw new USBException("invalid device handle"); } @@ -259,14 +263,13 @@ public class Device { if (lenRead == TIMEOUT_ERROR_CODE) { if (lenRead == TIMEOUT_ERROR_CODE) { // try to reopen the device and send the data again - if (reopenOnTimeout & !reopenDone) { + if (reopenOnTimeout) { logger.info("try to reopen"); reset(); 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: " + LibusbWin.usb_strerror()); } @@ -397,18 +400,6 @@ public class Device { 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.
- * 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 * once. diff --git a/mcdp/src/ch/ntb/usb/test/TestImplementation.java b/mcdp/src/ch/ntb/usb/test/TestImplementation.java index ee8148d..36f261b 100644 --- a/mcdp/src/ch/ntb/usb/test/TestImplementation.java +++ b/mcdp/src/ch/ntb/usb/test/TestImplementation.java @@ -69,7 +69,8 @@ public class TestImplementation { static void write(byte[] data, int length) { int lenWritten = 0; 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 + " Bytes sent: "); for (int i = 0; i < lenWritten; i++) { @@ -86,7 +87,7 @@ public class TestImplementation { int lenRead = 0; try { lenRead = dev.bulkread(IN_ENDPOINT, data, dev.getMaxPacketSize(), - TIMEOUT); + TIMEOUT, false); StringBuffer sb = new StringBuffer("read_bulkdata: " + lenRead + " Bytes received: Data: "); for (int i = 0; i < lenRead; i++) {