Skip to content

Commit

Permalink
更新配置串口数据获取间隔,改为可动态配置
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxudong committed Apr 25, 2020
1 parent 32ccf06 commit 1ba354f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions serial/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 6
versionName "1.2.1"
versionCode 7
versionName "1.2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
19 changes: 18 additions & 1 deletion serial/src/main/java/org/sheedon/serial/SerialClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class SerialClient implements SerialRealCallback, RealClient,

checkNotNull(builder.path, "path is null");
try {
port = new SerialPort(builder.path, builder.baudRate, builder.flags, this, this.dispatcher);
port = new SerialPort(builder.path, builder.baudRate, builder.flags, builder.threadSleepTime, this, this.dispatcher);
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
Expand Down Expand Up @@ -113,6 +113,8 @@ public static final class Builder {

String name;

int threadSleepTime;

int messageTimeout;

SerialRealCallback callback;
Expand All @@ -123,6 +125,7 @@ public static final class Builder {
public Builder() {
dispatcher = new Dispatcher();
baudRate = 9600;
threadSleepTime = 1000;
flags = 0;
messageTimeout = 5;
name = String.format("%d", this.getClass().hashCode());
Expand All @@ -145,6 +148,20 @@ public Builder path(@NonNull String path) {
return this;
}

/**
* 设置线程数据获取间隔
* 建议大于100毫秒
*
* @param interval 间隔
*/
public Builder threadSleepTime(int interval) {
if (interval < 50)
return this;

this.threadSleepTime = interval;
return this;
}

/**
* 设置串口路径
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ public class SerialPort {

private Thread thread;

private int interval;

/**
* 构建 创建客户端
*
* @param path 路径
* @param baudRate 波特率
* @param flags 标志位
* @param interval 线程间隔
* @param callback 反馈
*/
public SerialPort(String path, int baudRate, int flags,
public SerialPort(String path, int baudRate, int flags, int interval,
SerialRealCallback callback, Dispatcher dispatcher)
throws IOException, SecurityException {
Util.checkNotNull(path, "path is null");
Util.checkNotNull(dispatcher, "dispatcher is null");
this.interval = interval;
converter = Util.checkNotNull(dispatcher.checkDataConverter(), "converter is null");
SerialRunnable runnable = new SerialRunnable(path);
this.callback = callback;
Expand Down Expand Up @@ -81,7 +85,7 @@ protected void execute() {
while (!Thread.currentThread().isInterrupted()) {
readThread();
try {
Thread.sleep(1000);
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 1ba354f

Please sign in to comment.