I am developing an Android application using the USB Host API connected to the FT232 USB chip. The FT232 device transfers a large number of samples (20 Kbps) to Android SBC. However, I noticed that sometimes I lose some samples. I have a USB analyzer connected to view the connection between two endpoints. I see a delay in some of the USB packages.
FT232 USB packets are 64 KB in size, and my stream repeatedly calls apt bulktransfer to get the next packet and fill up the circular buffer. I have another stream that reads data from a circular buffer. Both streams are synchronized when reading and writing to the ring buffer. I'm not sure if the read stream is too long, which causes the message stream to skip some of the packages? Any ideas or suggestions for improving this?
Baud Rate 460800, 8, N, 1
The following is a snippet of code.
public class UsbClass { private static final int MAX_BUFFER_SIZE = 512000; private byte[] circularBuffer = new byte[MAX_BUFFER_SIZE]; private int writeIndex=0; private int readIndex=0; ReadUsbThread usbReadThread; ParseDataThread parseThread; public UsbClass() { super(); usbReadThread = new ReadUsbThread(); usbReadThread.start(); parseThread = new ParseDataThread(); parseThread.start(); } // Read data from USB endpoint public class ReadUsbThread extends Thread { public ReadUsbThread() { } int length; byte[] buffer = new byte[64]; public void run() { while(true) { length = conn.bulkTransfer(epIN, buffer, buffer.length, 100); if(length>2) { writeCircularBuffer(buffer, length); } else Thread.sleep(1); } } } // Parse the data from circular buffer public class ParseDataThread extends Thread { public ParseDataThread() { } byte[] data; public void run() { while(true) { data = readCircularBuffer(1); // do something with data } } } // Write to circular buffer public synchronized void writeCircularBuffer(byte[] buffer, int length) { for(int i = 2; i<length; i++) { circularBuffer[writeIndex++] = buffer[i]; if(writeIndex == MAX_BUFFER_SIZE) writeIndex=0; } } // Read from circular buffer public synchronized byte[] readCircularBuffer(int length) { byte[] buffer = new byte[length]; for(int i = 0; i<length; i++) { buffer[i] = circularBuffer[readIndex++]; if(readIndex == MAX_BUFFER_SIZE) readIndex=0; } return buffer; }
}
source share