Skip to content

Commit

Permalink
新增可配置数据位、校验位、停止位
Browse files Browse the repository at this point in the history
  • Loading branch information
licheedev committed Nov 19, 2019
1 parent db496a9 commit fc65d18
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 468 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Android-SerialPort-API

用这个API写的一个小工具 [Android-SerialPort-Tool](https://github.com/licheedev/Android-SerialPort-Tool)

基于这个API封装的库 [SerialWroker](https://github.com/licheedev/SerialWorker),有分包、粘包疑惑的,可以参考一下里面的DEMO

[![](https://jitpack.io/v/licheedev/Android-SerialPort-API.svg)](https://jitpack.io/#licheedev/Android-SerialPort-API)

**Gradle 引用**
Expand All @@ -23,7 +19,7 @@ allprojects {

```
dependencies {
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1'
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:2.0.0'
}
```

Expand All @@ -34,3 +30,22 @@ dependencies {
// 可通过此方法修改
SerialPort.setSuPath("/system/xbin/su");
```

**可选配置数据位、校验位、停止位**

实现方式参考
> https://juejin.im/post/5c010a19e51d456ac27b40fc
```java

// 默认8N1(8数据位、无校验位、1停止位)
SerialPort serialPort = SerialPort.newBuilder(path, baudrate).build();

// 7E2(7数据位、偶校验、2停止位)
SerialPort serialPort = SerialPort //
.newBuilder(path, baudrate) // 串口地址地址,波特率
.parity(2) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
.dataBits(7) // 数据位,默认8;可选值为5~8
.stopBits(2) // 停止位,默认1;1:1位停止位;2:2位停止位
.build();
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ ext {
targetSdkVersion = 28

versionCode = 2
versionName = "1.0.1"
versionName = "2.0.0"
}
24 changes: 16 additions & 8 deletions sample/src/main/java/android/serialport/sample/Application.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/*
* Copyright 2009 Cedric Priscal
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/

package android.serialport.sample;

import android.content.SharedPreferences;
import android.serialport.SerialPort;
import android.serialport.SerialPortFinder;
import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;

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

/* Check parameters */
/* Check parameters */
if ((path.length() == 0) || (baudrate == -1)) {
throw new InvalidParameterException();
}

/* Open the serial port */
mSerialPort = new SerialPort(new File(path), baudrate, 0);
/* Open the serial port */
//mSerialPort = new SerialPort(new File(path), baudrate, 0);

SerialPort serialPort = SerialPort //
.newBuilder(path, baudrate) // 串口地址地址,波特率
.parity(2) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
.dataBits(7) // 数据位,默认8;可选值为5~8
.stopBits(2) // 停止位,默认1;1:1位停止位;2:2位停止位
.build();

mSerialPort = serialPort;
}
return mSerialPort;
}
Expand Down
Loading

0 comments on commit fc65d18

Please sign in to comment.