kopia lustrzana https://github.com/felHR85/UsbSerial
avoiding 16kb limit in bulktransfers
rodzic
9a30dfb93b
commit
ff9f314406
|
@ -7,8 +7,9 @@ import junit.framework.TestCase;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,46 +38,96 @@ public class SerialBufferTest extends TestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBigSimpleWriteBuffer(){
|
public void testBigSimpleWriteBuffer(){
|
||||||
Arrays.fill(bigBuffer, (byte) 0x0A);
|
try {
|
||||||
serialBuffer = new SerialBuffer(true);
|
new Random().nextBytes(bigBuffer);
|
||||||
serialBuffer.putWriteBuffer(bigBuffer);
|
serialBuffer = new SerialBuffer(true);
|
||||||
byte[] dataReceived = serialBuffer.getWriteBuffer();
|
serialBuffer.putWriteBuffer(bigBuffer);
|
||||||
Assert.assertArrayEquals(bigBuffer, dataReceived);
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
|
||||||
|
|
||||||
|
while(outputStream.size() < bigBuffer.length){
|
||||||
|
byte[] srcData = serialBuffer.getWriteBuffer();
|
||||||
|
outputStream.write(srcData);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] srcBuffered = outputStream.toByteArray();
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(bigBuffer, srcBuffered);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuperBigSimpleWriteBuffer(){
|
public void testSuperBigSimpleWriteBuffer(){
|
||||||
new Random().nextBytes(bigBuffer2);
|
try {
|
||||||
serialBuffer = new SerialBuffer(true);
|
new Random().nextBytes(bigBuffer2);
|
||||||
serialBuffer.putWriteBuffer(bigBuffer2);
|
serialBuffer = new SerialBuffer(true);
|
||||||
byte[] dataReceived = serialBuffer.getWriteBuffer();
|
serialBuffer.putWriteBuffer(bigBuffer2);
|
||||||
Assert.assertArrayEquals(bigBuffer2, dataReceived);
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
|
||||||
|
|
||||||
|
while(outputStream.size() < bigBuffer2.length){
|
||||||
|
byte[] srcData = serialBuffer.getWriteBuffer();
|
||||||
|
outputStream.write(srcData);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] srcBuffered = outputStream.toByteArray();
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(bigBuffer2, srcBuffered);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleWriteBufferAsync1(){
|
public void testSimpleWriteBufferAsync1(){
|
||||||
new Random().nextBytes(bigBuffer2);
|
try {
|
||||||
serialBuffer = new SerialBuffer(true);
|
new Random().nextBytes(bigBuffer2);
|
||||||
|
serialBuffer = new SerialBuffer(true);
|
||||||
|
|
||||||
WriterThread writerThread = new WriterThread(serialBuffer, bigBuffer2);
|
WriterThread writerThread = new WriterThread(serialBuffer, bigBuffer2);
|
||||||
writerThread.start();
|
writerThread.start();
|
||||||
|
|
||||||
while(writerThread.getState() != Thread.State.TERMINATED){/*Busy waiting*/}
|
while(writerThread.getState() != Thread.State.TERMINATED){/*Busy waiting*/}
|
||||||
|
|
||||||
byte[] dataReceived = serialBuffer.getWriteBuffer();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
|
||||||
Assert.assertArrayEquals(bigBuffer2, dataReceived);
|
|
||||||
|
while(outputStream.size() < bigBuffer2.length){
|
||||||
|
byte[] srcData = serialBuffer.getWriteBuffer();
|
||||||
|
outputStream.write(srcData);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] dataReceived = outputStream.toByteArray();
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(bigBuffer2, dataReceived);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleWriteBufferAsync2(){
|
public void testSimpleWriteBufferAsync2(){
|
||||||
new Random().nextBytes(bigBuffer2);
|
try {
|
||||||
serialBuffer = new SerialBuffer(true);
|
new Random().nextBytes(bigBuffer2);
|
||||||
|
serialBuffer = new SerialBuffer(true);
|
||||||
|
|
||||||
WriterThread writerThread = new WriterThread(serialBuffer, bigBuffer2, Thread.currentThread());
|
WriterThread writerThread = new WriterThread(serialBuffer, bigBuffer2, Thread.currentThread());
|
||||||
writerThread.start();
|
writerThread.start();
|
||||||
|
|
||||||
byte[] dataReceived = serialBuffer.getWriteBuffer();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
|
||||||
Assert.assertArrayEquals(bigBuffer2, dataReceived);
|
|
||||||
|
while(outputStream.size() < bigBuffer2.length){
|
||||||
|
byte[] srcData = serialBuffer.getWriteBuffer();
|
||||||
|
outputStream.write(srcData);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] dataReceived = outputStream.toByteArray();
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(bigBuffer2, dataReceived);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing ReadBuffer
|
// Testing ReadBuffer
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.felhr.usbserial;
|
package com.felhr.usbserial;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@ -8,6 +9,7 @@ import okio.Buffer;
|
||||||
public class SerialBuffer
|
public class SerialBuffer
|
||||||
{
|
{
|
||||||
static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||||
|
static final int MAX_BULK_BUFFER = 16 * 1024;
|
||||||
private ByteBuffer readBuffer;
|
private ByteBuffer readBuffer;
|
||||||
private SynchronizedBuffer writeBuffer;
|
private SynchronizedBuffer writeBuffer;
|
||||||
private byte[] readBufferCompatible; // Read buffer for android < 4.2
|
private byte[] readBufferCompatible; // Read buffer for android < 4.2
|
||||||
|
@ -117,8 +119,17 @@ public class SerialBuffer
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
byte[] dst;
|
||||||
byte[] dst = buffer.readByteArray();
|
if(buffer.size() <= MAX_BULK_BUFFER){
|
||||||
|
dst = buffer.readByteArray();
|
||||||
|
}else{
|
||||||
|
try {
|
||||||
|
dst = buffer.readByteArray(MAX_BULK_BUFFER);
|
||||||
|
} catch (EOFException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(debugging)
|
if(debugging)
|
||||||
UsbSerialDebugger.printLogGet(dst, true);
|
UsbSerialDebugger.printLogGet(dst, true);
|
||||||
|
|
|
@ -29,7 +29,7 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
protected final UsbDevice device;
|
protected final UsbDevice device;
|
||||||
protected final UsbDeviceConnection connection;
|
protected final UsbDeviceConnection connection;
|
||||||
|
|
||||||
protected static final int USB_TIMEOUT = 5000;
|
protected static final int USB_TIMEOUT = 0;
|
||||||
|
|
||||||
protected SerialBuffer serialBuffer;
|
protected SerialBuffer serialBuffer;
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue