Skip to content

Commit fc65d18

Browse files
committed
新增可配置数据位、校验位、停止位
参考 https://juejin.im/post/5c010a19e51d456ac27b40fc
1 parent db496a9 commit fc65d18

File tree

20 files changed

+362
-468
lines changed

20 files changed

+362
-468
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# Android-SerialPort-API
22

3-
用这个API写的一个小工具 [Android-SerialPort-Tool](https://github.com/licheedev/Android-SerialPort-Tool)
4-
5-
基于这个API封装的库 [SerialWroker](https://github.com/licheedev/SerialWorker),有分包、粘包疑惑的,可以参考一下里面的DEMO
6-
73
[![](https://jitpack.io/v/licheedev/Android-SerialPort-API.svg)](https://jitpack.io/#licheedev/Android-SerialPort-API)
84

95
**Gradle 引用**
@@ -23,7 +19,7 @@ allprojects {
2319

2420
```
2521
dependencies {
26-
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1'
22+
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:2.0.0'
2723
}
2824
```
2925

@@ -34,3 +30,22 @@ dependencies {
3430
// 可通过此方法修改
3531
SerialPort.setSuPath("/system/xbin/su");
3632
```
33+
34+
**可选配置数据位、校验位、停止位**
35+
36+
实现方式参考
37+
> https://juejin.im/post/5c010a19e51d456ac27b40fc
38+
39+
```java
40+
41+
// 默认8N1(8数据位、无校验位、1停止位)
42+
SerialPort serialPort = SerialPort.newBuilder(path, baudrate).build();
43+
44+
// 7E2(7数据位、偶校验、2停止位)
45+
SerialPort serialPort = SerialPort //
46+
.newBuilder(path, baudrate) // 串口地址地址,波特率
47+
.parity(2) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
48+
.dataBits(7) // 数据位,默认8;可选值为5~8
49+
.stopBits(2) // 停止位,默认1;1:1位停止位;2:2位停止位
50+
.build();
51+
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ ext {
3131
targetSdkVersion = 28
3232

3333
versionCode = 2
34-
versionName = "1.0.1"
34+
versionName = "2.0.0"
3535
}

sample/src/main/java/android/serialport/sample/Application.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/*
22
* Copyright 2009 Cedric Priscal
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
14-
* limitations under the License.
14+
* limitations under the License.
1515
*/
1616

1717
package android.serialport.sample;
1818

1919
import android.content.SharedPreferences;
2020
import android.serialport.SerialPort;
2121
import android.serialport.SerialPortFinder;
22-
import java.io.File;
2322
import java.io.IOException;
2423
import java.security.InvalidParameterException;
2524

@@ -38,13 +37,22 @@ public SerialPort getSerialPort()
3837
String path = sp.getString("DEVICE", "");
3938
int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));
4039

41-
/* Check parameters */
40+
/* Check parameters */
4241
if ((path.length() == 0) || (baudrate == -1)) {
4342
throw new InvalidParameterException();
4443
}
4544

46-
/* Open the serial port */
47-
mSerialPort = new SerialPort(new File(path), baudrate, 0);
45+
/* Open the serial port */
46+
//mSerialPort = new SerialPort(new File(path), baudrate, 0);
47+
48+
SerialPort serialPort = SerialPort //
49+
.newBuilder(path, baudrate) // 串口地址地址,波特率
50+
.parity(2) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
51+
.dataBits(7) // 数据位,默认8;可选值为5~8
52+
.stopBits(2) // 停止位,默认1;1:1位停止位;2:2位停止位
53+
.build();
54+
55+
mSerialPort = serialPort;
4856
}
4957
return mSerialPort;
5058
}

0 commit comments

Comments
 (0)