kopia lustrzana https://github.com/felHR85/UsbSerial
Runka
rodzic
d369c0d31d
commit
b5f7699a52
|
@ -2,12 +2,14 @@ package com.felhr.usbserial;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbRequest;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import com.felhr.utils.SafeUsbRequest;
|
||||
|
@ -626,6 +628,10 @@ public class FTDISerialDevice extends UsbSerialDevice
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (mr1Version) {
|
||||
return readSyncJelly(buffer, timeout, stopTime);
|
||||
}
|
||||
|
||||
int n = buffer.length / 62;
|
||||
if(buffer.length % 62 != 0)
|
||||
{
|
||||
|
@ -666,4 +672,40 @@ public class FTDISerialDevice extends UsbSerialDevice
|
|||
|
||||
return readen;
|
||||
}
|
||||
|
||||
private static final byte[] skip = new byte[2];
|
||||
|
||||
/**
|
||||
* This method avoids creation of garbage by reusing the same
|
||||
* array instance for skipping header bytes and running
|
||||
* {@link UsbDeviceConnection#bulkTransfer(UsbEndpoint, byte[], int, int, int)}
|
||||
* directly.
|
||||
*/
|
||||
@SuppressLint("NewApi")
|
||||
private int readSyncJelly(byte[] buffer, int timeout, long stopTime) {
|
||||
int read = 0;
|
||||
do
|
||||
{
|
||||
int timeLeft = 0;
|
||||
if(timeout > 0)
|
||||
{
|
||||
timeLeft = (int) (stopTime - System.currentTimeMillis());
|
||||
if (timeLeft <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int numberBytes = connection.bulkTransfer(inEndpoint, skip, skip.length, timeLeft);
|
||||
|
||||
if(numberBytes > 2) // Data received
|
||||
{
|
||||
numberBytes = connection.bulkTransfer(inEndpoint, buffer, read, 62, timeLeft);
|
||||
read += numberBytes;
|
||||
}
|
||||
} while(read <= 0);
|
||||
|
||||
return read;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
|
||||
protected static final String COM_PORT = "COM ";
|
||||
|
||||
private static final boolean mr1Version;
|
||||
// Android version < 4.3 It is not going to be asynchronous read operations
|
||||
static final boolean mr1Version =
|
||||
android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
|
||||
protected final UsbDevice device;
|
||||
protected final UsbDeviceConnection connection;
|
||||
|
||||
|
@ -47,15 +49,6 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
private String portName = "";
|
||||
protected boolean isOpen;
|
||||
|
||||
// Get Android version if version < 4.3 It is not going to be asynchronous read operations
|
||||
static
|
||||
{
|
||||
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
mr1Version = true;
|
||||
else
|
||||
mr1Version = false;
|
||||
}
|
||||
|
||||
public UsbSerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
||||
{
|
||||
this.device = device;
|
||||
|
@ -455,14 +448,13 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
|
||||
protected void setThreadsParams(UsbRequest request, UsbEndpoint endpoint)
|
||||
{
|
||||
writeThread.setUsbEndpoint(endpoint);
|
||||
if(mr1Version)
|
||||
{
|
||||
workerThread.setUsbRequest(request);
|
||||
writeThread.setUsbEndpoint(endpoint);
|
||||
}else
|
||||
{
|
||||
readThread.setUsbEndpoint(request.getEndpoint());
|
||||
writeThread.setUsbEndpoint(endpoint);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue