UsbSerial for android.
 
 
 
Go to file
green-green-avk 7ad6c9f688
Merge pull request #334 from green-green-avk/build-env-update
Updating build settings to support current Android Studio and its JRE
2023-04-28 18:35:06 -07:00
example Merge pull request #194 from felHR85/example_close 2019-01-12 12:19:24 +01:00
examplemultipleports changed target version 2018-11-18 14:50:22 +01:00
examplestreams Merge pull request #194 from felHR85/example_close 2019-01-12 12:19:24 +01:00
examplesync Merge pull request #194 from felHR85/example_close 2019-01-12 12:19:24 +01:00
gradle/wrapper Gradle 6.7.1 / AGP 4.2.1 2021-06-14 20:31:33 -07:00
integration changed sync tests 2019-07-05 22:44:45 +02:00
integrationapp changed sync tests 2019-07-05 22:44:45 +02:00
usbserial FTDI: Fix baudrate setting on multiport devices 2021-06-14 18:44:59 -07:00
.gitignore updating gitignore for python 2019-02-23 22:29:05 +01:00
.travis.yml travis with build tools 27.0.3 2018-03-27 01:20:50 +02:00
CHANGELOG.md changelog 2019-07-11 23:39:59 +02:00
LICENSE Initial commit 2014-03-20 04:23:57 -07:00
README.md Looking for collaborators 2020-06-12 18:11:16 +02:00
RELEASING.md Update RELEASING.md 2021-06-14 20:23:58 -07:00
build.gradle jcenter() expired: => mavenCentral() 2021-06-14 20:31:38 -07:00
gradle.properties release 6.1.0 2019-07-11 23:31:57 +02:00
gradlew Upgrade Android Gradle plugin to version 3.0.1 2018-01-04 17:19:06 +01:00
gradlew.bat Update project to gradle build system. 2015-12-10 10:05:54 +01:00
release.sh release.sh a+x 2021-06-14 20:31:38 -07:00
settings.gradle started integration test app 2019-02-24 19:29:01 +01:00

README.md

UsbSerial Build Status AndroidArsenal Join the chat at https://gitter.im/UsbSerial/Lobby

UsbSerial wiki available. Read it first

Getting started
Create UsbSerialDevice objects
Asynchronous api
Synchronous api
InputStream and OutputStream I/O
Multiple Serial ports
Projects using UsbSerial
Debugging over Wifi
UsbSerial video tutorials

I am looking for collaborators to keep this project updated

Support UsbSerial

If UsbSerial helped you with your projects please consider donating a little sum
Or consider buying DroidTerm Pro: A Usb serial port terminal using UsbSerial

Devices Supported

CP210X devices Default: 9600,8,1,None,flow off

CDC devices Default 115200,8,1,None,flow off

FTDI devices Default: 9600,8,1,None,flow off

PL2303 devices Default 9600,8,1,None,flow off

CH34x devices Default 9600,8,1,None,flow off

CP2130 SPI-USB

Known Issue

Due to a bug in Android itself, it's highly recommended to not use it with a device running Android 5.1.1 Lollipop. See issue #142 for more details.

How to use it?

Instantiate a new object of the UsbSerialDevice class

UsbDevice device;
UsbDeviceConnection usbConnection;
...
UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(device, usbConnection); 

Open the device and set it up as desired

serial.open();
serial.setBaudRate(115200);
serial.setDataBits(UsbSerialInterface.DATA_BITS_8);
serial.setParity(UsbSerialInterface.PARITY_ODD);
serial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); 

If flow control is needed (currently only supported in CP201x and FTDI devices)

/**
Values:
    UsbSerialInterface.FLOW_CONTROL_OFF
    UsbSerialInterface.FLOW_CONTROL_RTS_CTS 
    UsbSerialInterface.FLOW_CONTROL_DSR_DTR
**/
serial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_RTS_CTS);

There is no need to be polling if you want to perform a bulk transaction to a IN endpoint. Define a simply callback

private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {

		@Override
		public void onReceivedData(byte[] arg0) 
		{
			// Code here :)
		}
		
};

And pass a reference of it

serial.read(mCallback);

Changes in the CTS and DSR lines will be received in the same manner. Define a callback and pass a reference of it.

private UsbSerialInterface.UsbCTSCallback ctsCallback = new UsbSerialInterface.UsbCTSCallback() {
        @Override
        public void onCTSChanged(boolean state) {
           // Code here :)
        }
    };
    
private UsbSerialInterface.UsbDSRCallback dsrCallback = new UsbSerialInterface.UsbDSRCallback() {
        @Override
        public void onDSRChanged(boolean state) {
           // Code here :)
        }
    };
    
serial.getCTS(ctsCallback);
//serial.getDSR(dsrCallback);

Write something

serial.write("DATA".getBytes()); // Async-like operation now! :)

Raise the state of the RTS or DTR lines

serial.setRTS(true); // Raised
serial.setRTS(false); // Not Raised
serial.setDTR(true); // Raised
serial.setDTR(false); // Not Raised

Close the device:

serial.close();

I recommend using UsbSerial as shown above but if you want to perform write and read operations in synchronous way it is possible using these methods:

public boolean syncOpen();
public int syncWrite(byte[] buffer, int timeout)
public int syncRead(byte[] buffer, int timeout)
public void syncClose();

In Android usb api, when a usb device has been close it must be reopened

UsbDevice device;
...
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
manager.openDevice(UsbDevice device)

How to use the SPI interface (BETA)

Support for USB to SPI devices was added recently but it is still in beta. Although I tried to keep the api as close to standard UsbSerial api as possible, be aware because the beta nature of this feature this api may change in the future. Only CP2130 chipset is supported at the moment.

UsbSpiDevice spi = UsbSpiDevice.createUsbSerialDevice(device, connection);
spi.connectSPI();
spi.selectSlave(0);
spi.setClock(CP2130SpiDevice.CLOCK_3MHz);

Define the usual callback

private UsbSpiInterface.UsbMISOCallback misoCallback = new UsbSpiInterface.UsbMISOCallback()
    {
        @Override
        public int onReceivedData(byte[] data) {
             // Your code here :)
        }
    };
//...
spi.setMISOCallback(misoCallback);
spi.writeMOSI("Hola!".getBytes()); // Write "Hola!" to the selected slave through MOSI (MASTER OUTPUT SLAVE INPUT)
spi.readMISO(5); // Read 5 bytes from the MISO (MASTER INPUT SLAVE OUTPUT) line. Data will be received through UsbMISOCallback
spi.writeRead("Hola!".getBytes(), 15); // Write "Hola!" and read 15 bytes synchronously

Close the device when done

spi.closeSPI();

Gradle

Add the jitpack repo to your your project's build.gradle at the end of repositories

/build.gradle

allprojects {
	repositories {
		jcenter()
		maven { url "https://jitpack.io" }
	}
}

Then add the dependency to your module's build.gradle:

/app/build.gradle

implementation 'com.github.felHR85:UsbSerial:6.1.0'

TO-DO

  • RTS/CTS and DSR/DTR implementations for PL2303 and CDC