@@ -0,0 +1,36 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-android</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>SmartBotKit Serial-Android</name>
|
||||
<description>SmartBotKit Serial Communication Library Android</description>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<artifactId>serial-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>android</groupId>
|
||||
<artifactId>android</artifactId>
|
||||
<version>${version.android}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.smartbotkit.serial.android;
|
||||
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import com.smartbotkit.serial.IFlowControlListener;
|
||||
import com.smartbotkit.serial.ILineErrorListener;
|
||||
import com.smartbotkit.serial.ISerialPort;
|
||||
import com.smartbotkit.serial.SerialConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A base class for serial ports that handles the configuration
|
||||
* and handlers.
|
||||
*/
|
||||
public abstract class AbstractSerialPort implements ISerialPort {
|
||||
|
||||
/**
|
||||
* The error listener which may be null.
|
||||
*/
|
||||
protected ILineErrorListener lineErrorListener;
|
||||
|
||||
/**
|
||||
* The flow control listener which may be null.
|
||||
*/
|
||||
protected IFlowControlListener flowControlListener;
|
||||
|
||||
/**
|
||||
* The current config.
|
||||
*/
|
||||
protected SerialConfig config;
|
||||
|
||||
/**
|
||||
* The usb manager to open connections.
|
||||
*/
|
||||
protected UsbManager manager;
|
||||
|
||||
/**
|
||||
* The usb manager to open connections.
|
||||
*/
|
||||
protected UsbDevice device;
|
||||
|
||||
/**
|
||||
* The usb interface to open the connection.
|
||||
*/
|
||||
protected UsbInterface inter;
|
||||
|
||||
/**
|
||||
* The input endpoint.
|
||||
*/
|
||||
protected UsbEndpoint in;
|
||||
|
||||
/**
|
||||
* The output endpoint.
|
||||
*/
|
||||
protected UsbEndpoint out;
|
||||
|
||||
/**
|
||||
* The connection.
|
||||
*/
|
||||
protected UsbDeviceConnection connection;
|
||||
|
||||
/**
|
||||
* Creates a new port with the manager, device and config.
|
||||
*
|
||||
* @param config The serial config of the port.
|
||||
* @param device The usb device.
|
||||
* @param manager The usb manager.
|
||||
*/
|
||||
public AbstractSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) {
|
||||
this.config = config;
|
||||
this.manager = manager;
|
||||
this.device = device;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerialConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLineErrorListener(ILineErrorListener lineErrorListener) {
|
||||
this.lineErrorListener = lineErrorListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFlowControlListener(IFlowControlListener flowControlListener) {
|
||||
this.flowControlListener = flowControlListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection to the device. This method requires the interface to be set.
|
||||
* Subclasses should overwrite this to find the interface first.
|
||||
*
|
||||
* @throws IOException Thrown if the interface cannot be found, if the device cannot be opened,
|
||||
* if the interface cannot be claimed or if the input and output endpoint cannot be found.
|
||||
*/
|
||||
protected void initConnection() throws IOException {
|
||||
connection = manager.openDevice(device);
|
||||
if (connection == null) throw new IOException("Failed to open device.");
|
||||
try {
|
||||
if (! connection.claimInterface(inter, true)) throw new IOException("Failed to claim interface.");
|
||||
try {
|
||||
for (int i = 0, s = inter.getEndpointCount(); i < s; i++) {
|
||||
UsbEndpoint end = inter.getEndpoint(i);
|
||||
if (end.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
|
||||
if (end.getDirection() == UsbConstants.USB_DIR_IN) {
|
||||
in = end;
|
||||
} else if (end.getDirection() == UsbConstants.USB_DIR_OUT) {
|
||||
out = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (in == null) throw new IOException("Failed to find input endpoint.");
|
||||
if (out == null) throw new IOException("Failed to find output endpoint.");
|
||||
} catch (IOException e) {
|
||||
connection.releaseInterface(inter);
|
||||
throw e;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
connection.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
connection.releaseInterface(inter);
|
||||
connection.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value of result and throws an exception, if the
|
||||
* result is negative.
|
||||
*
|
||||
* @param result The result to check.
|
||||
* @param message The message to throw, if the result is negative.
|
||||
* @throws IOException Thrown if the result is negative.
|
||||
*/
|
||||
protected void throwIfNegative(int result, String message) throws IOException {
|
||||
if (result < 0) throw new IOException(String.format(message, result));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
package com.smartbotkit.serial.android;
|
||||
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import com.smartbotkit.serial.FlowControl;
|
||||
import com.smartbotkit.serial.InputControlLine;
|
||||
import com.smartbotkit.serial.LineError;
|
||||
import com.smartbotkit.serial.OutputControlLine;
|
||||
import com.smartbotkit.serial.SerialConfig;
|
||||
|
||||
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.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class CDCSerialPort extends AbstractSerialPort {
|
||||
|
||||
private static final int REQTYPE_HOST2DEVICE = 0x21;
|
||||
private static final int REQTYPE_DEVICE2HOST = 0xA1;
|
||||
|
||||
private static final int SET_LINE_CODING = 0x20;
|
||||
|
||||
private static final int SERIAL_STATE = 0x20;
|
||||
private static final int SET_CONTROL_LINE_STATE = 0x22;
|
||||
|
||||
private static final int CONTROL_LINE_RTS = 0x2;
|
||||
private static final int CONTROL_LINE_DTR = 0x1;
|
||||
|
||||
private static final byte LINE_CODING_DATA_BITS_5 = 0x05;
|
||||
private static final byte LINE_CODING_DATA_BITS_6 = 0x06;
|
||||
private static final byte LINE_CODING_DATA_BITS_7 = 0x07;
|
||||
private static final byte LINE_CODING_DATA_BITS_8 = 0x08;
|
||||
|
||||
private static final byte LINE_CODING_STOP_BITS_1 = 0x00;
|
||||
private static final byte LINE_CODING_STOP_BITS_1_5 = 0x01;
|
||||
private static final byte LINE_CODING_STOP_BITS_2 = 0x02;
|
||||
|
||||
private static final byte LINE_CODING_PARITY_NONE = 0x00;
|
||||
private static final byte LINE_CODING_PARITY_ODD = 0x01;
|
||||
private static final byte LINE_CODING_PARITY_EVEN = 0x02;
|
||||
private static final byte LINE_CODING_PARITY_MARK = 0x03;
|
||||
private static final byte LINE_CODING_PARITY_SPACE = 0x04;
|
||||
|
||||
|
||||
/**
|
||||
* The buffer used to read the serial state.
|
||||
*/
|
||||
private byte[] statusBuffer = new byte[2];
|
||||
|
||||
/**
|
||||
* The current control line state.
|
||||
*/
|
||||
private int controlLine = 0;
|
||||
|
||||
/**
|
||||
* 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 CDCSerialPort(SerialConfig config, UsbManager manager, UsbDevice device) throws IOException {
|
||||
super(manager, device, config);
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the port or throws an io exception if the
|
||||
* initialization fails.
|
||||
*
|
||||
* @throws IOException Thrown if the initialization fails.
|
||||
*/
|
||||
protected void init() throws IOException {
|
||||
initConnection();
|
||||
try {
|
||||
initParameters();
|
||||
setControlLine(controlLine);
|
||||
} catch (IOException e) {
|
||||
close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection to the device.
|
||||
*
|
||||
* @throws IOException Thrown if the interface cannot be found, if the device cannot be opened,
|
||||
* if the interface cannot be claimed or if the input and output endpoint cannot be found.
|
||||
*/
|
||||
protected void initConnection() throws IOException {
|
||||
for (int i = 0, s = device.getInterfaceCount(); i < s; i++) {
|
||||
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
||||
inter = device.getInterface(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (inter == null) throw new IOException("Failed to find interface.");
|
||||
super.initConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the connection parameters set
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
protected void initParameters() throws IOException {
|
||||
byte[] lineCoding = new byte[7];
|
||||
setBaudRate(lineCoding);
|
||||
setDataBits(lineCoding);
|
||||
setStopBits(lineCoding);
|
||||
setParity(lineCoding);
|
||||
if (config.getFlowControl() != FlowControl.NONE) {
|
||||
throw new IOException("Unsupported flow control configuration.");
|
||||
}
|
||||
sendControlTransfer(SET_LINE_CODING, 0, lineCoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the baud rate in the line encoding array.
|
||||
*
|
||||
* @param lineCoding The line encoding array.
|
||||
* @throws IOException Thrown if the baud rate is not supported.
|
||||
*/
|
||||
protected void setBaudRate(byte[] lineCoding) throws IOException {
|
||||
int value = 0;
|
||||
switch (config.getBaudRate()) {
|
||||
case RATE_300:
|
||||
value = 300;
|
||||
break;
|
||||
case RATE_600:
|
||||
value = 600;
|
||||
break;
|
||||
case RATE_1200:
|
||||
value = 1200;
|
||||
break;
|
||||
case RATE_2400:
|
||||
value = 2400;
|
||||
break;
|
||||
case RATE_4800:
|
||||
value = 4800;
|
||||
break;
|
||||
case RATE_9600:
|
||||
value = 9600;
|
||||
break;
|
||||
case RATE_19200:
|
||||
value = 19200;
|
||||
break;
|
||||
case RATE_38400:
|
||||
value = 38400;
|
||||
break;
|
||||
case RATE_57600:
|
||||
value = 57600;
|
||||
break;
|
||||
case RATE_115200:
|
||||
value = 115200;
|
||||
break;
|
||||
case RATE_230400:
|
||||
value = 230400;
|
||||
break;
|
||||
case RATE_460800:
|
||||
value = 460800;
|
||||
break;
|
||||
case RATE_921600:
|
||||
value = 921600;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported baud rate configuration.");
|
||||
}
|
||||
lineCoding[0] = (byte) (value & 0xff);
|
||||
lineCoding[1] = (byte) (value >> 8 & 0xff);
|
||||
lineCoding[2] = (byte) (value >> 16 & 0xff);
|
||||
lineCoding[3] = (byte) (value >> 24 & 0xff);
|
||||
}
|
||||
|
||||
protected void setDataBits(byte[] lineCoding) throws IOException {
|
||||
switch (config.getDataBits()) {
|
||||
case BITS_5:
|
||||
lineCoding[6] = LINE_CODING_DATA_BITS_5;
|
||||
break;
|
||||
case BITS_6:
|
||||
lineCoding[6] = LINE_CODING_DATA_BITS_6;
|
||||
break;
|
||||
case BITS_7:
|
||||
lineCoding[6] = LINE_CODING_DATA_BITS_7;
|
||||
break;
|
||||
case BITS_8:
|
||||
lineCoding[6] = LINE_CODING_DATA_BITS_8;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported data bits configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the parity configuration in the line coding.
|
||||
*
|
||||
* @param lineCoding The line coding to update.
|
||||
* @throws IOException Thrown if the parity configuration is unsupported.
|
||||
*/
|
||||
protected void setParity(byte[] lineCoding) throws IOException {
|
||||
switch (config.getParity()) {
|
||||
case NONE:
|
||||
lineCoding[5] = LINE_CODING_PARITY_NONE;
|
||||
break;
|
||||
case ODD:
|
||||
lineCoding[5] = LINE_CODING_PARITY_ODD;
|
||||
break;
|
||||
case EVEN:
|
||||
lineCoding[5] = LINE_CODING_PARITY_EVEN;
|
||||
break;
|
||||
case MARK:
|
||||
lineCoding[5] = LINE_CODING_PARITY_MARK;
|
||||
break;
|
||||
case SPACE:
|
||||
lineCoding[5] = LINE_CODING_PARITY_SPACE;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported parity configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the stop bits configuration in the line coding.
|
||||
*
|
||||
* @param lineCoding The line coding to update.
|
||||
* @throws IOException Thrown if the stop bits configuration is unsupported.
|
||||
*/
|
||||
protected void setStopBits(byte[] lineCoding) throws IOException {
|
||||
switch (config.getStopBits()) {
|
||||
case BITS_1:
|
||||
lineCoding[4] = LINE_CODING_STOP_BITS_1;
|
||||
break;
|
||||
case BITS_1_5:
|
||||
lineCoding[4] = LINE_CODING_STOP_BITS_1_5;
|
||||
break;
|
||||
case BITS_2:
|
||||
lineCoding[4] = LINE_CODING_STOP_BITS_2;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported stop bits configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the control line state which defines whether rts and dtr are
|
||||
* high or low.
|
||||
*
|
||||
* @param state The state.
|
||||
* @throws IOException Thrown if the state cannot be set.
|
||||
*/
|
||||
protected void setControlLine(int state) throws IOException {
|
||||
throwIfNegative(sendControlTransfer(SET_CONTROL_LINE_STATE, state, null), "Failed to set control line state (error: %d).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a control transfer from host to device with the specified request
|
||||
* type and value and data with an unlimited timeout using the default interface.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @param data The data to send or null, if none.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number otherwise.
|
||||
*/
|
||||
protected int sendControlTransfer(int type, int value, byte[] data) {
|
||||
int length = 0;
|
||||
if(data != null) length = data.length;
|
||||
return connection.controlTransfer(REQTYPE_HOST2DEVICE, type, value, 0, data, length, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a control transfer from device to host with the specified request
|
||||
* type and value into the specified data array with an unlimited timeout using
|
||||
* the default interface.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @param data The data buffer to receive.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number with the written bytes.
|
||||
*/
|
||||
protected int receiveControlTransfer(int type, int value, byte[] data) {
|
||||
int length = 0;
|
||||
if(data != null) length = data.length;
|
||||
return connection.controlTransfer(REQTYPE_DEVICE2HOST, type, value, 0, data, length, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setLine(OutputControlLine line, boolean value) throws IOException {
|
||||
int mask = 0;
|
||||
switch (line) {
|
||||
case RTS:
|
||||
mask = CONTROL_LINE_RTS;
|
||||
break;
|
||||
case DTR:
|
||||
mask = CONTROL_LINE_DTR;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported control line.");
|
||||
}
|
||||
if (value) {
|
||||
controlLine |= mask;
|
||||
} else {
|
||||
controlLine &= ~mask;
|
||||
}
|
||||
setControlLine(controlLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
int read = connection.bulkTransfer(in, buffer, length, 0);
|
||||
if (read < 0) return read;
|
||||
throwIfNegative(receiveControlTransfer(SERIAL_STATE, 0, statusBuffer), "Failed to read modem status (error: %s).");
|
||||
interpretModemData(statusBuffer[0], statusBuffer[1]);
|
||||
return read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interprets the modem data received from the serial state control transfer.
|
||||
*
|
||||
* @param data1 The first byte.
|
||||
* @param data2 The second byte.
|
||||
*/
|
||||
protected void interpretModemData(byte data1, byte data2) {
|
||||
// check listener multiple times to handle reentrant code
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.CTS, (data1 & 0x01) == 0x02);
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.DSR, (data1 & 0x02) == 0x02);
|
||||
if (lineErrorListener != null && (data1 & 0x04) == 0x04) lineErrorListener.onLineError(LineError.BREAK);
|
||||
if (lineErrorListener != null && (data1 & 0x10) == 0x10) lineErrorListener.onLineError(LineError.FRAMING);
|
||||
if (lineErrorListener != null && (data1 & 0x20) == 0x20) lineErrorListener.onLineError(LineError.PARITY);
|
||||
if (lineErrorListener != null && (data1 & 0x40) == 0x40) lineErrorListener.onLineError(LineError.OVERRUN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buffer, int offset, int length) throws IOException {
|
||||
while (length > 0) {
|
||||
int res = connection.bulkTransfer(out, buffer, offset, buffer.length, 0);
|
||||
if (res < 0) throw new IOException(String.format("Failed to write bytes (error: %d)", res));
|
||||
offset += res;
|
||||
length -= res;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
sendControlTransfer(SET_CONTROL_LINE_STATE, 0, null);
|
||||
super.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,418 @@
|
||||
package com.smartbotkit.serial.android;
|
||||
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import com.smartbotkit.serial.InputControlLine;
|
||||
import com.smartbotkit.serial.LineError;
|
||||
import com.smartbotkit.serial.OutputControlLine;
|
||||
import com.smartbotkit.serial.SerialConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An implementation of the serial port driver for the CP210x chip set family.
|
||||
* The implementation is a literal translation from https://github.com/felHR85.
|
||||
*
|
||||
* This chip set is used by the LDS shipped with the TurtleBot 3.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class CP210XSerialPort extends AbstractSerialPort {
|
||||
|
||||
|
||||
private static final int PURGE = 0x12;
|
||||
private static final int IFC_ENABLE = 0x00;
|
||||
private static final int SET_LINE_CTL = 0x03;
|
||||
private static final int GET_LINE_CTL = 0x04;
|
||||
private static final int SET_MHS = 0x07;
|
||||
private static final int SET_BAUDRATE = 0x1E;
|
||||
private static final int SET_FLOW = 0x13;
|
||||
private static final int SET_CHARS = 0x19;
|
||||
private static final int GET_MDMSTS = 0x08;
|
||||
private static final int GET_COMM_STATUS = 0x10;
|
||||
|
||||
private static final int REQTYPE_HOST2DEVICE = 0x41;
|
||||
private static final int REQTYPE_DEVICE2HOST = 0xC1;
|
||||
|
||||
private static final int MHS_RTS_ON = 0x202;
|
||||
private static final int MHS_RTS_OFF = 0x200;
|
||||
private static final int MHS_DTR_ON = 0x101;
|
||||
private static final int MHS_DTR_OFF = 0x100;
|
||||
|
||||
private static final int PURGE_ALL = 0x000f;
|
||||
|
||||
private static final int UART_ENABLE = 0x0001;
|
||||
private static final int UART_DISABLE = 0x0000;
|
||||
|
||||
/**
|
||||
* A buffer to fetch the comm state.
|
||||
*/
|
||||
private byte[] commState = new byte[19];
|
||||
/**
|
||||
* A buffer to fetch the line state.
|
||||
*/
|
||||
private byte[] lineState = new byte[2];
|
||||
/**
|
||||
* A buffer to fetch the modem state.
|
||||
*/
|
||||
private byte[] modemState = new byte[1];
|
||||
|
||||
/**
|
||||
* 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 CP210XSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException {
|
||||
super(manager, device, config);
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Opens and initializes the connection based on the settings.
|
||||
*
|
||||
* @throws IOException Thrown if the connection cannot be opened.
|
||||
*/
|
||||
protected void init() throws IOException {
|
||||
initConnection();
|
||||
try {
|
||||
throwIfNegative(sendControlTransfer(IFC_ENABLE, UART_ENABLE, null), "Failed to enable uart (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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the connection parameters.
|
||||
*
|
||||
* @return The number of bytes written or negative, if failed.
|
||||
* @throws IOException Thrown if something fails.
|
||||
*/
|
||||
protected int initParameters() throws IOException {
|
||||
int config = getStopBits() | getDataBits() | getParity();
|
||||
return sendControlTransfer(SET_LINE_CTL, config, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bit mask to configure the stop bits.
|
||||
*
|
||||
* @return The bit mask for the config.
|
||||
* @throws IOException Thrown if the config is not supported.
|
||||
*/
|
||||
protected int getStopBits() throws IOException {
|
||||
switch (config.getStopBits()) {
|
||||
case BITS_1:
|
||||
return 0;
|
||||
case BITS_1_5:
|
||||
return 1;
|
||||
case BITS_2:
|
||||
return 2;
|
||||
}
|
||||
throw new IOException("Unsupported stop bits configuration.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bit mask to configure the parity.
|
||||
*
|
||||
* @return The bit mask for the config.
|
||||
* @throws IOException Thrown if the config is not supported.
|
||||
*/
|
||||
protected int getParity() throws IOException {
|
||||
switch (config.getParity()) {
|
||||
case NONE:
|
||||
return 0x0000;
|
||||
case ODD:
|
||||
return 0x0010;
|
||||
case EVEN:
|
||||
return 0x0020;
|
||||
case MARK:
|
||||
return 0x0030;
|
||||
case SPACE:
|
||||
return 0x0040;
|
||||
}
|
||||
throw new IOException("Unsupported parity configuration.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bit mask to configure the data bits.
|
||||
*
|
||||
* @return The bit mask for the config.
|
||||
* @throws IOException Thrown if the config is not supported.
|
||||
*/
|
||||
protected int getDataBits() throws IOException {
|
||||
switch (config.getDataBits()) {
|
||||
case BITS_5:
|
||||
return 0x0500;
|
||||
case BITS_6:
|
||||
return 0x0600;
|
||||
case BITS_7:
|
||||
return 0x0700;
|
||||
case BITS_8:
|
||||
return 0x0800;
|
||||
}
|
||||
throw new IOException("Unsupported data bits configuration.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the flow control based on the config.
|
||||
*
|
||||
* @return A negative number, if failed.
|
||||
* @throws IOException Thrown if the flow control config is unsupported.
|
||||
*/
|
||||
protected int initFlowControl() throws IOException {
|
||||
switch (config.getFlowControl()) {
|
||||
case NONE:
|
||||
byte[] dataOff = new byte[]{
|
||||
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00
|
||||
};
|
||||
return sendControlTransfer(SET_FLOW, 0, dataOff);
|
||||
case DSR_DTR:
|
||||
byte[] dataDSRDTR = new byte[]{
|
||||
(byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00
|
||||
};
|
||||
return sendControlTransfer(SET_FLOW, 0, dataDSRDTR);
|
||||
case RTS_CTS:
|
||||
byte[] dataRTSCTS = new byte[]{
|
||||
(byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00
|
||||
};
|
||||
return sendControlTransfer(SET_FLOW, 0, dataRTSCTS);
|
||||
case XON_XOFF:
|
||||
byte[] dataXONXOFF = new byte[]{
|
||||
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x43, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00
|
||||
};
|
||||
|
||||
byte[] dataChars = new byte[] {
|
||||
(byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x11, (byte) 0x13
|
||||
};
|
||||
int res = sendControlTransfer(SET_CHARS, 0, dataChars);
|
||||
return Math.min(res, sendControlTransfer(SET_FLOW, 0, dataXONXOFF));
|
||||
}
|
||||
throw new IOException("Unsupported flow control configuration.");
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the baud rage based on the configuration.
|
||||
*
|
||||
* @throws IOException Thrown if the baud rage cannot be set.
|
||||
*/
|
||||
protected int initBaudRate() throws IOException {
|
||||
int value = 0;
|
||||
switch (config.getBaudRate()) {
|
||||
case RATE_300:
|
||||
value = 300;
|
||||
break;
|
||||
case RATE_600:
|
||||
value = 600;
|
||||
break;
|
||||
case RATE_1200:
|
||||
value = 1200;
|
||||
break;
|
||||
case RATE_2400:
|
||||
value = 2400;
|
||||
break;
|
||||
case RATE_4800:
|
||||
value = 4800;
|
||||
break;
|
||||
case RATE_9600:
|
||||
value = 9600;
|
||||
break;
|
||||
case RATE_19200:
|
||||
value = 19200;
|
||||
break;
|
||||
case RATE_38400:
|
||||
value = 38400;
|
||||
break;
|
||||
case RATE_57600:
|
||||
value = 57600;
|
||||
break;
|
||||
case RATE_115200:
|
||||
value = 115200;
|
||||
break;
|
||||
case RATE_230400:
|
||||
value = 230400;
|
||||
break;
|
||||
case RATE_460800:
|
||||
value = 460800;
|
||||
break;
|
||||
case RATE_921600:
|
||||
value = 921600;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported baud rate configuration.");
|
||||
}
|
||||
byte[] data = new byte[] {
|
||||
(byte) (value & 0xff),
|
||||
(byte) (value >> 8 & 0xff),
|
||||
(byte) (value >> 16 & 0xff),
|
||||
(byte) (value >> 24 & 0xff)
|
||||
};
|
||||
return sendControlTransfer(SET_BAUDRATE, 0, data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Opens the connection to the device.
|
||||
*
|
||||
* @throws IOException Thrown if the interface cannot be found, if the device cannot be opened,
|
||||
* if the interface cannot be claimed or if the input and output endpoint cannot be found.
|
||||
*/
|
||||
protected void initConnection() throws IOException {
|
||||
if (device.getInterfaceCount() == 0) throw new IOException("Failed to find interface.");
|
||||
inter = device.getInterface(0);
|
||||
super.initConnection();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs a control transfer from host to device with the specified request
|
||||
* type and value and data with an unlimited timeout using the default interface.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @param data The data to send or null, if none.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number otherwise.
|
||||
*/
|
||||
protected int sendControlTransfer(int type, int value, byte[] data) {
|
||||
int length = 0;
|
||||
if(data != null) length = data.length;
|
||||
return connection.controlTransfer(REQTYPE_HOST2DEVICE, type, value, inter.getId(), data, length, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a control transfer from device to host with the specified request
|
||||
* type and value into the specified data array with an unlimited timeout using
|
||||
* the default interface.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @param data The data buffer to receive.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number with the written bytes.
|
||||
*/
|
||||
protected int receiveControlTransfer(int type, int value, byte[] data) {
|
||||
int length = 0;
|
||||
if (data != null) length = data.length;
|
||||
return connection.controlTransfer(REQTYPE_DEVICE2HOST, type, value, inter.getId(), data, length, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a byte array representing the modem status.
|
||||
*
|
||||
* @return A byte array representing the modem status.
|
||||
* @throws IOException Thrown if the status cannot be retrieved.
|
||||
*/
|
||||
protected byte[] getModemState() throws IOException {
|
||||
throwIfNegative(receiveControlTransfer(GET_MDMSTS, 0, modemState), "Failed to read modem state (error: %d).");
|
||||
return modemState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a byte array representing the comm status.
|
||||
*
|
||||
* @return A byte array representing the comm status.
|
||||
* @throws IOException Thrown if the status cannot be retrieved.
|
||||
*/
|
||||
protected byte[] getCommStatus() throws IOException {
|
||||
throwIfNegative(receiveControlTransfer(GET_COMM_STATUS, 0, commState), "Failed to read comm state (error: %d).");
|
||||
return commState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a short representing the current line configuration from the
|
||||
* chip. This is the inverse of the setting done during init parameters.
|
||||
*
|
||||
* @return A short representing the current line configuration.
|
||||
* @throws IOException Thrown if the status cannot be retrieved.
|
||||
*/
|
||||
protected short getLineCtl() throws IOException {
|
||||
throwIfNegative(receiveControlTransfer(GET_LINE_CTL, 0, lineState), "Failed to read line state (error: %d).");
|
||||
return (short)((lineState[1] << 8) | (lineState[0] & 0xFF));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLine(OutputControlLine line, boolean value) throws IOException {
|
||||
int flag = 0;
|
||||
switch (line) {
|
||||
case DTR:
|
||||
if (value) flag = MHS_DTR_ON;
|
||||
else flag = MHS_DTR_OFF;
|
||||
break;
|
||||
case RTS:
|
||||
if (value) flag = MHS_RTS_ON;
|
||||
else flag = MHS_RTS_OFF;
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Invalid line configuration.");
|
||||
}
|
||||
throwIfNegative(sendControlTransfer(SET_MHS, flag, null), "Failed to set line (error: %d).");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
int read = connection.bulkTransfer(in, buffer, length, 0);
|
||||
if (read < 0) return read;
|
||||
interpretModemData(getModemState()[0], getCommStatus()[0]);
|
||||
return read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interprets the modem data received from the serial state control transfer.
|
||||
*
|
||||
* @param data1 The first byte.
|
||||
* @param data2 The second byte.
|
||||
*/
|
||||
protected void interpretModemData(byte data1, byte data2) {
|
||||
// check listener multiple times to handle reentrant code
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.CTS, (data1 & 0x10) == 0x10);
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.DSR, (data1 & 0x20) == 0x20);
|
||||
if (lineErrorListener != null && (data1 & 0x01) == 0x01) lineErrorListener.onLineError(LineError.BREAK);
|
||||
if (lineErrorListener != null && (data1 & 0x02) == 0x02) lineErrorListener.onLineError(LineError.FRAMING);
|
||||
if (lineErrorListener != null && (data1 & 0x10) == 0x10) lineErrorListener.onLineError(LineError.PARITY);
|
||||
if (lineErrorListener != null && ((data1 & 0x04) == 0x04 || (data1 & 0x08) == 0x08)) lineErrorListener.onLineError(LineError.OVERRUN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buffer, int offset, int length) throws IOException {
|
||||
while (length > 0) {
|
||||
int res = connection.bulkTransfer(out, buffer, offset, buffer.length, 0);
|
||||
if (res < 0) throw new IOException(String.format("Failed to write bytes (error: %d)", res));
|
||||
offset += res;
|
||||
length -= res;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
sendControlTransfer(PURGE, PURGE_ALL, null);
|
||||
sendControlTransfer(IFC_ENABLE, UART_DISABLE, null);
|
||||
super.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
package com.smartbotkit.serial.android;
|
||||
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import com.smartbotkit.serial.InputControlLine;
|
||||
import com.smartbotkit.serial.LineError;
|
||||
import com.smartbotkit.serial.OutputControlLine;
|
||||
import com.smartbotkit.serial.SerialConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Implements a serial port for an FTDI chip. This implementation is
|
||||
* a literal translation the one found at https://github.com/felHR85.
|
||||
*
|
||||
* This chip set is used by the Kobuki base of the TurtleBot 2.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class FTDISerialPort extends AbstractSerialPort {
|
||||
|
||||
private static final int REQTYPE_HOST2DEVICE = 0x40;
|
||||
|
||||
private static final int SIO_RESET = 0;
|
||||
private static final int SIO_MODEM_CTRL = 1;
|
||||
private static final int SIO_SET_FLOW_CTRL = 2;
|
||||
private static final int SIO_SET_BAUD_RATE = 3;
|
||||
private static final int SIO_SET_DATA = 4;
|
||||
|
||||
private static final int SIO_SET_DTR_MASK = 0x1;
|
||||
private static final int SIO_SET_DTR_HIGH = (1 | (SIO_SET_DTR_MASK << 8));
|
||||
private static final int SIO_SET_DTR_LOW = (0 | (SIO_SET_DTR_MASK << 8));
|
||||
private static final int SIO_SET_RTS_MASK = 0x2;
|
||||
private static final int SIO_SET_RTS_HIGH = (2 | (SIO_SET_RTS_MASK << 8));
|
||||
private static final int SIO_SET_RTS_LOW = (0 | (SIO_SET_RTS_MASK << 8));
|
||||
|
||||
private static final int BAUDRATE_300 = 0x2710;
|
||||
private static final int BAUDRATE_600 = 0x1388;
|
||||
private static final int BAUDRATE_1200 = 0x09c4;
|
||||
private static final int BAUDRATE_2400 = 0x04e2;
|
||||
private static final int BAUDRATE_4800 = 0x0271;
|
||||
private static final int BAUDRATE_9600 = 0x4138;
|
||||
private static final int BAUDRATE_19200 = 0x809c;
|
||||
private static final int BAUDRATE_38400 = 0xc04e;
|
||||
private static final int BAUDRATE_57600 = 0x0034;
|
||||
private static final int BAUDRATE_115200 = 0x001a;
|
||||
private static final int BAUDRATE_230400 = 0x000d;
|
||||
private static final int BAUDRATE_460800 = 0x4006;
|
||||
private static final int BAUDRATE_921600 = 0x8003;
|
||||
|
||||
private static final int SET_MODEM_CTRL_DEFAULT1 = 0x0101;
|
||||
private static final int SET_MODEM_CTRL_DEFAULT2 = 0x0202;
|
||||
private static final int SET_MODEM_CTRL_DEFAULT3 = 0x0100;
|
||||
private static final int SET_MODEM_CTRL_DEFAULT4 = 0x0200;
|
||||
private static final int SET_FLOW_CTRL_DEFAULT = 0x0000;
|
||||
private static final int SET_FLOW_CTRL_SOFTWARE = 0x1311;
|
||||
|
||||
private static final int INTERFACE_OFFSET_DEFAULT = 1;
|
||||
private static final int INTERFACE_OFFSET_RTS_CTS = 2;
|
||||
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.
|
||||
*/
|
||||
protected byte[] readBuffer = new byte[4096 + 2];
|
||||
|
||||
/**
|
||||
* 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 FTDISerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException {
|
||||
super(manager, device, config);
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens and initializes the connection based on the settings.
|
||||
*
|
||||
* @throws IOException Thrown if the connection cannot be opened.
|
||||
*/
|
||||
protected void init() throws IOException {
|
||||
initConnection();
|
||||
try {
|
||||
throwIfNegative(sendReset(), "Failed to reset device (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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection to the device.
|
||||
*
|
||||
* @throws IOException Thrown if the interface cannot be found, if the device cannot be opened,
|
||||
* if the interface cannot be claimed or if the input and output endpoint cannot be found.
|
||||
*/
|
||||
protected void initConnection() throws IOException {
|
||||
if (device.getInterfaceCount() == 0) throw new IOException("Failed to find interface.");
|
||||
inter = device.getInterface(0);
|
||||
super.initConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a reset.
|
||||
*
|
||||
* @return The value of the reset.
|
||||
*/
|
||||
protected int sendReset() {
|
||||
int result = sendControlTransfer(SIO_RESET, 0);
|
||||
result = Math.min(result, sendControlTransfer(SIO_MODEM_CTRL, SET_MODEM_CTRL_DEFAULT1));
|
||||
result = Math.min(result, sendControlTransfer(SIO_MODEM_CTRL, SET_MODEM_CTRL_DEFAULT2));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the baud rate and returns the result of the control command.
|
||||
*
|
||||
* @return A negative value if failed, zero or positive, if success.
|
||||
* @throws IOException Thrown if the baud rate is not supported.
|
||||
*/
|
||||
protected int initBaudRate() throws IOException {
|
||||
switch (config.getBaudRate()) {
|
||||
case RATE_300:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_300);
|
||||
case RATE_600:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_600);
|
||||
case RATE_1200:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_1200);
|
||||
case RATE_2400:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_2400);
|
||||
case RATE_4800:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_4800);
|
||||
case RATE_9600:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_9600);
|
||||
case RATE_19200:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_19200);
|
||||
case RATE_38400:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_38400);
|
||||
case RATE_57600:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_57600);
|
||||
case RATE_115200:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_115200);
|
||||
case RATE_230400:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_230400);
|
||||
case RATE_460800:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_460800);
|
||||
case RATE_921600:
|
||||
return sendControlTransfer(SIO_SET_BAUD_RATE, BAUDRATE_921600);
|
||||
default:
|
||||
throw new IOException("Unsupported baud rate configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the connection parameters (parity, data bits and stop bits).
|
||||
*
|
||||
* @return A negative value if failed, zero or positive, if success.
|
||||
* @throws IOException Thrown if the parity, data bits or stop bits
|
||||
* are not supported.
|
||||
*/
|
||||
protected int initParameters() throws IOException {
|
||||
int value = setDatabits(0);
|
||||
value = setParity(value);
|
||||
value = setStopBits(value);
|
||||
return sendControlTransfer(SIO_SET_DATA, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bits in the value according to the data bits
|
||||
* configured in the serial config.
|
||||
*
|
||||
* @param value The base value.
|
||||
* @return The adjusted value.
|
||||
* @throws IOException Thrown if the configuration is not supported.
|
||||
*/
|
||||
protected int setDatabits(int value) throws IOException {
|
||||
switch (config.getDataBits()) {
|
||||
case BITS_5:
|
||||
value |= 1;
|
||||
value &= ~(1 << 1);
|
||||
value |= (1 << 2);
|
||||
value &= ~(1 << 3);
|
||||
break;
|
||||
case BITS_6:
|
||||
value &= ~1;
|
||||
value |= (1 << 1);
|
||||
value |= (1 << 2);
|
||||
value &= ~(1 << 3);
|
||||
break;
|
||||
case BITS_7:
|
||||
value |= 1;
|
||||
value |= (1 << 1);
|
||||
value |= (1 << 2);
|
||||
value &= ~(1 << 3);
|
||||
break;
|
||||
case BITS_8:
|
||||
value &= ~1;
|
||||
value &= ~(1 << 1);
|
||||
value &= ~(1 << 2);
|
||||
value |= (1 << 3);
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported data bits configuration.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bits in the value according to the stop bits
|
||||
* configured in the serial config.
|
||||
*
|
||||
* @param value The base value.
|
||||
* @return The adjusted value.
|
||||
* @throws IOException Thrown if the configuration is not supported.
|
||||
*/
|
||||
protected int setStopBits(int value) throws IOException {
|
||||
switch (config.getStopBits()) {
|
||||
case BITS_1:
|
||||
value &= ~(1 << 11);
|
||||
value &= ~(1 << 12);
|
||||
value &= ~(1 << 13);
|
||||
break;
|
||||
case BITS_1_5:
|
||||
value |= (1 << 11);
|
||||
value &= ~(1 << 12);
|
||||
value &= ~(1 << 13);
|
||||
break;
|
||||
case BITS_2:
|
||||
value &= ~(1 << 11);
|
||||
value |= (1 << 12);
|
||||
value &= ~(1 << 13);
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported stop bits configuration.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bits in the value according to the parity
|
||||
* configured in the serial config.
|
||||
*
|
||||
* @param value The base value.
|
||||
* @return The adjusted value.
|
||||
* @throws IOException Thrown if the configuration is not supported.
|
||||
*/
|
||||
protected int setParity(int value) throws IOException {
|
||||
switch (config.getParity()) {
|
||||
case NONE:
|
||||
value &= ~(1 << 8);
|
||||
value &= ~(1 << 9);
|
||||
value &= ~(1 << 10);
|
||||
break;
|
||||
case ODD:
|
||||
value |= (1 << 8);
|
||||
value &= ~(1 << 9);
|
||||
value &= ~(1 << 10);
|
||||
break;
|
||||
case EVEN:
|
||||
value &= ~(1 << 8);
|
||||
value |= (1 << 9);
|
||||
value &= ~(1 << 10);
|
||||
break;
|
||||
case MARK:
|
||||
value |= (1 << 8);
|
||||
value |= (1 << 9);
|
||||
value &= ~(1 << 10);
|
||||
break;
|
||||
case SPACE:
|
||||
value &= ~(1 << 8);
|
||||
value &= ~(1 << 9);
|
||||
value |= (1 << 10);
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported parity configuration.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a control transfer with the specified request type and value
|
||||
* and zero data with an unlimited timeout using the default interface.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number otherwise.
|
||||
*/
|
||||
protected int sendControlTransfer(int type, int value) {
|
||||
return sendControlTransfer(type, value, INTERFACE_OFFSET_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a control transfer with the specified request type and value
|
||||
* and zero data with an unlimited timeout using the specified interface
|
||||
* offset.
|
||||
*
|
||||
* @param type The request type.
|
||||
* @param value The request value.
|
||||
* @param interfaceOffset The interface offset.
|
||||
* @return A negative number, if the control transfer failed, zero or a
|
||||
* positive number otherwise.
|
||||
*/
|
||||
protected int sendControlTransfer(int type, int value, int interfaceOffset) {
|
||||
return connection.controlTransfer(REQTYPE_HOST2DEVICE, type, value, inter.getId() + interfaceOffset, null, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the flow control value according to the
|
||||
* configuration of the serial config.
|
||||
*
|
||||
* @return The result of the control transfer.
|
||||
* @throws IOException Thrown if the configuration
|
||||
*/
|
||||
protected int initFlowControl() throws IOException {
|
||||
switch (config.getFlowControl()) {
|
||||
case NONE:
|
||||
return sendControlTransfer(SIO_SET_FLOW_CTRL, SET_FLOW_CTRL_DEFAULT, INTERFACE_OFFSET_DEFAULT);
|
||||
case DSR_DTR:
|
||||
return sendControlTransfer(SIO_SET_FLOW_CTRL, SET_FLOW_CTRL_DEFAULT, INTERFACE_OFFSET_DSR_DTR);
|
||||
case RTS_CTS:
|
||||
return sendControlTransfer(SIO_SET_FLOW_CTRL, SET_FLOW_CTRL_DEFAULT, INTERFACE_OFFSET_RTS_CTS);
|
||||
case XON_XOFF:
|
||||
return sendControlTransfer(SIO_SET_FLOW_CTRL, SET_FLOW_CTRL_SOFTWARE, INTERFACE_OFFSET_XON_XOFF);
|
||||
default:
|
||||
throw new IOException("Unsupported flow control configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLine(OutputControlLine line, boolean value) throws IOException {
|
||||
switch (line) {
|
||||
case RTS:
|
||||
throwIfNegative(sendControlTransfer(SIO_MODEM_CTRL, value?SIO_SET_RTS_HIGH:SIO_SET_RTS_LOW), "Failed to set rts value (error: %d).");
|
||||
break;
|
||||
case DTR:
|
||||
throwIfNegative(sendControlTransfer(SIO_MODEM_CTRL, value?SIO_SET_DTR_HIGH:SIO_SET_DTR_LOW), "Failed to set dtr value (error: %d).");
|
||||
break;
|
||||
default:
|
||||
throw new IOException("Unsupported control line.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
int maximum = Math.min(length + 2, readBuffer.length);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interprets the modem data send as part of each bulk transfer
|
||||
* to the in endpoint.
|
||||
*
|
||||
* @param data1 The first byte.
|
||||
* @param data2 The second byte.
|
||||
*/
|
||||
protected void interpretModemData(byte data1, byte data2) {
|
||||
// check listener multiple times to handle reentrant code
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.CTS, (data1 & 0x10) == 0x10);
|
||||
if (flowControlListener != null) flowControlListener.onLineSet(InputControlLine.DSR, (data1 & 0x20) == 0x20);
|
||||
if (lineErrorListener != null && (data2 & 0x10) == 0x10) lineErrorListener.onLineError(LineError.BREAK);
|
||||
if (lineErrorListener != null && (data2 & 0x20) == 0x20) lineErrorListener.onLineError(LineError.OVERRUN);
|
||||
if (lineErrorListener != null && (data2 & 0x40) == 0x40) lineErrorListener.onLineError(LineError.PARITY);
|
||||
if (lineErrorListener != null && (data2 & 0x80) == 0x80) lineErrorListener.onLineError(LineError.FRAMING);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void write(byte[] buffer, int offset, int length) throws IOException {
|
||||
while (length > 0) {
|
||||
int res = connection.bulkTransfer(out, buffer, offset, buffer.length, 0);
|
||||
if (res < 0) throw new IOException(String.format("Failed to write bytes (error: %d)", res));
|
||||
offset += res;
|
||||
length -= res;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
sendControlTransfer(SIO_MODEM_CTRL, SET_MODEM_CTRL_DEFAULT3);
|
||||
sendControlTransfer(SIO_MODEM_CTRL, SET_MODEM_CTRL_DEFAULT4);
|
||||
super.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user