Back to project page usb-serial-for-android.
The source code is released under:
GNU Lesser General Public License
If you think the Android project usb-serial-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright 2011 Google Inc. *// w w w.j ava2 s.c om * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * Project home page: http://code.google.com/p/usb-serial-for-android/ */ package com.hoho.android.usbserial.driver; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import java.io.IOException; /** * Driver interface for a supported USB serial device. * * @author mike wakerly (opensource@hoho.com) */ public abstract class UsbSerialDriver { public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024; public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024; protected final UsbDevice mDevice; protected final UsbDeviceConnection mConnection; protected final Object mReadBufferLock = new Object(); protected final Object mWriteBufferLock = new Object(); /** Internal read buffer. Guarded by {@link #mReadBufferLock}. */ protected byte[] mReadBuffer; /** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */ protected byte[] mWriteBuffer; public static final int DATABITS_5 = 5; public static final int DATABITS_6 = 6; public static final int DATABITS_7 = 7; public static final int DATABITS_8 = 8; public static final int FLOWCONTROL_NONE = 0; public static final int FLOWCONTROL_RTSCTS_IN = 1; public static final int FLOWCONTROL_RTSCTS_OUT = 2; public static final int FLOWCONTROL_XONXOFF_IN = 4; public static final int FLOWCONTROL_XONXOFF_OUT = 8; public static final int PARITY_EVEN = 2; public static final int PARITY_MARK = 3; public static final int PARITY_NONE = 0; public static final int PARITY_ODD = 1; public static final int PARITY_SPACE = 4; /** 1 stop bit. */ public static final int STOPBITS_1 = 1; /** 1.5 stop bits. */ public static final int STOPBITS_1_5 = 3; /** 2 stop bits. */ public static final int STOPBITS_2 = 2; public UsbSerialDriver(UsbDevice device, UsbDeviceConnection connection) { mDevice = device; mConnection = connection; mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE]; mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE]; } /** * Opens and initializes the device as a USB serial device. Upon success, * caller must ensure that {@link #close()} is eventually called. * * @throws IOException on error opening or initializing the device. */ public abstract void open() throws IOException; /** * Closes the serial device. * * @throws IOException on error closing the device. */ public abstract void close() throws IOException; /** * Reads as many bytes as possible into the destination buffer. * * @param dest the destination byte buffer * @param timeoutMillis the timeout for reading * @return the actual number of bytes read * @throws IOException if an error occurred during reading */ public abstract int read(final byte[] dest, final int timeoutMillis) throws IOException; /** * Writes as many bytes as possible from the source buffer. * * @param src the source byte buffer * @param timeoutMillis the timeout for writing * @return the actual number of bytes written * @throws IOException if an error occurred during writing */ public abstract int write(final byte[] src, final int timeoutMillis) throws IOException; /** * Sets the baud rate of the serial device. * * @param baudRate the desired baud rate, in bits per second * @return the actual rate set * @throws IOException on error setting the baud rate * @deprecated Use {@link #setParameters(int, int, int, int)} instead of this method. */ @Deprecated public abstract int setBaudRate(final int baudRate) throws IOException; /** * Sets various serial port parameters. * * @param baudRate baud rate as an integer, for example {@code 115200}. * @param dataBits one of {@link #DATABITS_5}, {@link #DATABITS_6}, * {@link #DATABITS_7}, or {@link #DATABITS_8}. * @param stopBits one of {@link #STOPBITS_1}, {@link #STOPBITS_1_5}, or * {@link #STOPBITS_2}. * @param parity one of {@link #PARITY_NONE}, {@link #PARITY_ODD}, * {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or * {@link #PARITY_SPACE}. * @throws IOException on error setting the port parameters */ public abstract void setParameters( int baudRate, int dataBits, int stopBits, int parity) throws IOException; /** * Gets the CD (Carrier Detect) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getCD() throws IOException; /** * Gets the CTS (Clear To Send) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getCTS() throws IOException; /** * Gets the DSR (Data Set Ready) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getDSR() throws IOException; /** * Gets the DTR (Data Terminal Ready) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getDTR() throws IOException; /** * Sets the DTR (Data Terminal Ready) bit on the underlying UART, if * supported. * * @param value the value to set * @throws IOException if an error occurred during writing */ public abstract void setDTR(boolean value) throws IOException; /** * Gets the RI (Ring Indicator) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getRI() throws IOException; /** * Gets the RTS (Request To Send) bit from the underlying UART. * * @return the current state, or {@code false} if not supported. * @throws IOException if an error occurred during reading */ public abstract boolean getRTS() throws IOException; /** * Sets the RTS (Request To Send) bit on the underlying UART, if * supported. * * @param value the value to set * @throws IOException if an error occurred during writing */ public abstract void setRTS(boolean value) throws IOException; /** * Returns the currently-bound USB device. * * @return the device */ public final UsbDevice getDevice() { return mDevice; } /** * Sets the size of the internal buffer used to exchange data with the USB * stack for read operations. Most users should not need to change this. * * @param bufferSize the size in bytes */ public final void setReadBufferSize(int bufferSize) { synchronized (mReadBufferLock) { if (bufferSize == mReadBuffer.length) { return; } mReadBuffer = new byte[bufferSize]; } } /** * Sets the size of the internal buffer used to exchange data with the USB * stack for write operations. Most users should not need to change this. * * @param bufferSize the size in bytes */ public final void setWriteBufferSize(int bufferSize) { synchronized (mWriteBufferLock) { if (bufferSize == mWriteBuffer.length) { return; } mWriteBuffer = new byte[bufferSize]; } } }