Android Question felUsbSerial v1.12 Error at random intervals ArrayIndexOutOfBoundsException, and lost characters

weighment

Member
Licensed User
Using felUsbSerial - v1.12 on Android 8.1 with FTDI PID 0x6010

The serial communication works for a variable length of time before an exception in the library.

Connecting to a serial device, requesting data from it every half second or so, data out is 10 or so characters, data in is usually 10 or so characters, occasionally 100 characters in a burst.
Randomly, anywhere from within 10 seconds of connection, to as long as 5 minutes later I get a Array index out of bounds error shown below. For the input buffer size, I see the "Default value is 16 * 1024", and this app has no where near enough data to overflow the buffer. (I doubled it to 32*1024 and still had the error) Is the buffer argument sent to the event only 60 bytes? In observation I never see it have more than 20 characters at a time sent to the event in normal operation of the app.

What is the best way to trap this exception and prevent it from taking down the app? Ideally I'd like to detect the error and recover.

Also, regularly losing 2 or three characters from messages. Occurs on a similar random interval. May or may not be related.

Is source code for this library available to help solve it?

B4X:
    Public usbserial As felUsbSerial

Error message:
java.lang.ArrayIndexOutOfBoundsException: length=61; index=61
    at com.felhr.usbserial.FTDISerialDevice$FTDIUtilities.copyData(FTDISerialDevice.java:594)
    at com.felhr.usbserial.FTDISerialDevice$FTDIUtilities.adaptArray(FTDISerialDevice.java:502)
    at com.felhr.usbserial.UsbSerialDevice$WorkerThread.run(UsbSerialDevice.java:248)
 

Daestrum

Expert
Licensed User
Longtime User
What about replacing serialbuffer with Erels version.

Changed to use Erels version of serialbuffer but the rest of source code is UsbSerial-6.1.0

And I still forgot to change the version number.
 

Attachments

  • felUsbSerial.jar
    87.5 KB · Views: 65
  • felUsbSerial.xml
    7.3 KB · Views: 63
Last edited:
Upvote 0

weighment

Member
Licensed User
Worth a shot @Daestrum, it builds and runs, but getting this error at the first attempt to write

IllegalArgumentException:
java.lang.IllegalArgumentException: 0 > -1
    at java.util.Arrays.copyOfRange(Arrays.java:3447)
    at com.felhr.usbserial.SerialBuffer$SynchronizedBuffer.get(SerialBuffer.java:161)
    at com.felhr.usbserial.SerialBuffer.getWriteBuffer(SerialBuffer.java:93)
    at com.felhr.usbserial.UsbSerialDevice$WriteThread.doRun(UsbSerialDevice.java:402)
    at com.felhr.usbserial.AbstractWorkerThread.run(AbstractWorkerThread.java:21)

I think the best fix so far is to replace the FTDIUtilities.adaptArray in the current source here
https://github.com/AnywhereSoftware/B4A/tree/master/felUsbSerial/src
in this file on line 493:
https://github.com/AnywhereSoftware...src/com/felhr/usbserial/FTDISerialDevice.java
with this function:

adaptArray:
public byte[] adaptArray(byte[] ftdiData)
{
   if (ftdiData.length <=2)
       {
           return EMPTY_BYTE_ARRAY;
       }
       else {
           return Arrays.copyOfRange(ftdiData, 2, ftdiData.length-2);
       }
}
 
Upvote 0
Top