(UsbManager.EXTRA_DEVICE)
119 | val success = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
120 | L.d("permission response :" + device.hashCode() + "===" + success + "===" + device)
121 | requestContinue(context, getUsbManager(context))
122 | }
123 | }
124 | }
125 | }
126 |
127 | companion object {
128 |
129 | fun newInstance(): RequestUsbPermission {
130 | return RequestUsbPermission()
131 | }
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/help/RootCmd.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.help
2 |
3 |
4 | /**
5 | * Created by hd on 2017/8/22.
6 | * 执行Linux命令,修改usb权限为可读
7 | */
8 | object RootCmd {
9 |
10 | // 开放/dev目录下所有文件的读、写、执行权限, " -R " 指令勿随意指定,较危险操作;
11 | // 其他指令例如下 :
12 | //
13 | // "chmod 777 /dev;"
14 | // + "chmod 777 /dev/;"
15 | // + "chmod 777 /dev/usb/;"
16 | // + "chmod 777 /dev/bus/;"
17 | // + "chmod 777 /dev/bus/usb/;"
18 | // + "chmod 777 /dev/bus/usb/0*;"
19 | // + "chmod 777 /dev/bus/usb/001/*;"
20 | // + "chmod 777 /dev/bus/usb/002/*;"
21 | // + "chmod 777 /dev/bus/usb/003/*;"
22 | // + "chmod 777 /dev/bus/usb/004/*;"
23 | // + "chmod 777 /dev/bus/usb/005/*;"
24 | private val param_all = "chmod 777 /dev;" + "chmod -R 777 /dev/*"
25 |
26 | @JvmOverloads fun execRootCmdOrder(paramString: String = param_all): Int {
27 | try {
28 | val su = Runtime.getRuntime().exec("/system/bin/su")
29 | su.outputStream.write(paramString.toByteArray())
30 | su.outputStream.flush()
31 | su.outputStream.write("\nexit\n".toByteArray())
32 | su.outputStream.flush()
33 | su.waitFor()
34 | return su.exitValue()
35 | } catch (localException: Exception) {
36 | localException.printStackTrace()
37 | }
38 | return 0
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/help/SystemSecurity.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.help
2 |
3 | import android.content.Context
4 | import android.hardware.usb.UsbDevice
5 | import android.hardware.usb.UsbManager
6 | import com.hd.serialport.utils.L
7 |
8 | /**
9 | * Created by hd on 2017/8/22.
10 | *
11 | * some vendors provide SDK maybe exists error in the usb module.
12 | *
13 | * the error come from the API [UsbDevice.getInterface] and belongs to the system SDK error,
14 | * might throw a error [NullPointerException] after when you traverse all usb and call it.
15 | *
16 | * this error need the vendors to modify the underlying code,
17 | * however,in fact, we can also go to avoid it by use API[UsbDevice.getInterfaceCount] to judge,
18 | * but,in order to avoid other unknown problems to modify the underlying code is the best way.
19 | *
20 | * in theory, you only need to check once,so ,advice used that in project debugging or test project.
21 | */
22 | object SystemSecurity {
23 |
24 | /**
25 | * @return return true if the current system security's support usb devices
26 | */
27 | fun check(context: Context): Boolean {
28 | return try {
29 | val usbManager = context.applicationContext.getSystemService(Context.USB_SERVICE) as UsbManager
30 | val deviceList = usbManager.deviceList
31 | for (usbDevice in deviceList.values)
32 | usbDevice.getInterface(0)
33 | true
34 | } catch (e: Exception) {
35 | L.d("TAG", "There are errors in the current system usb module :$e")
36 | false
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/listener/MeasureListener.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.listener
2 |
3 |
4 | /**
5 | * Created by hd on 2017/8/22 .
6 | *
7 | */
8 | interface MeasureListener {
9 |
10 | /**
11 | * hint measure error message
12 | * @param tag [com.hd.serialport.param.SerialPortMeasureParameter.tag]
13 | */
14 | fun measureError(tag: Any?, message: String)
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/listener/SerialPortMeasureListener.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.listener
2 |
3 | import java.io.OutputStream
4 |
5 |
6 | /**
7 | * Created by hd on 2017/8/27 .
8 | *
9 | */
10 | interface SerialPortMeasureListener : MeasureListener {
11 | /**
12 | * receive data from serial port
13 | * @param path serial port device path[com.hd.serialport.param.SerialPortMeasureParameter.devicePath]
14 | * @param tag [com.hd.serialport.param.SerialPortMeasureParameter.tag]
15 | */
16 | fun measuring(tag: Any?, path: String, data: ByteArray)
17 |
18 | /**
19 | * only initialize one time,write data into serial port [OutputStream.write]
20 | * @param tag [com.hd.serialport.param.SerialPortMeasureParameter.tag]
21 | */
22 | fun write(tag: Any?, outputStream: OutputStream)
23 |
24 | }
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/listener/UsbMeasureListener.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.listener
2 |
3 | import com.hd.serialport.usb_driver.UsbSerialPort
4 |
5 | /**
6 | * Created by hd on 2017/8/27 .
7 |
8 | */
9 | interface UsbMeasureListener : MeasureListener {
10 |
11 | /**
12 | * receive data[data] from usb port[usbSerialPort]
13 | * @param tag [com.hd.serialport.param.SerialPortMeasureParameter.tag]
14 | */
15 | fun measuring(tag: Any?, usbSerialPort: UsbSerialPort, data: ByteArray)
16 |
17 | /**
18 | * only initialize one time,write data into usb port [UsbSerialPort.write]
19 | * @param tag [com.hd.serialport.param.SerialPortMeasureParameter.tag]
20 | */
21 | fun write(tag: Any?, usbSerialPort: UsbSerialPort)
22 | }
23 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/method/DeviceMeasureController.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.method
2 |
3 | import android.annotation.SuppressLint
4 | import android.content.Context
5 | import android.hardware.usb.UsbDevice
6 | import android.hardware.usb.UsbManager
7 | import android.serialport.SerialPortFinder
8 | import com.hd.serialport.config.UsbPortDeviceType
9 | import com.hd.serialport.engine.SerialPortEngine
10 | import com.hd.serialport.engine.UsbPortEngine
11 | import com.hd.serialport.help.RequestUsbPermission
12 | import com.hd.serialport.help.SystemSecurity
13 | import com.hd.serialport.listener.SerialPortMeasureListener
14 | import com.hd.serialport.listener.UsbMeasureListener
15 | import com.hd.serialport.param.SerialPortMeasureParameter
16 | import com.hd.serialport.param.UsbMeasureParameter
17 | import com.hd.serialport.usb_driver.UsbSerialDriver
18 | import com.hd.serialport.usb_driver.UsbSerialPort
19 | import com.hd.serialport.usb_driver.UsbSerialProber
20 | import com.hd.serialport.utils.L
21 | import java.util.concurrent.ConcurrentHashMap
22 |
23 | /**
24 | * Created by hd on 2017/8/22 .
25 | * usb device measurement controller
26 | */
27 | @SuppressLint("StaticFieldLeak")
28 | object DeviceMeasureController {
29 |
30 | private lateinit var usbManager: UsbManager
31 |
32 | private lateinit var usbPortEngine: UsbPortEngine
33 |
34 | private lateinit var serialPortEngine: SerialPortEngine
35 |
36 | fun init(context: Context, openLog: Boolean) {
37 | init(context, openLog, null)
38 | }
39 |
40 | fun init(context: Context, openLog: Boolean, callback: RequestUsbPermission.RequestPermissionCallback? = null) {
41 | init(context, openLog, true, callback)
42 | }
43 |
44 | fun init(context: Context, openLog: Boolean, requestUsbPermission: Boolean, callback: RequestUsbPermission.RequestPermissionCallback? = null) {
45 | if (!SystemSecurity.check(context)) throw RuntimeException("There are a error in the current system usb module !")
46 | L.allowLog = openLog
47 | this.usbManager = context.applicationContext.getSystemService(Context.USB_SERVICE) as UsbManager
48 | serialPortEngine = SerialPortEngine(context.applicationContext)
49 | usbPortEngine = UsbPortEngine(context.applicationContext, usbManager)
50 | if (requestUsbPermission)
51 | RequestUsbPermission.newInstance().requestAllUsbDevicePermission(context.applicationContext, callback)
52 | }
53 |
54 | fun scanUsbPort(): List = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager)
55 |
56 | fun scanSerialPort(): ConcurrentHashMap = SerialPortFinder().allDevices
57 |
58 | fun measure(usbDevice: UsbDevice, deviceType: UsbPortDeviceType, parameter: UsbMeasureParameter, listener: UsbMeasureListener) {
59 | val driver = UsbSerialProber.getDefaultProber().convertDriver(usbDevice,deviceType.value)
60 | measure(driver.ports[0], parameter, listener)
61 | }
62 |
63 | fun measure(usbSerialDriverList: List?, parameter: UsbMeasureParameter, listener: UsbMeasureListener) {
64 | if (!usbSerialDriverList.isNullOrEmpty()) {
65 | usbSerialDriverList.filter { it.deviceType == parameter.usbPortDeviceType || parameter.usbPortDeviceType == UsbPortDeviceType.USB_OTHERS }
66 | .filter { it.ports[0] != null }.forEach { measure(it.ports[0], parameter, listener) }
67 | } else {
68 | val portList = scanUsbPort()
69 | if (portList.isNullOrEmpty()) {
70 | measure(portList, parameter, listener)
71 | } else {
72 | listener.measureError(parameter.tag,"not find ports")
73 | }
74 | }
75 | }
76 |
77 | fun measure(usbSerialPort: UsbSerialPort?, parameter: UsbMeasureParameter, listener: UsbMeasureListener) {
78 | if (null != usbSerialPort) {
79 | usbPortEngine.open(usbSerialPort, parameter, listener)
80 | } else {
81 | measure(usbSerialDriverList = null, parameter = parameter, listener = listener)
82 | }
83 | }
84 |
85 | fun measure(paths: Array?, parameter: SerialPortMeasureParameter, listeners: List) {
86 | if (!paths.isNullOrEmpty()) {
87 | for (index in paths.indices) {
88 | val path = paths[index]
89 | when {
90 | path.isNotEmpty() -> {
91 | parameter.devicePath = path
92 | when {
93 | listeners.size == paths.size -> measure(parameter, listeners[index])
94 | listeners.isNotEmpty() -> measure(parameter, listeners[0])
95 | else -> L.d("not find serialPortMeasureListener")
96 | }
97 | }
98 | index < listeners.size -> listeners[index].measureError(parameter.tag,"path is null")
99 | else -> L.d("current position $index path is empty :$path ")
100 | }
101 | }
102 | } else {
103 | measure(SerialPortFinder().allDevicesPath, parameter, listeners)
104 | }
105 | }
106 |
107 | fun measure(parameter: SerialPortMeasureParameter, listener: SerialPortMeasureListener) {
108 | if (!parameter.devicePath.isNullOrEmpty()) {
109 | serialPortEngine.open(parameter, listener)
110 | } else {
111 | measure(paths = null, parameter = parameter, listeners = listOf(listener))
112 | }
113 | }
114 |
115 | /**write data by the tag filter, write all if tag==null*/
116 | fun write(data: List?, tag: Any? = null) {
117 | L.d("DeviceMeasureController write usbPortEngine is working ${usbPortEngine.isWorking()}," +
118 | "serialPortEngine is working ${serialPortEngine.isWorking()}")
119 | when {
120 | usbPortEngine.isWorking() -> usbPortEngine.write(tag, data)
121 | serialPortEngine.isWorking() -> serialPortEngine.write(tag, data)
122 | }
123 | }
124 |
125 | /**stop engine by the tag filter, stop all if tag==null*/
126 | fun stop(tag: Any? = null) {
127 | L.d("DeviceMeasureController stop")
128 | when {
129 | usbPortEngine.isWorking() -> usbPortEngine.stop(tag)
130 | serialPortEngine.isWorking() -> serialPortEngine.stop(tag)
131 | }
132 | }
133 | }
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/param/MeasureParameter.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.param
2 |
3 | import java.io.Serializable
4 |
5 |
6 | /**
7 | * Created by hd on 2017/8/28 .
8 | *
9 | */
10 | abstract class MeasureParameter : Serializable
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/param/SerialPortMeasureParameter.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.param
2 |
3 |
4 | /**
5 | * Created by hd on 2017/8/27 .
6 | * @param devicePath serial port path [android.serialport.SerialPortFinder.getAllDevicesPath]
7 | * @param baudRate baud rate as an integer, for example {@code 115200}.
8 | * @param flags default value :0
9 | * @param tag set tag
10 | */
11 | data class SerialPortMeasureParameter(var devicePath: String? = null, var baudRate: Int = 115200,
12 | var flags: Int = 0, var tag :Any?="default_serial_tag"):MeasureParameter()
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/param/UsbMeasureParameter.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.param
2 |
3 | import com.hd.serialport.config.UsbPortDeviceType
4 |
5 |
6 | /**
7 | * Created by hd on 2017/8/22 .
8 | * Sets various serial port parameters.
9 | * @param usbPortDeviceType set usb type [UsbPortDeviceType]
10 | * @param baudRate baud rate as an integer, for example {@code 115200}.
11 | * @param dataBits one of {@link UsbSerialPort#DATABITS_5}, {@link UsbSerialPort#DATABITS_6},
12 | * {@link UsbSerialPort#DATABITS_7}, or {@link UsbSerialPort#DATABITS_8}.
13 | * @param stopBits one of {@link UsbSerialPort#STOPBITS_1}, {@link UsbSerialPort#STOPBITS_1_5}, or
14 | * {@link UsbSerialPort#STOPBITS_2}.
15 | * @param parity one of {@link UsbSerialPort#PARITY_NONE}, {@link UsbSerialPort#PARITY_ODD},
16 | * {@link UsbSerialPort#PARITY_EVEN}, {@link UsbSerialPort#PARITY_MARK}, or
17 | * {@link UsbSerialPort#PARITY_SPACE}.
18 | * @param tag set tag
19 | */
20 | data class UsbMeasureParameter(var usbPortDeviceType: UsbPortDeviceType?=UsbPortDeviceType.USB_OTHERS,
21 | var baudRate: Int = 115200, var dataBits: Int = 8, var stopBits: Int = 1,
22 | var parity: Int = 0,var tag : Any?="default_usb_tag"):MeasureParameter()
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/reader/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/java/com/hd/serialport/reader/.DS_Store
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/reader/ReadWriteRunnable.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.reader
2 |
3 | import android.content.Context
4 | import com.hd.serialport.R
5 | import com.hd.serialport.config.MeasureStatus
6 | import com.hd.serialport.listener.MeasureListener
7 | import com.hd.serialport.utils.L
8 | import java.nio.ByteBuffer
9 |
10 |
11 | /**
12 | * Created by hd on 2017/8/27 .
13 | *
14 | */
15 | abstract class ReadWriteRunnable(val tag: Any?, val context: Context, val measureListener: MeasureListener) : Runnable {
16 |
17 | private val MAX_BUFFER_SIZE = 16 * 4096
18 |
19 | protected val READ_WAIT_MILLIS = 100
20 |
21 | private var status = MeasureStatus.PREPARE
22 |
23 | protected val readBuffer = ByteBuffer.allocate(MAX_BUFFER_SIZE)!!
24 |
25 | private val writeBuffer = ByteBuffer.allocate(MAX_BUFFER_SIZE)!!
26 |
27 | init {
28 | status = MeasureStatus.RUNNING
29 | }
30 |
31 | /**
32 | * allow write data into port,it`s recommended async write to use
33 | * [com.hd.serialport.listener.SerialPortMeasureListener.write] or
34 | * [com.hd.serialport.listener.UsbMeasureListener.write]
35 | * at write large data volumes
36 | **/
37 | fun write(data: ByteArray) {
38 | synchronized(writeBuffer) {
39 | writeBuffer.put(data)
40 | }
41 | }
42 |
43 | fun stop() {
44 | L.d("read-write runnable stop 1 :$status")
45 | if (status != MeasureStatus.STOPPED || status != MeasureStatus.STOPPING) {
46 | status = MeasureStatus.STOPPING
47 | readBuffer.clear()
48 | writeBuffer.clear()
49 | close()
50 | status = MeasureStatus.STOPPED
51 | L.d("read-write runnable stop 2 :$status")
52 | }
53 | }
54 |
55 | abstract fun writing(byteArray: ByteArray)
56 |
57 | abstract fun reading()
58 |
59 | abstract fun close()
60 |
61 | override fun run() {
62 | while (status != MeasureStatus.STOPPED) {
63 | try {
64 | reading()
65 | } catch (ignored: Exception) {
66 | L.d("reading into port error :$ignored")
67 | measureListener.measureError(tag, context.resources.getString(R.string.measure_target_device_error))
68 | try {
69 | close()
70 | } catch (ignored: Exception) {
71 | L.d("close error :$ignored")
72 | }
73 | status = MeasureStatus.STOPPED
74 | break
75 | }
76 | try {
77 | writing()
78 | } catch (ignored: Exception) {
79 | L.d("writing into port error:$ignored")
80 | }
81 | }
82 | }
83 |
84 | private fun writing() {
85 | synchronized(writeBuffer) {
86 | val len = writeBuffer.position()
87 | if (len > 0) {
88 | val outBuff = ByteArray(len)
89 | writeBuffer.rewind()
90 | writeBuffer.get(outBuff, 0, len)
91 | writeBuffer.clear()
92 | writing(outBuff)
93 | }
94 | }
95 | }
96 |
97 | }
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/reader/SerialPortReadWriteRunnable.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.reader
2 |
3 | import android.serialport.SerialPort
4 | import com.hd.serialport.engine.SerialPortEngine
5 | import com.hd.serialport.listener.SerialPortMeasureListener
6 |
7 |
8 | /**
9 | * Created by hd on 2017/8/27 .
10 | *
11 | */
12 | class SerialPortReadWriteRunnable(private val devicePath: String, private val serialPort: SerialPort,
13 | listener: SerialPortMeasureListener, engine: SerialPortEngine, tag: Any?) :
14 | ReadWriteRunnable(tag, engine.context, listener) {
15 | init {
16 | (measureListener as SerialPortMeasureListener).write(tag, serialPort.outputStream!!)
17 | }
18 |
19 | override fun writing(byteArray: ByteArray) {
20 | serialPort.outputStream?.write(byteArray)
21 | }
22 |
23 | override fun reading() {
24 | var length = serialPort.inputStream?.available() ?: 0
25 | if (length > 0) {
26 | length = serialPort.inputStream!!.read(readBuffer.array())
27 | val data = ByteArray(length)
28 | readBuffer.get(data, 0, length)
29 | (measureListener as SerialPortMeasureListener).measuring(tag, devicePath, data)
30 | readBuffer.clear()
31 | }
32 | }
33 |
34 | override fun close() {
35 | try {
36 | serialPort.close()
37 | } catch (e: Exception) {
38 | e.printStackTrace()
39 | } finally {
40 | try {
41 | serialPort.outputStream?.close()
42 | } catch (e: Exception) {
43 | e.printStackTrace()
44 | }
45 | try {
46 | serialPort.inputStream?.close()
47 | } catch (e: Exception) {
48 | e.printStackTrace()
49 | }
50 | }
51 | }
52 |
53 | }
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/reader/UsbReadWriteRunnable.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.reader
2 |
3 | import com.hd.serialport.config.UsbPortDeviceType
4 | import com.hd.serialport.engine.UsbPortEngine
5 | import com.hd.serialport.listener.UsbMeasureListener
6 | import com.hd.serialport.usb_driver.UsbSerialPort
7 |
8 |
9 | /**
10 | * Created by hd on 2017/8/27 .
11 | *
12 | */
13 | class UsbReadWriteRunnable(private val usbSerialPort: UsbSerialPort,
14 | listener: UsbMeasureListener, engine: UsbPortEngine, tag: Any?) :
15 | ReadWriteRunnable(tag, engine.context, listener) {
16 | init {
17 | listener.write(tag, usbSerialPort)
18 | }
19 |
20 | override fun writing(byteArray: ByteArray) {
21 | usbSerialPort.write(byteArray, READ_WAIT_MILLIS * 10)
22 | }
23 |
24 | override fun reading() {
25 | val length = usbSerialPort.read(readBuffer.array(), READ_WAIT_MILLIS)
26 | if (length > 0) {
27 | val data = ByteArray(length)
28 | readBuffer.get(data, 0, length)
29 | (measureListener as UsbMeasureListener).measuring(tag, usbSerialPort, data)
30 | }
31 | readBuffer.clear()
32 | }
33 |
34 | override fun close() {
35 | try {
36 | usbSerialPort.close()
37 | } catch (e: Exception) {
38 | e.printStackTrace()
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/CommonUsbSerialDriver.java:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.usb_driver;
2 |
3 | import android.hardware.usb.UsbDevice;
4 | import android.support.annotation.Keep;
5 | import android.support.annotation.NonNull;
6 |
7 | import com.hd.serialport.config.UsbPortDeviceType;
8 |
9 | import java.util.Collections;
10 | import java.util.List;
11 |
12 | /**
13 | * Created by hd on 2017/2/27 0027.
14 | */
15 | @Keep
16 | public abstract class CommonUsbSerialDriver implements UsbSerialDriver {
17 |
18 | public UsbDevice mDevice;
19 |
20 | public UsbSerialPort mPort;
21 |
22 | public abstract UsbSerialPort setPort(UsbDevice mDevice);
23 |
24 | @NonNull
25 | public abstract String setDriverName();
26 |
27 | public CommonUsbSerialDriver(UsbDevice mDevice) {
28 | this.mDevice = mDevice;
29 | mPort = setPort(mDevice);
30 | }
31 |
32 | @Override
33 | public UsbDevice getDevice() {
34 | return mDevice;
35 | }
36 |
37 | @Override
38 | public List getPorts() {
39 | return Collections.singletonList(mPort);
40 | }
41 |
42 | @Override
43 | public UsbPortDeviceType getDeviceType() {
44 | UsbPortDeviceType type = UsbPortDeviceType.USB_CUSTOM_TYPE;
45 | type.setValue(setDriverName());
46 | return type;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/CommonUsbSerialPort.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | import android.hardware.usb.UsbDevice;
25 | import android.hardware.usb.UsbDeviceConnection;
26 |
27 |
28 | import java.io.IOException;
29 |
30 | /**
31 | * A base class shared by several driver implementations.
32 | *
33 | * @author mike wakerly (opensource@hoho.com)
34 | */
35 | public abstract class CommonUsbSerialPort implements UsbSerialPort {
36 |
37 | public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
38 | public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
39 |
40 | protected final UsbDevice mDevice;
41 | protected final int mPortNumber;
42 |
43 | // non-null when open()
44 | protected UsbDeviceConnection mConnection = null;
45 |
46 | protected final Object mReadBufferLock = new Object();
47 | protected final Object mWriteBufferLock = new Object();
48 |
49 | /**
50 | * Internal read buffer. Guarded by {@link #mReadBufferLock}.
51 | */
52 | protected byte[] mReadBuffer;
53 |
54 | /**
55 | * Internal write buffer. Guarded by {@link #mWriteBufferLock}.
56 | */
57 | protected byte[] mWriteBuffer;
58 |
59 | public CommonUsbSerialPort(UsbDevice device, int portNumber) {
60 | mDevice = device;
61 | mPortNumber = portNumber;
62 |
63 | mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
64 | mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return String.format("<%s device_name=%s device_id=%s port_number=%s>", getClass().getSimpleName(), mDevice.getDeviceName(), mDevice.getDeviceId(), mPortNumber);
70 | }
71 |
72 | @Override
73 | public UsbDeviceConnection getUsbDeviceConnection() {
74 | return mConnection;
75 | }
76 |
77 | /**
78 | * Returns the currently-bound USB device.
79 | *
80 | * @return the device
81 | */
82 | public final UsbDevice getDevice() {
83 | return mDevice;
84 | }
85 |
86 | @Override
87 | public int getPortNumber() {
88 | return mPortNumber;
89 | }
90 |
91 | /**
92 | * Returns the device serial number
93 | *
94 | * @return serial number
95 | */
96 | @Override
97 | public String getSerial() {
98 | return mConnection.getSerial();
99 | }
100 |
101 | @Override
102 | public boolean purgeHwBuffers(boolean flushReadBuffers, boolean flushWriteBuffers) throws IOException {
103 | return !flushReadBuffers && !flushWriteBuffers;
104 | }
105 |
106 | /**
107 | * Sets the size of the internal buffer used to exchange data with the USB
108 | * stack for read operations. Most users should not need to change this.
109 | *
110 | * @param bufferSize the size in bytes
111 | */
112 | public final void setReadBufferSize(int bufferSize) {
113 | synchronized (mReadBufferLock) {
114 | if (bufferSize == mReadBuffer.length) {
115 | return;
116 | }
117 | mReadBuffer = new byte[bufferSize];
118 | }
119 | }
120 |
121 | /**
122 | * Sets the size of the internal buffer used to exchange data with the USB
123 | * stack for write operations. Most users should not need to change this.
124 | *
125 | * @param bufferSize the size in bytes
126 | */
127 | public final void setWriteBufferSize(int bufferSize) {
128 | synchronized (mWriteBufferLock) {
129 | if (bufferSize == mWriteBuffer.length) {
130 | return;
131 | }
132 | mWriteBuffer = new byte[bufferSize];
133 | }
134 | }
135 |
136 | @Override
137 | public abstract void open(UsbDeviceConnection connection) throws IOException;
138 |
139 | @Override
140 | public abstract void close() throws IOException;
141 |
142 | @Override
143 | public abstract int read(final byte[] dest, final int timeoutMillis) throws IOException;
144 |
145 | @Override
146 | public abstract int write(final byte[] src, final int timeoutMillis) throws IOException;
147 |
148 | @Override
149 | public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
150 |
151 | @Override
152 | public abstract boolean getCD() throws IOException;
153 |
154 | @Override
155 | public abstract boolean getCTS() throws IOException;
156 |
157 | @Override
158 | public abstract boolean getDSR() throws IOException;
159 |
160 | @Override
161 | public abstract boolean getDTR() throws IOException;
162 |
163 | @Override
164 | public abstract void setDTR(boolean value) throws IOException;
165 |
166 | @Override
167 | public abstract boolean getRI() throws IOException;
168 |
169 | @Override
170 | public abstract boolean getRTS() throws IOException;
171 |
172 | @Override
173 | public abstract void setRTS(boolean value) throws IOException;
174 |
175 | }
176 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/ProbeTable.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | import android.util.Pair;
25 |
26 | import java.lang.reflect.InvocationTargetException;
27 | import java.lang.reflect.Method;
28 | import java.util.LinkedHashMap;
29 | import java.util.Map;
30 |
31 | /**
32 | * Maps (vendor id, product id) pairs to the corresponding serial driver.
33 | *
34 | * @author mike wakerly (opensource@hoho.com)
35 | */
36 | public class ProbeTable {
37 |
38 | private final Map, Class extends UsbSerialDriver>> mProbeTable =new LinkedHashMap, Class extends UsbSerialDriver>>();
39 |
40 | public Map, Class extends UsbSerialDriver>> getProbeTable(){
41 | return mProbeTable;
42 | }
43 | /**
44 | * Adds or updates a (vendor, product) pair in the table.
45 | *
46 | * @param vendorId the USB vendor id
47 | * @param productId the USB product id
48 | * @param driverClass the driver class responsible for this pair
49 | * @return {@code this}, for chaining
50 | */
51 | public ProbeTable addProduct(int vendorId, int productId, Class extends UsbSerialDriver> driverClass) {
52 | mProbeTable.put(Pair.create(vendorId, productId), driverClass);
53 | return this;
54 | }
55 |
56 | /**
57 | * Internal method to add all supported products from
58 | * {@code getSupportedProducts} static method.
59 | *
60 | * @param driverClass
61 | * @return
62 | */
63 | @SuppressWarnings("unchecked")
64 | ProbeTable addDriver(Class extends UsbSerialDriver> driverClass) {
65 | final Method method;
66 | try {
67 | method = driverClass.getMethod("getSupportedDevices");
68 | } catch (SecurityException e) {
69 | throw new RuntimeException(e);
70 | } catch (NoSuchMethodException e) {
71 | throw new RuntimeException(e);
72 | }
73 | final Map devices;
74 | try {
75 | devices = (Map) method.invoke(null);
76 | } catch (IllegalArgumentException e) {
77 | throw new RuntimeException(e);
78 | } catch (IllegalAccessException e) {
79 | throw new RuntimeException(e);
80 | } catch (InvocationTargetException e) {
81 | throw new RuntimeException(e);
82 | }
83 | for (Map.Entry entry : devices.entrySet()) {
84 | final int vendorId = entry.getKey().intValue();
85 | for (int productId : entry.getValue()) {
86 | addProduct(vendorId, productId, driverClass);
87 | }
88 | }
89 |
90 | return this;
91 | }
92 |
93 | /**
94 | * Returns the driver for the given (vendor, product) pair, or {@code null}
95 | * if no match.
96 | *
97 | * @param vendorId the USB vendor id
98 | * @param productId the USB product id
99 | * @return the driver class matching this pair, or {@code null}
100 | */
101 | public Class extends UsbSerialDriver> findDriver(int vendorId, int productId) {
102 | final Pair pair = Pair.create(vendorId, productId);
103 | return mProbeTable.get(pair);
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/UsbId.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | /**
25 | * Registry of USB vendor/product ID constants.
26 | *
27 | * Culled from various sources; see
28 | * usb.ids for one listing.
29 | *
30 | * @author mike wakerly (opensource@hoho.com)
31 | */
32 | public final class UsbId {
33 |
34 | public static final int VENDOR_FTDI = 0x0403;
35 | public static final int FTDI_FT232R = 0x6001;
36 | public static final int FTDI_FT231X = 0x6015;
37 |
38 | public static final int VENDOR_ATMEL = 0x03EB;
39 | public static final int ATMEL_LUFA_CDC_DEMO_APP = 0x2044;
40 |
41 | public static final int VENDOR_ARDUINO = 0x2341;
42 | public static final int ARDUINO_UNO = 0x0001;
43 | public static final int ARDUINO_MEGA_2560 = 0x0010;
44 | public static final int ARDUINO_SERIAL_ADAPTER = 0x003b;
45 | public static final int ARDUINO_MEGA_ADK = 0x003f;
46 | public static final int ARDUINO_MEGA_2560_R3 = 0x0042;
47 | public static final int ARDUINO_UNO_R3 = 0x0043;
48 | public static final int ARDUINO_MEGA_ADK_R3 = 0x0044;
49 | public static final int ARDUINO_SERIAL_ADAPTER_R3 = 0x0044;
50 | public static final int ARDUINO_LEONARDO = 0x8036;
51 | public static final int ARDUINO_MICRO = 0x8037;
52 |
53 | public static final int VENDOR_VAN_OOIJEN_TECH = 0x16c0;
54 | public static final int VAN_OOIJEN_TECH_TEENSYDUINO_SERIAL = 0x0483;
55 | public static final int VENDOR_VAN_COM3 = 0x5740;
56 |
57 | public static final int VENDOR_LEAFLABS = 0x1eaf;
58 | public static final int LEAFLABS_MAPLE = 0x0004;
59 |
60 | public static final int VENDOR_SILABS = 0x10c4;
61 | public static final int SILABS_CP2102 = 0xea60;
62 | public static final int SILABS_CP2105 = 0xea70;
63 | public static final int SILABS_CP2108 = 0xea71;
64 | public static final int SILABS_CP2110 = 0xea80;
65 |
66 | public static final int VENDOR_PROLIFIC = 0x067b;
67 | public static final int PROLIFIC_PL2303 = 0x2303;
68 |
69 | public static final int VENDOR_QINHENG = 0x1a86;
70 | public static final int QINHENG_HL340 = 0x7523;
71 | public static final int QINHENG_CH341 = 0x5523;
72 |
73 | private UsbId() {
74 | throw new IllegalAccessError("Non-instantiable class.");
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/UsbSerialDriver.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | import android.hardware.usb.UsbDevice;
25 |
26 | import com.hd.serialport.config.UsbPortDeviceType;
27 |
28 | import java.util.List;
29 |
30 | /**
31 | *
32 | * @author mike wakerly (opensource@hoho.com)
33 | */
34 | public interface UsbSerialDriver {
35 |
36 | /**
37 | * Returns the raw {@link UsbDevice} backing this port.
38 | *
39 | * @return the device
40 | */
41 | public UsbDevice getDevice();
42 |
43 | /**
44 | * Returns all available ports for this device. This list must have at least one entry.
45 | *
46 | * @return the ports
47 | */
48 | public List getPorts();
49 |
50 | /**
51 | * @return usb port device type {@link UsbPortDeviceType}
52 | */
53 | public UsbPortDeviceType getDeviceType();
54 | }
55 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/UsbSerialPort.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | import android.hardware.usb.UsbDeviceConnection;
25 | import android.hardware.usb.UsbManager;
26 |
27 | import java.io.IOException;
28 |
29 | /**
30 | * Interface for a single serial port.
31 | *
32 | * @author mike wakerly (opensource@hoho.com)
33 | */
34 | public interface UsbSerialPort {
35 |
36 | /** 5 data bits. */
37 | public static final int DATABITS_5 = 5;
38 |
39 | /** 6 data bits. */
40 | public static final int DATABITS_6 = 6;
41 |
42 | /** 7 data bits. */
43 | public static final int DATABITS_7 = 7;
44 |
45 | /** 8 data bits. */
46 | public static final int DATABITS_8 = 8;
47 |
48 | /** No flow control. */
49 | public static final int FLOWCONTROL_NONE = 0;
50 |
51 | /** RTS/CTS input flow control. */
52 | public static final int FLOWCONTROL_RTSCTS_IN = 1;
53 |
54 | /** RTS/CTS output flow control. */
55 | public static final int FLOWCONTROL_RTSCTS_OUT = 2;
56 |
57 | /** XON/XOFF input flow control. */
58 | public static final int FLOWCONTROL_XONXOFF_IN = 4;
59 |
60 | /** XON/XOFF output flow control. */
61 | public static final int FLOWCONTROL_XONXOFF_OUT = 8;
62 |
63 | /** No parity. */
64 | public static final int PARITY_NONE = 0;
65 |
66 | /** Odd parity. */
67 | public static final int PARITY_ODD = 1;
68 |
69 | /** Even parity. */
70 | public static final int PARITY_EVEN = 2;
71 |
72 | /** Mark parity. */
73 | public static final int PARITY_MARK = 3;
74 |
75 | /** Space parity. */
76 | public static final int PARITY_SPACE = 4;
77 |
78 | /** 1 stop bit. */
79 | public static final int STOPBITS_1 = 1;
80 |
81 | /** 1.5 stop bits. */
82 | public static final int STOPBITS_1_5 = 3;
83 |
84 | /** 2 stop bits. */
85 | public static final int STOPBITS_2 = 2;
86 |
87 | public UsbSerialDriver getDriver();
88 |
89 | /**
90 | * Port number within driver.
91 | */
92 | public int getPortNumber();
93 |
94 | public UsbDeviceConnection getUsbDeviceConnection();
95 |
96 | /**
97 | * The serial number of the underlying UsbDeviceConnection, or {@code null}.
98 | */
99 | public String getSerial();
100 |
101 | /**
102 | * Opens and initializes the port. Upon success, caller must ensure that
103 | * {@link #close()} is eventually called.
104 | *
105 | * @param connection an open device connection, acquired with
106 | * {@link UsbManager#openDevice(android.hardware.usb.UsbDevice)}
107 | * @throws IOException on error opening or initializing the port.
108 | */
109 | public void open(UsbDeviceConnection connection) throws IOException;
110 |
111 | /**
112 | * Closes the port.
113 | *
114 | * @throws IOException on error closing the port.
115 | */
116 | public void close() throws IOException;
117 |
118 | /**
119 | * Reads as many bytes as possible into the destination buffer.
120 | *
121 | * @param dest the destination byte buffer
122 | * @param timeoutMillis the timeout for reading
123 | * @return the actual number of bytes read
124 | * @throws IOException if an error occurred during reading
125 | */
126 | public int read(final byte[] dest, final int timeoutMillis) throws IOException;
127 |
128 | /**
129 | * Writes as many bytes as possible from the source buffer.
130 | *
131 | * @param src the source byte buffer
132 | * @param timeoutMillis the timeout for writing
133 | * @return the actual number of bytes written
134 | * @throws IOException if an error occurred during writing
135 | */
136 | public int write(final byte[] src, final int timeoutMillis) throws IOException;
137 |
138 | /**
139 | * Sets various serial port parameters.
140 | *
141 | * @param baudRate baud rate as an integer, for example {@code 115200}.
142 | * @param dataBits one of {@link #DATABITS_5}, {@link #DATABITS_6},
143 | * {@link #DATABITS_7}, or {@link #DATABITS_8}.
144 | * @param stopBits one of {@link #STOPBITS_1}, {@link #STOPBITS_1_5}, or
145 | * {@link #STOPBITS_2}.
146 | * @param parity one of {@link #PARITY_NONE}, {@link #PARITY_ODD},
147 | * {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or
148 | * {@link #PARITY_SPACE}.
149 | * @throws IOException on error setting the port parameters
150 | */
151 | public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
152 |
153 | /**
154 | * Gets the CD (Carrier Detect) bit from the underlying UART.
155 | *
156 | * @return the current state, or {@code false} if not supported.
157 | * @throws IOException if an error occurred during reading
158 | */
159 | public boolean getCD() throws IOException;
160 |
161 | /**
162 | * Gets the CTS (Clear To Send) bit from the underlying UART.
163 | *
164 | * @return the current state, or {@code false} if not supported.
165 | * @throws IOException if an error occurred during reading
166 | */
167 | public boolean getCTS() throws IOException;
168 |
169 | /**
170 | * Gets the DSR (Data Set Ready) bit from the underlying UART.
171 | *
172 | * @return the current state, or {@code false} if not supported.
173 | * @throws IOException if an error occurred during reading
174 | */
175 | public boolean getDSR() throws IOException;
176 |
177 | /**
178 | * Gets the DTR (Data Terminal Ready) bit from the underlying UART.
179 | *
180 | * @return the current state, or {@code false} if not supported.
181 | * @throws IOException if an error occurred during reading
182 | */
183 | public boolean getDTR() throws IOException;
184 |
185 | /**
186 | * Sets the DTR (Data Terminal Ready) bit on the underlying UART, if
187 | * supported.
188 | *
189 | * @param value the value to set
190 | * @throws IOException if an error occurred during writing
191 | */
192 | public void setDTR(boolean value) throws IOException;
193 |
194 | /**
195 | * Gets the RI (Ring Indicator) bit from the underlying UART.
196 | *
197 | * @return the current state, or {@code false} if not supported.
198 | * @throws IOException if an error occurred during reading
199 | */
200 | public boolean getRI() throws IOException;
201 |
202 | /**
203 | * Gets the RTS (Request To Send) bit from the underlying UART.
204 | *
205 | * @return the current state, or {@code false} if not supported.
206 | * @throws IOException if an error occurred during reading
207 | */
208 | public boolean getRTS() throws IOException;
209 |
210 | /**
211 | * Sets the RTS (Request To Send) bit on the underlying UART, if
212 | * supported.
213 | *
214 | * @param value the value to set
215 | * @throws IOException if an error occurred during writing
216 | */
217 | public void setRTS(boolean value) throws IOException;
218 |
219 | /**
220 | * Flush non-transmitted output data and / or non-read input data
221 | * @param flushRX {@code true} to flush non-transmitted output data
222 | * @param flushTX {@code true} to flush non-read input data
223 | * @return {@code true} if the operation was successful, or
224 | * {@code false} if the operation is not supported by the driver or device
225 | * @throws IOException if an error occurred during flush
226 | */
227 | public boolean purgeHwBuffers(boolean flushRX, boolean flushTX) throws IOException;
228 |
229 | }
230 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/UsbSerialProber.java:
--------------------------------------------------------------------------------
1 | /* Copyright 2011-2013 Google Inc.
2 | * Copyright 2013 mike wakerly
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 | * USA.
18 | *
19 | * Project home page: https://github.com/mik3y/usb-serial-for-android
20 | */
21 |
22 | package com.hd.serialport.usb_driver;
23 |
24 | import android.hardware.usb.UsbDevice;
25 | import android.hardware.usb.UsbManager;
26 | import android.support.annotation.Keep;
27 | import android.text.TextUtils;
28 | import android.util.Pair;
29 |
30 | import com.hd.serialport.config.DriversType;
31 | import com.hd.serialport.usb_driver.extend.UsbExtendDriver;
32 |
33 | import java.lang.reflect.Constructor;
34 | import java.util.ArrayList;
35 | import java.util.Arrays;
36 | import java.util.HashMap;
37 | import java.util.Iterator;
38 | import java.util.List;
39 | import java.util.Map;
40 |
41 | /**
42 | * @author mike wakerly (opensource@hoho.com)
43 | */
44 | public class UsbSerialProber {
45 |
46 | private final ProbeTable mProbeTable;
47 |
48 | public UsbSerialProber(ProbeTable probeTable) {
49 | mProbeTable = probeTable;
50 | }
51 |
52 | public static UsbSerialProber getDefaultProber() {
53 | return new UsbSerialProber(getDefaultProbeTable());
54 | }
55 |
56 | private static ProbeTable getDefaultProbeTable() {
57 | final ProbeTable probeTable = new ProbeTable();
58 | final List>> drivers = new UsbExtendDriver().getExtendDrivers();
59 |
60 | List defaultTypeList = Arrays.asList(DriversType.USB_CDC_ACM, DriversType.USB_CP21xx,//
61 | DriversType.USB_FTD, DriversType.USB_PL2303,//
62 | DriversType.USB_CH34xx);
63 |
64 | List> defaultDriverList = Arrays.asList(CdcAcmSerialDriver.class, Cp21xxSerialDriver.class,//
65 | FtdiSerialDriver.class, ProlificSerialDriver.class,//
66 | Ch34xSerialDriver.class);
67 | if(null != drivers && !drivers.isEmpty()) {
68 | int index;
69 | for (Pair> pair : drivers) {
70 | index = defaultTypeList.indexOf(pair.first);
71 | if (index > 0) {
72 | defaultDriverList.remove(index);
73 | }
74 | probeTable.addDriver(pair.second);
75 | }
76 | }
77 | for (Class extends CommonUsbSerialDriver> driver : defaultDriverList) {
78 | probeTable.addDriver(driver);
79 | }
80 | return probeTable;
81 | }
82 |
83 | /**
84 | * Finds and builds all possible {@link UsbSerialDriver UsbSerialDrivers}
85 | * from the currently-attached {@link UsbDevice} hierarchy. This method does
86 | * not require permission from the Android USB system, since it does not
87 | * open any of the devices.
88 | *
89 | * @param usbManager
90 | *
91 | * @return a list, possibly empty, of all compatible drivers
92 | */
93 | @Keep
94 | public List findAllDrivers(final UsbManager usbManager) {
95 | final List result = new ArrayList();
96 | HashMap deviceList = usbManager.getDeviceList();
97 | Iterator deviceIterator = deviceList.values().iterator();
98 | while (deviceIterator.hasNext()) {
99 | UsbDevice usbDevice = deviceIterator.next();
100 | UsbSerialDriver driver = probeDevice(usbDevice);
101 | if (driver != null) {
102 | result.add(driver);
103 | }
104 | }
105 | return result;
106 | }
107 |
108 | /**
109 | * Probes a single device for a compatible driver.
110 | *
111 | * @param usbDevice the usb device to probe
112 | *
113 | * @return a new {@link UsbSerialDriver} compatible with this device, or
114 | * {@code null} if none available.
115 | */
116 | @Keep
117 | public UsbSerialDriver probeDevice(final UsbDevice usbDevice) {
118 | final int vendorId = usbDevice.getVendorId();
119 | final int productId = usbDevice.getProductId();
120 | final Class extends UsbSerialDriver> driverClass = mProbeTable.findDriver(vendorId, productId);
121 | return getUsbSerialDriver(usbDevice, driverClass);
122 | }
123 |
124 | @Keep
125 | public UsbSerialDriver probeDevice(final UsbDevice usbDevice, String driverName) {
126 | if(TextUtils.isEmpty(driverName))return probeDevice(usbDevice);
127 | UsbSerialDriver driver = null;
128 | Map, Class extends UsbSerialDriver>> table = mProbeTable.getProbeTable();
129 | for (Map.Entry, Class extends UsbSerialDriver>> entry : table.entrySet()) {
130 | driver = getUsbSerialDriver(usbDevice, entry.getValue());
131 | if (null != driver && driverName.equals(driver.getDeviceType().getValue())) {
132 | return driver;
133 | }
134 | }
135 | return driver;
136 | }
137 |
138 | private UsbSerialDriver getUsbSerialDriver(UsbDevice usbDevice, Class extends UsbSerialDriver> driverClass) {
139 | if (driverClass != null) {
140 | final UsbSerialDriver driver;
141 | try {
142 | final Constructor extends UsbSerialDriver> constructor = driverClass.getConstructor(UsbDevice.class);
143 | driver = constructor.newInstance(usbDevice);
144 | } catch (Exception e) {
145 | throw new RuntimeException(e);
146 | }
147 | return driver;
148 | }
149 | return null;
150 | }
151 |
152 | public UsbSerialDriver convertDriver(UsbDevice usbDevice, String driverName) {
153 | UsbSerialDriver driver = probeDevice(usbDevice);
154 | if (driver == null) {
155 | driver = probeDevice(usbDevice, driverName);
156 | }
157 | if (driver == null)
158 | throw new NullPointerException("unknown usb device type name : " + driverName);
159 | return driver;
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/usb_driver/extend/UsbExtendDriver.java:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.usb_driver.extend;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.util.Pair;
5 |
6 | import com.hd.serialport.usb_driver.UsbSerialDriver;
7 |
8 | import java.lang.reflect.Method;
9 | import java.util.List;
10 | import java.util.function.Consumer;
11 |
12 | /**
13 | * Created by hd on 2019-08-26 .
14 | *
15 | * usb驱动扩展
16 | *
17 | * 1.扩展的serial driver 需要继承 {@link com.hd.serialport.usb_driver.CommonUsbSerialDriver}
18 | * 2.扩展的serial port 需要继承 {@link com.hd.serialport.usb_driver.CommonUsbSerialPort}
19 | * 3.扩展的serial driver类方法 getDeviceType,理论上需要写成 :
20 | * ```
21 | * @Override public String setDriverName() {
22 | * return "custom value";
23 | * }
24 | * ```
25 | * 4.扩展的serial port 类需要添加`getSupportedDevices`方法 ,如下:
26 | * ```
27 | * public static Map getSupportedDevices() {
28 | * final Map supportedDevices = new LinkedHashMap();
29 | * supportedDevices.put(UsbId.VENDOR_QINHENG, new int[]{UsbId.QINHENG_HL340, UsbId.QINHENG_CH341});
30 | * return supportedDevices;
31 | * }
32 | * ```
33 | */
34 | @SuppressWarnings("ALL")
35 | public class UsbExtendDriver {
36 |
37 | private static List>> extendDrivers;
38 |
39 | public UsbExtendDriver() { }
40 |
41 | private UsbExtendDriver(Extender extender) {
42 | extendDrivers = extender.drivers;
43 | }
44 |
45 | private void clearExtendDrivers(){
46 | extendDrivers.clear();
47 | extendDrivers = null;
48 | }
49 |
50 | public List>> getExtendDrivers(){
51 | return extendDrivers;
52 | }
53 |
54 | public static class Extender {
55 |
56 | public Extender Extender() { return new Extender();}
57 |
58 | private List>> drivers;
59 |
60 | /**
61 | * e.g.
62 | * List list = new ArrayList();
63 | * list.add(new Pair("custom1 value",CustomDriver1.class));
64 | * list.add(new Pair("custom2 value",CustomDriver2.class));
65 | * list.add(new Pair("custom3 value",CustomDriver3.class));
66 | * setDrivers(list);
67 | */
68 | public Extender setDrivers(@NonNull List>> drivers) {
69 | this.drivers = drivers;
70 | return this;
71 | }
72 |
73 | public void clearExtendDrivers(){
74 | new UsbExtendDriver().clearExtendDrivers();
75 | }
76 |
77 | public void extend() {
78 | drivers.forEach(new Consumer>>() {
79 | @Override
80 | public void accept(Pair> driver) {
81 | if (driver.first.isEmpty()) throw new NullPointerException("custom extended usb driver name must be not null !!");
82 | try {
83 | Method method = driver.second.getMethod("getSupportedDevices");
84 | } catch (Exception e) {
85 | throw new RuntimeException("custom extended usb driver must add a method named 'getSupportedDevices' !");
86 | }
87 | }
88 | });
89 | new UsbExtendDriver(this);
90 | }
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/utils/HexDump.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2006 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.hd.serialport.utils;
18 |
19 | import java.util.Arrays;
20 |
21 | /**
22 | * Clone of Android's HexDump class, for use in debugging. Cosmetic changes only.
23 | */
24 | public class HexDump {
25 | private final static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
26 | private final static String[] binaryArray = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
27 | private final static String hexStr= Arrays.toString(HEX_DIGITS);
28 | public static String dumpHexString(byte[] array) {
29 | return dumpHexString(array, 0, array.length);
30 | }
31 |
32 | public static String dumpHexString(byte[] array, int offset, int length) {
33 | StringBuilder result = new StringBuilder();
34 | byte[] line = new byte[16];
35 | int lineIndex = 0;
36 | result.append("\n0x");
37 | result.append(toHexString(offset));
38 |
39 | for (int i = offset; i < offset + length; i++) {
40 | if (lineIndex == 16) {
41 | result.append(" ");
42 |
43 | for (int j = 0; j < 16; j++) {
44 | if (line[j] > ' ' && line[j] < '~') {
45 | result.append(new String(line, j, 1));
46 | } else {
47 | result.append(".");
48 | }
49 | }
50 |
51 | result.append("\n0x");
52 | result.append(toHexString(i));
53 | lineIndex = 0;
54 | }
55 |
56 | byte b = array[i];
57 | result.append(" ");
58 | result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
59 | result.append(HEX_DIGITS[b & 0x0F]);
60 |
61 | line[lineIndex++] = b;
62 | }
63 |
64 | if (lineIndex != 16) {
65 | int count = (16 - lineIndex) * 3;
66 | count++;
67 | for (int i = 0; i < count; i++) {
68 | result.append(" ");
69 | }
70 |
71 | for (int i = 0; i < lineIndex; i++) {
72 | if (line[i] > ' ' && line[i] < '~') {
73 | result.append(new String(line, i, 1));
74 | } else {
75 | result.append(".");
76 | }
77 | }
78 | }
79 |
80 | return result.toString();
81 | }
82 |
83 | public static String hextoString(byte[] array) {
84 | return hextoString(array, 0, array.length);
85 | }
86 |
87 | public static String hextoString(byte[] array, int offset, int length) {
88 | StringBuilder result = new StringBuilder();
89 | for (int i = offset; i < offset + length; i++) {
90 | result.append(HEX_DIGITS[array[i] & 0x0F]);
91 | }
92 | return result.toString();
93 | }
94 |
95 | /**
96 | * 字节数组转为普通字符串(ASCII对应的字符)
97 | *
98 | * @param bytearray byte[]
99 | *
100 | * @return String
101 | */
102 | public static String bytetoString(byte[] bytearray) {
103 | String result = "";
104 | char temp;
105 |
106 | int length = bytearray.length;
107 | for (byte aBytearray : bytearray) {
108 | temp = (char) aBytearray;
109 | result += temp;
110 | }
111 | return result;
112 | }
113 |
114 | public static String toHexString(byte b) {
115 | return toHexString(toByteArray(b));
116 | }
117 |
118 | public static String toHexString(byte[] array) {
119 | return toHexString(array, 0, array.length);
120 | }
121 |
122 | public static String toHexString(byte[] array, int offset, int length) {
123 | char[] buf = new char[length * 2];
124 |
125 | int bufIndex = 0;
126 | for (int i = offset; i < offset + length; i++) {
127 | byte b = array[i];
128 | buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
129 | buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
130 | }
131 |
132 | return new String(buf);
133 | }
134 |
135 | public static String toHexString(int i) {
136 | return toHexString(toByteArray(i));
137 | }
138 |
139 | public static String toHexString(short i) {
140 | return toHexString(toByteArray(i));
141 | }
142 |
143 | public static byte[] toByteArray(byte b) {
144 | byte[] array = new byte[1];
145 | array[0] = b;
146 | return array;
147 | }
148 |
149 | public static byte[] toByteArray(int i) {
150 | byte[] array = new byte[4];
151 | array[3] = (byte) (i & 0xFF);
152 | array[2] = (byte) ((i >> 8) & 0xFF);
153 | array[1] = (byte) ((i >> 16) & 0xFF);
154 | array[0] = (byte) ((i >> 24) & 0xFF);
155 |
156 | return array;
157 | }
158 |
159 | public static byte[] toByteArray(short i) {
160 | byte[] array = new byte[2];
161 | array[1] = (byte) (i & 0xFF);
162 | array[0] = (byte) ((i >> 8) & 0xFF);
163 |
164 | return array;
165 | }
166 |
167 | private static int toByte(char c) {
168 | if (c >= '0' && c <= '9')
169 | return (c - '0');
170 | if (c >= 'A' && c <= 'F')
171 | return (c - 'A' + 10);
172 | if (c >= 'a' && c <= 'f')
173 | return (c - 'a' + 10);
174 |
175 | throw new RuntimeException("Invalid hex char '" + c + "'");
176 | }
177 |
178 | public static byte[] hexStringToByteArray(String hexString) {
179 | int length = hexString.length();
180 | byte[] buffer = new byte[length / 2];
181 | for (int i = 0; i < length; i += 2) {
182 | buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));
183 | }
184 | return buffer;
185 | }
186 |
187 | /**
188 | * @param hexString
189 | *
190 | * @return 将十六进制转换为二进制字符串 16-2
191 | */
192 | public static String hexStrBinaryStr(String hexString) {
193 | return bytes2BinStr(hexStrToBinary(hexString));
194 | }
195 |
196 | /**
197 | * @param hexString
198 | *
199 | * @return 将十六进制转换为二进制字节数组 16-2
200 | */
201 | public static byte[] hexStrToBinary(String hexString){
202 | //hexString的长度对2取整,作为bytes的长度
203 | int len = hexString.length() / 2;
204 | byte[] bytes = new byte[len];
205 | byte high = 0;//字节高四位
206 | byte low = 0;//字节低四位
207 | for (int i = 0; i < len; i++) {
208 | //右移四位得到高位
209 | high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4);
210 | low = (byte)hexStr.indexOf(hexString.charAt(2*i+1));
211 | bytes[i] = (byte) (high | low);//高地位做或运算
212 | }
213 | return bytes;
214 | }
215 |
216 |
217 | /**
218 | * @return 二进制数组转换为二进制字符串 2-2
219 | */
220 | public static String bytes2BinStr(byte[] bArray) {
221 | String outStr = "";
222 | int pos = 0;
223 | for (byte b : bArray) {
224 | //高四位
225 | pos = (b & 0xF0) >> 4;
226 | outStr += binaryArray[pos];
227 | //低四位
228 | pos = b & 0x0F;
229 | outStr += binaryArray[pos];
230 | }
231 | return outStr;
232 | }
233 |
234 | /**
235 | * 将传入的十六进制字符串按位两两异或 最终得到异或校验和
236 | *
237 | * @param hexStr
238 | * @return
239 | */
240 | public static String checkSum(String hexStr) {
241 | if (hexStr.length() < 2) {
242 | // 保证必须有one byte十六进制字符
243 | return null;
244 | }
245 | String x = null;
246 | for (int i = 0; i < hexStr.length() / 2 - 1; i++) {
247 | if (i == 0) {
248 | x = hexStr.substring(i, i + 2);
249 | }
250 | String y = hexStr.substring(i * 2 + 2, i * 2 + 4);
251 | x = xor(x, y);
252 | }
253 | return x;
254 | }
255 |
256 | /**
257 | * 将传入的两个one byte十六进制字符异或
258 | *
259 | * @param strHex_X
260 | * @param strHex_Y
261 | * @return
262 | */
263 | private static String xor(String strHex_X, String strHex_Y) {
264 | // 将x、y转成二进制形式
265 | String anotherBinary = Integer.toBinaryString(Integer.valueOf(strHex_X,
266 | 16));
267 | String thisBinary = Integer.toBinaryString(Integer
268 | .valueOf(strHex_Y, 16));
269 | String result = "";
270 | // 判断是否为8位二进制,否则左补零
271 | if (anotherBinary.length() != 8) {
272 | for (int i = anotherBinary.length(); i < 8; i++) {
273 | anotherBinary = "0" + anotherBinary;
274 | }
275 | }
276 | if (thisBinary.length() != 8) {
277 | for (int i = thisBinary.length(); i < 8; i++) {
278 | thisBinary = "0" + thisBinary;
279 | }
280 | }
281 | // 异或运算
282 | for (int i = 0; i < anotherBinary.length(); i++) {
283 | // 如果相同位置数相同,则补0,否则补1
284 | if (thisBinary.charAt(i) == anotherBinary.charAt(i))
285 | result += "0";
286 | else {
287 | result += "1";
288 | }
289 | }
290 | return Integer.toHexString(Integer.parseInt(result, 2));
291 | }
292 |
293 | /**
294 | * 字符串的16进制累加和
295 | */
296 | public static String makeChecksum(String data) {
297 | if (data == null || data.equals("")) {
298 | return "";
299 | }
300 | int total = 0;
301 | int len = data.length();
302 | int num = 0;
303 | while (num < len) {
304 | String s = data.substring(num, num + 2);
305 | System.out.println(s);
306 | total += Integer.parseInt(s, 16);
307 | num = num + 2;
308 | }
309 | //用256求余最大是255,即16进制的FF
310 | int mod = total % 256;
311 | String hex = Integer.toHexString(mod);
312 | len = hex.length();
313 | // 如果不够校验位的长度,补0,这里用的是两位校验
314 | if (len < 2) {
315 | hex = "0" + hex;
316 | }
317 | return hex;
318 | }
319 | }
320 |
--------------------------------------------------------------------------------
/usbserialport/src/main/java/com/hd/serialport/utils/L.kt:
--------------------------------------------------------------------------------
1 | package com.hd.serialport.utils
2 |
3 | import android.util.Log
4 | import com.hd.serialport.BuildConfig
5 |
6 | /**
7 | * Created by hd on 2017/5/8.
8 | *
9 | */
10 | object L {
11 | var allowLog = BuildConfig.DEBUG
12 | private val TAG = "usb-serial-port"
13 | fun i(i: String) {
14 | i(TAG, i)
15 | }
16 |
17 | fun d(d: String) {
18 | d(TAG, d)
19 | }
20 |
21 | fun w(w: String) {
22 | w(TAG, w)
23 | }
24 |
25 | fun e(e: String) {
26 | e(TAG, e)
27 | }
28 |
29 | fun i(tag: String?, i: String) {
30 | if (allowLog)
31 | Log.i(tag ?: TAG, i)
32 | }
33 |
34 | fun d(tag: String?, d: String) {
35 | if (allowLog)
36 | Log.d(tag ?: TAG, d)
37 | }
38 |
39 | fun w(tag: String?, w: String) {
40 | if (allowLog)
41 | Log.w(tag ?: TAG, w)
42 | }
43 |
44 | fun e(tag: String?, e: String) {
45 | if (allowLog)
46 | Log.e(tag ?: TAG, e)
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/arm64-v8a/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/arm64-v8a/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/armeabi-v7a/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/armeabi-v7a/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/armeabi/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/armeabi/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/mips/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/mips/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/mips64/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/mips64/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/x86/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/x86/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/jniLibs/x86_64/libserial_port.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HelloHuDi/usb-with-serial-port/68f3fef3782f1c98681080ee062f04f8f6d5bf7a/usbserialport/src/main/jniLibs/x86_64/libserial_port.so
--------------------------------------------------------------------------------
/usbserialport/src/main/res/values-zh-rCN/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | usbSerialPort
3 | 检测到当前系统存在usb模块Bug,请与厂商联系
4 | 查找设备失败
5 | 打开设备失败
6 | 本次测量失败
7 | 权限请求失败
8 | 参数错误,串口打开失败
9 | 当前串口没有读写权限
10 | 串口打开失败
11 |
12 |
--------------------------------------------------------------------------------
/usbserialport/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | usbSerialPort
3 | USB module Bug is detected in the current system, please contact the vendor
4 | Lookup device failure
5 | Open device failure
6 | Failure of this measurement
7 | Permission request failure
8 | Error of parameter, failure to open serial port
9 | No read and write permissions in the current serial port
10 | Failure to open serial port
11 |
12 |
--------------------------------------------------------------------------------