This commit is contained in:
@@ -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.
|
||||
*
|
||||
@@ -197,11 +196,7 @@ public class CP210XSerialPort extends AbstractSerialPort {
|
||||
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00
|
||||
};
|
||||
// work-around for RPLidar, don't set the flow control if it is off
|
||||
// TODO: check Linux driver to figure out how this should be done properly
|
||||
// see https://github.com/torvalds/linux/blob/master/drivers/usb/serial/cp210x.c
|
||||
//return sendControlTransfer(SET_FLOW, 0, dataOff);
|
||||
return 0;
|
||||
return sendControlTransfer(SET_FLOW, 0, dataOff);
|
||||
case DSR_DTR:
|
||||
byte[] dataDSRDTR = new byte[]{
|
||||
(byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
@@ -401,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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user