Skip to content

Commit

Permalink
added syncRead with offset except in FTDI
Browse files Browse the repository at this point in the history
  • Loading branch information
felHR85 committed Jul 7, 2019
1 parent 1e68f74 commit fdfb9f1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
59 changes: 59 additions & 0 deletions usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,65 @@ public int syncRead(byte[] buffer, int timeout)
return readen;
}

@Override
public int syncRead(byte[] buffer, int offset, int length, int timeout) {
/* TODO
long beginTime = System.currentTimeMillis();
long stopTime = beginTime + timeout;
if(asyncMode)
{
return -1;
}
if(buffer == null)
{
return 0;
}
int n = buffer.length / 62;
if(buffer.length % 62 != 0)
{
n++;
}
byte[] tempBuffer = new byte[buffer.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, 0, buffer.length);
int p = numberBytes / 64;
if(numberBytes % 64 != 0)
{
p++;
}
readen = numberBytes - p * 2;
}
}while(readen <= 0);
return readen;
*/
return 0;
}

private static final byte[] skip = new byte[2];

/**
Expand Down
30 changes: 30 additions & 0 deletions usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -216,6 +217,35 @@ public int syncRead(byte[] buffer, int timeout)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fdfb9f1

Please sign in to comment.