|
1 | 1 | # Android-SerialPort-API |
| 2 | +[Fork](https://code.google.com/archive/p/android-serialport-api/)自Google开源的Android串口通信Demo,修改成Android Studio项目 |
2 | 3 |
|
3 | | -**Gradle 引用** |
| 4 | +This lib is a [fork](https://code.google.com/archive/p/android-serialport-api/) of the Android serial port communication Demo open sourced by Google. |
4 | 5 |
|
5 | | -添加依赖 |
| 6 | +## Installation & Usage |
| 7 | +**Gradle** |
| 8 | + |
| 9 | +添加依赖: |
| 10 | + |
| 11 | +Add the dependency: |
6 | 12 |
|
7 | 13 | ``` |
8 | 14 | dependencies { |
9 | 15 | implementation 'com.licheedev:android-serialport:2.1.2' |
10 | 16 | } |
11 | 17 | ``` |
12 | 18 |
|
13 | | -**修改`su`路径** |
| 19 | +**Import** |
| 20 | + |
| 21 | +```java |
| 22 | +import android.serialport.SerialPort; |
| 23 | +``` |
| 24 | + |
| 25 | +**`su` path** |
| 26 | + |
| 27 | +In order to read/write to a serial port in Android you'll need `su` binary installed on device (this can be done by rooting the device). Usually Android devices that has the ability to communicate with serial ports have `su` installed on the default path `"/system/bin/su"`. To change this use: |
14 | 28 |
|
15 | 29 | ```java |
16 | 30 | // su默认路径为 "/system/bin/su" |
| 31 | +// The default path of su is "/system/bin/su" |
17 | 32 | // 可通过此方法修改 |
| 33 | +// If the path is different then change it using this |
18 | 34 | SerialPort.setSuPath("/system/xbin/su"); |
19 | 35 | ``` |
20 | 36 |
|
21 | | -**可选配置数据位、校验位、停止位** |
22 | | - |
23 | | -实现方式参考 |
24 | | -> https://juejin.im/post/5c010a19e51d456ac27b40fc |
| 37 | +**Usage** |
25 | 38 |
|
26 | 39 | ```java |
27 | | - |
28 | 40 | // 默认8N1(8数据位、无校验位、1停止位) |
| 41 | +// Default 8N1 (8 data bits, no parity bit, 1 stop bit) |
29 | 42 | SerialPort serialPort = new SerialPort(path, baudrate); |
30 | 43 |
|
31 | | -// 7E2(7数据位、偶校验、2停止位) |
32 | | -SerialPort serialPort = SerialPort // |
33 | | - .newBuilder(path, baudrate) // 串口地址地址,波特率 |
34 | | - .parity(2) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN) |
35 | | - .dataBits(7) // 数据位,默认8;可选值为5~8 |
36 | | - .stopBits(2) // 停止位,默认1;1:1位停止位;2:2位停止位 |
| 44 | +// 可选配置数据位、校验位、停止位 - 7E2(7数据位、偶校验、2停止位) |
| 45 | +// or with builder (with optional configurations) - 7E2 (7 data bits, even parity, 2 stop bits) |
| 46 | +SerialPort serialPort = SerialPort |
| 47 | + .newBuilder(path, baudrate) |
| 48 | +// 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN) |
| 49 | +// Check bit; 0: no check bit (NONE, default); 1: odd check bit (ODD); 2: even check bit (EVEN) |
| 50 | +// .parity(2) |
| 51 | +// 数据位,默认8;可选值为5~8 |
| 52 | +// Data bit, default 8; optional value is 5~8 |
| 53 | +// .dataBits(7) |
| 54 | +// 停止位,默认1;1:1位停止位;2:2位停止位 |
| 55 | +// Stop bit, default 1; 1:1 stop bit; 2: 2 stop bit |
| 56 | +// .stopBits(2) |
37 | 57 | .build(); |
| 58 | + |
| 59 | +// read/write to serial port - needs to be in different thread! |
| 60 | +InputStream in = serialPort.getInputStream(); |
| 61 | +OutputStream out = serialPort.getOutputStream(); |
| 62 | + |
| 63 | +// close |
| 64 | +serialPort.tryClose(); |
38 | 65 | ``` |
| 66 | + |
| 67 | +实现方式参考 |
| 68 | + |
| 69 | +Implementation reference |
| 70 | +1. Check [sample project](https://github.com/licheedev/Android-SerialPort-API/tree/master/sample) |
| 71 | +2. https://juejin.im/post/5c010a19e51d456ac27b40fc |
0 commit comments