kopia lustrzana https://github.com/felHR85/UsbSerial
Merge pull request #262 from felHR85/rabidaudio-outputstream-fix
Added sync and streams read/write methods with offsetspull/270/head
commit
b967da3c54
|
@ -689,6 +689,63 @@ public class FTDISerialDevice extends UsbSerialDevice
|
|||
return readen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int syncRead(byte[] buffer, int offset, int length, int timeout) {
|
||||
|
||||
long beginTime = System.currentTimeMillis();
|
||||
long stopTime = beginTime + timeout;
|
||||
|
||||
if(asyncMode)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(buffer == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int n = length / 62;
|
||||
if(length % 62 != 0)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
|
||||
byte[] tempBuffer = new byte[length + n * 2];
|
||||
|
||||
int readen = 0;
|
||||
|
||||
do
|
||||
{
|
||||
int timeLeft = 0;
|
||||
if(timeout > 0)
|
||||
{
|
||||
timeLeft = (int) (stopTime - System.currentTimeMillis());
|
||||
if (timeLeft <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int numberBytes = connection.bulkTransfer(inEndpoint, tempBuffer, tempBuffer.length, timeLeft);
|
||||
|
||||
if(numberBytes > 2) // Data received
|
||||
{
|
||||
byte[] newBuffer = this.ftdiUtilities.adaptArray(tempBuffer);
|
||||
System.arraycopy(newBuffer, 0, buffer, offset, length);
|
||||
|
||||
int p = numberBytes / 64;
|
||||
if(numberBytes % 64 != 0)
|
||||
{
|
||||
p++;
|
||||
}
|
||||
readen = numberBytes - p * 2;
|
||||
}
|
||||
}while(readen <= 0);
|
||||
|
||||
return readen;
|
||||
}
|
||||
|
||||
private static final byte[] skip = new byte[2];
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,6 +54,28 @@ public class SerialInputStream extends InputStream
|
|||
return device.syncRead(b, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte b[], int off, int len)
|
||||
{
|
||||
if(off < 0 ){
|
||||
throw new IndexOutOfBoundsException("Offset must be >= 0");
|
||||
}
|
||||
|
||||
if(len < 0){
|
||||
throw new IndexOutOfBoundsException("Length must positive");
|
||||
}
|
||||
|
||||
if(len > b.length - off) {
|
||||
throw new IndexOutOfBoundsException("Length greater than b.length - off");
|
||||
}
|
||||
|
||||
if (off == 0 && len == b.length) {
|
||||
return read(b);
|
||||
}
|
||||
|
||||
return device.syncRead(b, off, len, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
if(bufferSize > 0)
|
||||
|
|
|
@ -25,6 +25,29 @@ public class SerialOutputStream extends OutputStream
|
|||
device.syncWrite(b, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte b[], int off, int len)
|
||||
{
|
||||
if(off < 0 ){
|
||||
throw new IndexOutOfBoundsException("Offset must be >= 0");
|
||||
}
|
||||
|
||||
if(len < 0){
|
||||
throw new IndexOutOfBoundsException("Length must positive");
|
||||
}
|
||||
|
||||
if(off + len > b.length) {
|
||||
throw new IndexOutOfBoundsException("off + length greater than buffer length");
|
||||
}
|
||||
|
||||
if (off == 0 && len == b.length) {
|
||||
write(b);
|
||||
return;
|
||||
}
|
||||
|
||||
device.syncWrite(b, off, len, timeout);
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.felhr.deviceids.CP210xIds;
|
|||
import com.felhr.deviceids.FTDISioIds;
|
||||
import com.felhr.deviceids.PL2303Ids;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
|
@ -216,6 +217,35 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
|||
return connection.bulkTransfer(inEndpoint, buffer, buffer.length, timeout);
|
||||
}
|
||||
|
||||
@TargetApi(18)
|
||||
@Override
|
||||
public int syncWrite(byte[] buffer, int offset, int length, int timeout) {
|
||||
if(!asyncMode)
|
||||
{
|
||||
if(buffer == null)
|
||||
return 0;
|
||||
|
||||
return connection.bulkTransfer(outEndpoint, buffer, offset, length, timeout);
|
||||
}else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(18)
|
||||
@Override
|
||||
public int syncRead(byte[] buffer, int offset, int length, int timeout) {
|
||||
if(asyncMode)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (buffer == null)
|
||||
return 0;
|
||||
|
||||
return connection.bulkTransfer(inEndpoint, buffer, offset, length, timeout);
|
||||
}
|
||||
|
||||
// Serial port configuration
|
||||
@Override
|
||||
public abstract void setBaudRate(int baudRate);
|
||||
|
|
|
@ -38,6 +38,8 @@ public interface UsbSerialInterface
|
|||
boolean syncOpen();
|
||||
int syncWrite(byte[] buffer, int timeout);
|
||||
int syncRead(byte[] buffer, int timeout);
|
||||
int syncWrite(byte[] buffer, int offset, int length, int timeout);
|
||||
int syncRead(byte[] buffer, int offset, int length, int timeout);
|
||||
void syncClose();
|
||||
|
||||
// Serial port configuration
|
||||
|
|
Ładowanie…
Reference in New Issue