Workaround for cp210x chipset issues.
SmartBotKit/serial/master This commit looks good

This commit is contained in:
Marcus Handte
2019-05-06 17:17:44 +02:00
parent 43c51ecdcc
commit b9d741a17d
@@ -58,6 +58,25 @@ public class CP210XSerialPort extends AbstractSerialPort {
*/
private byte[] modemState = new byte[1];
/**
* A buffer for indirect reading to work around
* the problem that the bulk transfer fails if
* the read buffer passed by the application is
* too small.
*/
private byte[] readBuffer = new byte[4098];
/**
* The offset in the readBuffer.
*/
private int readOffset = 0;
/**
* The length in of data in the readBuffer.
*/
private int readLength = 0;
/**
* Creates a new serial port with the specified config, manager and device.
*
@@ -376,10 +395,26 @@ public class CP210XSerialPort extends AbstractSerialPort {
@Override
public int read(byte[] buffer, int offset, int length) throws IOException {
int read = connection.bulkTransfer(in, buffer, offset, length, 0);
if (read < 0) return read;
interpretModemData(getModemState()[0], getCommStatus()[0]);
return read;
// perform an indirect read using the read buffer to avoid read attempts
// by the application with small length values that will result in read errors
if (readLength <= 0) {
int read = connection.bulkTransfer(in, readBuffer, 0, readBuffer.length, 0);
readLength = read;
readOffset = 0;
if (read < 0) return read;
interpretModemData(getModemState()[0], getCommStatus()[0]);
}
if (readLength > 0) {
int read = Math.min(length, readLength);
for (int i = 0; i < read; i++) {
buffer[offset + i] = readBuffer[readOffset + i];
}
readOffset += read;
readLength -= read;
return read;
} else {
return 0;
}
}
/**