From 1ba354fa32aa5bac1329d87df12e5c7215273047 Mon Sep 17 00:00:00 2001 From: sunxudong Date: Sat, 25 Apr 2020 11:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E4=B8=B2?= =?UTF-8?q?=E5=8F=A3=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96=E9=97=B4=E9=9A=94?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E5=8F=AF=E5=8A=A8=E6=80=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serial/build.gradle | 4 ++-- .../java/org/sheedon/serial/SerialClient.java | 19 ++++++++++++++++++- .../sheedon/serial/serialport/SerialPort.java | 8 ++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/serial/build.gradle b/serial/build.gradle index a1a10ce..cb765e6 100644 --- a/serial/build.gradle +++ b/serial/build.gradle @@ -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' diff --git a/serial/src/main/java/org/sheedon/serial/SerialClient.java b/serial/src/main/java/org/sheedon/serial/SerialClient.java index 6369d55..cb39c3c 100644 --- a/serial/src/main/java/org/sheedon/serial/SerialClient.java +++ b/serial/src/main/java/org/sheedon/serial/SerialClient.java @@ -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) { @@ -113,6 +113,8 @@ public static final class Builder { String name; + int threadSleepTime; + int messageTimeout; SerialRealCallback callback; @@ -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()); @@ -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; + } + /** * 设置串口路径 */ diff --git a/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java b/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java index 82703f0..44ed63b 100644 --- a/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java +++ b/serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java @@ -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; @@ -81,7 +85,7 @@ protected void execute() { while (!Thread.currentThread().isInterrupted()) { readThread(); try { - Thread.sleep(1000); + Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); }