{"id":13647102,"url":"https://github.com/felHR85/UsbSerial","last_synced_at":"2025-04-21T21:32:36.698Z","repository":{"id":15211524,"uuid":"17939962","full_name":"felHR85/UsbSerial","owner":"felHR85","description":"Usb serial controller for Android","archived":false,"fork":false,"pushed_at":"2024-08-01T18:00:08.000Z","size":2025,"stargazers_count":1813,"open_issues_count":129,"forks_count":590,"subscribers_count":96,"default_branch":"master","last_synced_at":"2024-10-29T15:36:35.597Z","etag":null,"topics":["android","android-hardware","android-serial-port","android-usb-host","ftdi-device","ftdi-devices","hardware","iot","iot-device","serial","serialport","spi","usb","usb-serial-adapters","usb-serial-controller"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/felHR85.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-03-20T11:23:57.000Z","updated_at":"2024-10-28T06:29:59.000Z","dependencies_parsed_at":"2022-07-12T15:13:40.227Z","dependency_job_id":"ca5eca25-ae10-4ff1-8e31-693a9d4fd85a","html_url":"https://github.com/felHR85/UsbSerial","commit_stats":null,"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felHR85%2FUsbSerial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felHR85%2FUsbSerial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felHR85%2FUsbSerial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felHR85%2FUsbSerial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felHR85","download_url":"https://codeload.github.com/felHR85/UsbSerial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223880444,"owners_count":17219123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-hardware","android-serial-port","android-usb-host","ftdi-device","ftdi-devices","hardware","iot","iot-device","serial","serialport","spi","usb","usb-serial-adapters","usb-serial-controller"],"created_at":"2024-08-02T01:03:20.623Z","updated_at":"2024-11-09T20:31:02.604Z","avatar_url":"https://github.com/felHR85.png","language":"Java","readme":"UsbSerial [![Build Status](https://travis-ci.org/felHR85/UsbSerial.svg?branch=master)](https://travis-ci.org/felHR85/UsbSerial) [![](https://jitpack.io/v/felHR85/UsbSerial.svg)](https://jitpack.io/#felHR85/UsbSerial) [![AndroidArsenal](https://img.shields.io/badge/Android%20Arsenal-UsbSerial-green.svg?style=true)](https://android-arsenal.com/details/1/4162) [![Join the chat at https://gitter.im/UsbSerial/Lobby](https://badges.gitter.im/UsbSerial/Lobby.svg)](https://gitter.im/UsbSerial/Lobby?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge) \n=========\n\nUsbSerial wiki available. Read it first\n--------------------------------------\n[**Getting started**](https://github.com/felHR85/UsbSerial/wiki/2.-Getting-Started)\\\n[**Create UsbSerialDevice objects**](https://github.com/felHR85/UsbSerial/wiki/3.-Create-UsbSerialDevice)\\\n[**Asynchronous api**](https://github.com/felHR85/UsbSerial/wiki/4.-Asynchronous-api)\\\n[**Synchronous api**](https://github.com/felHR85/UsbSerial/wiki/5.-Synchronous-api)\\\n[**InputStream and OutputStream I/O**](https://github.com/felHR85/UsbSerial/wiki/6.-InputStream-and-OutputStream-I-O)\\\n[**Multiple Serial ports**](https://github.com/felHR85/UsbSerial/wiki/7.-Multiple-Serial-ports)\\\n[**Projects using UsbSerial**](https://github.com/felHR85/UsbSerial/wiki/8.-Projects-using-UsbSerial)\\\n[**Debugging over Wifi**](https://github.com/felHR85/UsbSerial/wiki/9.-Debugging-over-Wifi)\\\n[**UsbSerial video tutorials**](https://github.com/felHR85/UsbSerial/wiki/10.-UsbSerial-video-tutorials)\n\n[I am looking for collaborators to keep this project updated](https://github.com/felHR85/UsbSerial/issues/313)\n--------------------------------------\n\nSupport UsbSerial\n--------------------------------------\n[If UsbSerial helped you with your projects please consider donating a little sum](https://www.paypal.me/felhr)\\\n[Or consider buying DroidTerm Pro: A Usb serial port terminal using UsbSerial](https://play.google.com/store/apps/details?id=com.felhr.droidtermpro)\n\nDevices Supported\n--------------------------------------\n[CP210X devices](http://www.silabs.com/products/mcu/pages/usbtouartbridgevcpdrivers.aspx) Default: 9600,8,1,None,flow off\n\n[CDC devices](https://en.wikipedia.org/wiki/USB_communications_device_class) Default 115200,8,1,None,flow off\n\n[FTDI devices](http://www.ftdichip.com/FTProducts.htm) Default: 9600,8,1,None,flow off\n\n[PL2303 devices](http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225\u0026pcid=41) Default 9600,8,1,None,flow off\n\n[CH34x devices](https://www.olimex.com/Products/Breadboarding/BB-CH340T/resources/CH340DS1.PDF) Default 9600,8,1,None,flow off\n\n[CP2130 SPI-USB](http://www.silabs.com/products/interface/usb-bridges/classic-usb-bridges/Pages/usb-to-spi-bridge.aspx)\n\nKnown Issue\n--------------------------------------\nDue to a bug in Android itself, it's highly recommended to **not** use it with a device running [Android 5.1.1 Lollipop](https://en.wikipedia.org/wiki/Android_version_history#Android_5.1_Lollipop_(API_22)). See issue [#142](https://github.com/felHR85/UsbSerial/issues/142) for more details.\n\nHow to use it?\n--------------------------------------\nInstantiate a new object of the UsbSerialDevice class\n```java\nUsbDevice device;\nUsbDeviceConnection usbConnection;\n...\nUsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(device, usbConnection); \n```\n\nOpen the device and set it up as desired\n```java\nserial.open();\nserial.setBaudRate(115200);\nserial.setDataBits(UsbSerialInterface.DATA_BITS_8);\nserial.setParity(UsbSerialInterface.PARITY_ODD);\nserial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); \n```\n\nIf flow control is needed (currently only supported in CP201x and FTDI devices)\n```java\n/**\nValues:\n    UsbSerialInterface.FLOW_CONTROL_OFF\n    UsbSerialInterface.FLOW_CONTROL_RTS_CTS \n    UsbSerialInterface.FLOW_CONTROL_DSR_DTR\n**/\nserial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_RTS_CTS);\n```\n\nThere is no need to be polling if you want to perform a bulk transaction to a IN endpoint. Define a simply callback\n```java\nprivate UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {\n\n\t\t@Override\n\t\tpublic void onReceivedData(byte[] arg0) \n\t\t{\n\t\t\t// Code here :)\n\t\t}\n\t\t\n};\n```\n\nAnd pass a reference of it\n```java\nserial.read(mCallback);\n```\n\nChanges in the CTS and DSR lines will be received in the same manner. Define a callback and pass a reference of it.\n```java\nprivate UsbSerialInterface.UsbCTSCallback ctsCallback = new UsbSerialInterface.UsbCTSCallback() {\n        @Override\n        public void onCTSChanged(boolean state) {\n           // Code here :)\n        }\n    };\n    \nprivate UsbSerialInterface.UsbDSRCallback dsrCallback = new UsbSerialInterface.UsbDSRCallback() {\n        @Override\n        public void onDSRChanged(boolean state) {\n           // Code here :)\n        }\n    };\n    \nserial.getCTS(ctsCallback);\n//serial.getDSR(dsrCallback);\n```\n\n\n\nWrite something\n```java\nserial.write(\"DATA\".getBytes()); // Async-like operation now! :)\n```\n\nRaise the state of the RTS or DTR lines\n```java\nserial.setRTS(true); // Raised\nserial.setRTS(false); // Not Raised\nserial.setDTR(true); // Raised\nserial.setDTR(false); // Not Raised\n```\n\nClose the device:\n```java\nserial.close();\n```\n\nI 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:\n```java\npublic boolean syncOpen();\npublic int syncWrite(byte[] buffer, int timeout)\npublic int syncRead(byte[] buffer, int timeout)\npublic void syncClose();\n```\n\n\nIn Android usb api, when a usb device has been close it must be reopened\n```java\nUsbDevice device;\n...\nUsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);\nmanager.openDevice(UsbDevice device)\n```\nHow to use the SPI interface (BETA)\n--------------------------------------\nSupport 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.\n\n```java\nUsbSpiDevice spi = UsbSpiDevice.createUsbSerialDevice(device, connection);\nspi.connectSPI();\nspi.selectSlave(0);\nspi.setClock(CP2130SpiDevice.CLOCK_3MHz);\n```\nDefine the usual callback\n\n```java\nprivate UsbSpiInterface.UsbMISOCallback misoCallback = new UsbSpiInterface.UsbMISOCallback()\n    {\n        @Override\n        public int onReceivedData(byte[] data) {\n             // Your code here :)\n        }\n    };\n//...\nspi.setMISOCallback(misoCallback);\n```\n\n```java\nspi.writeMOSI(\"Hola!\".getBytes()); // Write \"Hola!\" to the selected slave through MOSI (MASTER OUTPUT SLAVE INPUT)\nspi.readMISO(5); // Read 5 bytes from the MISO (MASTER INPUT SLAVE OUTPUT) line. Data will be received through UsbMISOCallback\nspi.writeRead(\"Hola!\".getBytes(), 15); // Write \"Hola!\" and read 15 bytes synchronously\n```\n\nClose the device when done\n\n```java\nspi.closeSPI();\n```\n\nGradle\n--------------------------------------\nAdd the jitpack repo to your your project's build.gradle at the end of repositories\n\n/build.gradle\n```groovy\nallprojects {\n\trepositories {\n\t\tjcenter()\n\t\tmaven { url \"https://jitpack.io\" }\n\t}\n}\n```\n\nThen add the dependency to your module's build.gradle:\n\n/app/build.gradle\n```groovy\nimplementation 'com.github.felHR85:UsbSerial:6.1.0'\n```\n\nTO-DO\n--------------------------------------\n- RTS/CTS and DSR/DTR implementations for PL2303 and CDC\n\n\n\n\n\n\n","funding_links":["https://www.paypal.me/felhr"],"categories":["Java","Serial port"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FfelHR85%2FUsbSerial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FfelHR85%2FUsbSerial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FfelHR85%2FUsbSerial/lists"}