Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92237f59ff | ||
|
|
43cdec42e0 | ||
|
|
1e4467e7a3 | ||
|
|
11543ddb10 | ||
|
|
2e57c8c526 | ||
|
|
4375bdd5fe | ||
|
|
61f24738b4 | ||
|
|
71e1c20512 |
@@ -21,8 +21,8 @@ control. In addition, the library contains the following extensions:
|
||||
|
||||
## Versions
|
||||
|
||||
- Latest development version: **1.0.0-SNAPSHOT**
|
||||
- Latest production version: **none**
|
||||
- Latest development version: **1.0.2-SNAPSHOT**
|
||||
- Latest production version: **1.0.1**
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<name>SmartBotKit Serial</name>
|
||||
<description>SmartBotKit Serial Communication Library</description>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<version.android>4.4.2_r4</version.android>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-android</artifactId>
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.io.IOException;
|
||||
* A serial port implementation for chips following the CDC ACM (communication device class, abstract control model).
|
||||
* The documentation can be found <a href="https://www.usb.org/document-library/class-definitions-communication-devices-12">here</a>.
|
||||
*
|
||||
* This chipset is used by the OpenCR board of the turtlebot 3.
|
||||
* This chipset is used by the OpenCR board of the TurtleBot 3.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
|
||||
@@ -76,7 +76,6 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
*/
|
||||
private int readLength = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device.
|
||||
*
|
||||
@@ -100,9 +99,9 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
initConnection();
|
||||
try {
|
||||
throwIfNegative(sendControlTransfer(IFC_ENABLE, UART_ENABLE, null), "Failed to enable uart (error: %d).");
|
||||
throwIfNegative(initBaudRate(), "Failed to set baud rate (error: %d).");
|
||||
throwIfNegative(initParameters(), "Failed to set parameters (error: %d).");
|
||||
throwIfNegative(initFlowControl(), "Failed to set flow control (error: %d).");
|
||||
throwIfNegative(initBaudRate(), "Failed to set baud rate (error: %d).");
|
||||
} catch (IOException e) {
|
||||
close();
|
||||
throw e;
|
||||
@@ -116,8 +115,10 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
* @throws IOException Thrown if something fails.
|
||||
*/
|
||||
protected int initParameters() throws IOException {
|
||||
int config = getStopBits() | getDataBits() | getParity();
|
||||
return sendControlTransfer(SET_LINE_CTL, config, null);
|
||||
int value = getLineCtl();
|
||||
value &= ~0x05F3;
|
||||
value &= getStopBits() | getDataBits() | getParity();
|
||||
return sendControlTransfer(SET_LINE_CTL, value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,8 +229,6 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
return Math.min(res, sendControlTransfer(SET_FLOW, 0, dataXONXOFF));
|
||||
}
|
||||
throw new IOException("Unsupported flow control configuration.");
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -397,7 +396,7 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
// 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) {
|
||||
if (readLength <= 0) {
|
||||
int read = connection.bulkTransfer(in, readBuffer, 0, readBuffer.length, 0);
|
||||
readLength = read;
|
||||
readOffset = 0;
|
||||
|
||||
@@ -61,12 +61,25 @@ public class FTDISerialPort extends AbstractSerialPort {
|
||||
private static final int INTERFACE_OFFSET_DSR_DTR = 3;
|
||||
private static final int INTERFACE_OFFSET_XON_XOFF = 5;
|
||||
|
||||
|
||||
/**
|
||||
* The buffer to read data with the length initialized
|
||||
* to maximum + 2 bytes for the status.
|
||||
* The size of packets including the 2-byte prefix.
|
||||
*/
|
||||
protected byte[] readBuffer = new byte[4096 + 2];
|
||||
private static final int PACKET_SIZE = 64;
|
||||
|
||||
/**
|
||||
* The payload per packet.
|
||||
*/
|
||||
private static final int PACKET_PAYLOAD = PACKET_SIZE - 2;
|
||||
|
||||
/**
|
||||
* The maximum number of packets.
|
||||
*/
|
||||
private static final int PACKET_MAX = 50;
|
||||
|
||||
/**
|
||||
* The buffer to read data in 64 byte packets.
|
||||
*/
|
||||
protected byte[] readBuffer = new byte[PACKET_SIZE * PACKET_MAX];
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device.
|
||||
@@ -354,12 +367,25 @@ public class FTDISerialPort extends AbstractSerialPort {
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
int maximum = Math.min(length + 2, readBuffer.length);
|
||||
if (length <= 0) return 0; // check that user wants to read something
|
||||
int maximum = Math.min(length, PACKET_MAX * PACKET_PAYLOAD); // at most read buffer size - 2 per packet
|
||||
int packets = maximum / PACKET_PAYLOAD; // number of full packets
|
||||
int remainder = maximum - (packets * PACKET_PAYLOAD); // check for half full packet
|
||||
if (remainder > 0) packets += 1; // there is one
|
||||
maximum += (PACKET_SIZE - PACKET_PAYLOAD) * packets; // add 2 byte header for each packet
|
||||
int read = connection.bulkTransfer(in, readBuffer, maximum, 0);
|
||||
if (read < 2) throw new IOException("Failed to read modem data.");
|
||||
interpretModemData(readBuffer[0], readBuffer[1]);
|
||||
if (read > 2) System.arraycopy(readBuffer, 2, buffer, offset, read - 2);
|
||||
return read - 2;
|
||||
int processed = 0; // number of bytes payload processed
|
||||
int packetStart = 0; // start byte of next packet
|
||||
while (read >= 2) {
|
||||
int dataLength = Math.min(read, PACKET_SIZE) - 2; // determine available payload bytes in packet
|
||||
interpretModemData(readBuffer[packetStart], readBuffer[packetStart + 1]); // process modem data in packet
|
||||
System.arraycopy(readBuffer, packetStart + 2, buffer, offset + processed, dataLength); // copy remainder of packet
|
||||
processed += dataLength; // add bytes to number of processed
|
||||
packetStart += PACKET_SIZE; // move to next packet
|
||||
read -= dataLength + 2; // reduce available bytes by packet
|
||||
}
|
||||
if (read != 0) throw new IOException("Failed to read modem state (" + read + " < 2).");
|
||||
return processed;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.smartbotkit.serial.android;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.smartbotkit.serial.FlowControl;
|
||||
import com.smartbotkit.serial.RetrySerialPort;
|
||||
import com.smartbotkit.serial.SerialConfig;
|
||||
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
/**
|
||||
* This serial port works around the quirks of the cp2102
|
||||
* chipset on the slamtec usb board shipped with the rplidar.
|
||||
* Currently, this class fixes two issues. First,
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class SlamtecSerialPort extends RetrySerialPort {
|
||||
|
||||
/**
|
||||
* A helper class that ensures that the flow control
|
||||
* settings are not transfered as this will cause the
|
||||
* chip to hang.
|
||||
*/
|
||||
private static class SlamtecCP210XSerialPort extends CP210XSerialPort {
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device.
|
||||
*
|
||||
* @param config The port configuration.
|
||||
* @param manager The usb manager.
|
||||
* @param device The device.
|
||||
* @throws IOException Thrown if the port cannot be opened.
|
||||
*/
|
||||
public SlamtecCP210XSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException {
|
||||
super(manager, device, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int initFlowControl() throws IOException {
|
||||
// Do not set the flow control for this chip.
|
||||
// If this is configured, the board will turn on
|
||||
// a red light and stop working. Here we just
|
||||
// check if flow control is turned off as it should
|
||||
// be for this device.
|
||||
if (getConfig().getFlowControl() != FlowControl.NONE)
|
||||
throw new IOException("Unsupported flow control configuration.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device that
|
||||
* uses the default exponential back-off values of the underlying retry port.
|
||||
*
|
||||
* @param config The port configuration.
|
||||
* @param manager The usb manager.
|
||||
* @param device The device.
|
||||
* @throws IOException Thrown if the port cannot be opened.
|
||||
*/
|
||||
public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException {
|
||||
super(new SlamtecCP210XSerialPort(manager, device, config));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device that
|
||||
* uses the specified time as a fixed back-off for retries.
|
||||
*
|
||||
* @param config The port configuration.
|
||||
* @param manager The usb manager.
|
||||
* @param device The device.
|
||||
* @param backoff The back-off time in milliseconds.
|
||||
* @throws IllegalArgumentException Thrown if the back-off is not larger than 0.
|
||||
* @throws IOException Thrown if the port cannot be opened.
|
||||
*/
|
||||
public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config, long backoff) throws IOException {
|
||||
super(new SlamtecCP210XSerialPort(manager, device, config), backoff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new serial port with the specified config, manager and device that uses
|
||||
* an exponential back-off with the specified minimum and maximum delay for retries.
|
||||
*
|
||||
* @param config The port configuration.
|
||||
* @param manager The usb manager.
|
||||
* @param device The device.
|
||||
* @param minBackoff The minimum back-off time in milliseconds.
|
||||
* @param maxBackoff The maximum back-off time in milliseconds.
|
||||
* @throws IllegalArgumentException Thrown if the minimum or maximum back-off is not larger than 0.
|
||||
* @throws IOException Thrown if the port cannot be opened.
|
||||
*/
|
||||
public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config, long minBackoff, long maxBackoff) throws IOException {
|
||||
super(new SlamtecCP210XSerialPort(manager, device, config), minBackoff, maxBackoff);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-core</artifactId>
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A base class for implementing wrappers for ports.
|
||||
* This implementation simply forwards all calls to
|
||||
* the wrapped port. Subclasses may use this to
|
||||
* overwrite individual methods.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class FilterSerialPort implements ISerialPort {
|
||||
|
||||
/**
|
||||
* The port that we are operating on.
|
||||
*/
|
||||
protected ISerialPort port;
|
||||
|
||||
/**
|
||||
* Creates a wrapper for the port.
|
||||
*
|
||||
* @param port The port to be wrapped.
|
||||
*/
|
||||
public FilterSerialPort(ISerialPort port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerialConfig getConfig() {
|
||||
return port.getConfig();
|
||||
}
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
return port.read(buffer, offset, length);
|
||||
}
|
||||
@Override
|
||||
public void setFlowControlListener(IFlowControlListener flowControlListener) {
|
||||
port.setFlowControlListener(flowControlListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLine(OutputControlLine line, boolean value) throws IOException {
|
||||
port.setLine(line, value);
|
||||
}
|
||||
@Override
|
||||
public void setLineErrorListener(ILineErrorListener lineErrorListener) {
|
||||
port.setLineErrorListener(lineErrorListener);
|
||||
}
|
||||
@Override
|
||||
public void write(byte[] buffer, int offset, int length) throws IOException {
|
||||
port.write(buffer, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
port.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A port that retries read operations that are failing with
|
||||
* a negative return value until the port is manually closed.
|
||||
* To avoid high processor loads, the retry logic performs an
|
||||
* exponential back-off starting from configurable a minimum up
|
||||
* to a configurable maximum delay.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class RetrySerialPort extends FilterSerialPort {
|
||||
|
||||
/**
|
||||
* The default minimum back-off time.
|
||||
*/
|
||||
public static final long BACKOFF_MINIMUM_DEFAULT = 15;
|
||||
|
||||
/**
|
||||
* The default maximum back-off time.
|
||||
*/
|
||||
public static final long BACKOFF_MAXIMUM_DEFAULT = 120;
|
||||
|
||||
/**
|
||||
* A flag to indicate whether the port is closed.
|
||||
*/
|
||||
protected boolean closed = false;
|
||||
|
||||
/**
|
||||
* The port that we are operating on.
|
||||
*/
|
||||
protected ISerialPort port;
|
||||
|
||||
/**
|
||||
* The minimum back-off time.
|
||||
*/
|
||||
protected long minBackoff;
|
||||
|
||||
/**
|
||||
* The maximum back-off time.
|
||||
*/
|
||||
protected long maxBackoff;
|
||||
|
||||
/**
|
||||
* Creates a blocking port with an exponential back-off
|
||||
* with default back-off values.
|
||||
*
|
||||
* @param port The port.
|
||||
*/
|
||||
public RetrySerialPort(ISerialPort port) {
|
||||
this(port, BACKOFF_MINIMUM_DEFAULT, BACKOFF_MAXIMUM_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a port with a fixed back-off time in milliseconds.
|
||||
*
|
||||
* @param port The port.
|
||||
* @param backoff The back-off time in milliseconds.
|
||||
* @throws IllegalArgumentException Thrown if the back-off is not larger than 0.
|
||||
*/
|
||||
public RetrySerialPort(ISerialPort port, long backoff) {
|
||||
this(port, backoff, backoff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a port with an exponential back-off with the
|
||||
* specified minimum and maximum values in milliseconds.
|
||||
*
|
||||
* @param port The port.
|
||||
* @param minBackoff The minimum back-off time in milliseconds.
|
||||
* @param maxBackoff The maximum back-off time in milliseconds.
|
||||
* @throws IllegalArgumentException Thrown if the minimum or maximum back-off is not larger than 0.
|
||||
*/
|
||||
public RetrySerialPort(ISerialPort port, long minBackoff, long maxBackoff) {
|
||||
super(port);
|
||||
if (minBackoff <= 0) throw new IllegalArgumentException("Minimum back-off time must be positive (" + minBackoff + " <= 0).");
|
||||
if (maxBackoff <= 0) throw new IllegalArgumentException("Maximum back-off time must be positive (" + maxBackoff + " <= 0).");
|
||||
this.minBackoff = minBackoff;
|
||||
this.maxBackoff = maxBackoff;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
long backoff = Math.min(minBackoff, maxBackoff);
|
||||
while (true) {
|
||||
if (closed) throw new IOException("Port is closed.");
|
||||
int result = super.read(buffer, offset, length);
|
||||
if (result >= 0 || (result < 0 && closed)) return result;
|
||||
sleep(backoff);
|
||||
backoff = Math.min(backoff * 2, maxBackoff);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleeps for for the specified time back-off time by
|
||||
* waiting on the this monitor. The logic ensures
|
||||
* that the desired time is met unless the clock
|
||||
* is not monotonic or the port is closed. In both
|
||||
* cases, the sleep time can be shorter than expected.
|
||||
*
|
||||
* @param backoff The time to wait.
|
||||
*/
|
||||
protected void sleep(long backoff) {
|
||||
long now = System.currentTimeMillis();
|
||||
long end = now + backoff;
|
||||
synchronized (this) {
|
||||
while (now < end && !closed) {
|
||||
try {
|
||||
this.wait(end - now);
|
||||
} catch (InterruptedException e) {
|
||||
// nothing to be done
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
// handle non-monotonic wall-time clock changes
|
||||
if (time < now) break;
|
||||
now = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
// close the port an unblock waiting read operations
|
||||
synchronized (this) {
|
||||
this.closed = true;
|
||||
this.notifyAll();
|
||||
}
|
||||
super.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-rxtx</artifactId>
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-test</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user