-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sunxudong
committed
Apr 26, 2020
1 parent
1ba354f
commit 330fae0
Showing
2 changed files
with
100 additions
and
42 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.sheedon.serial.serialport; | ||
|
||
import org.sheedon.serial.NamedRunnable; | ||
|
||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
/** | ||
* 定时任务保活 | ||
* | ||
* @Author: sheedon | ||
* @Email: [email protected] | ||
* @Date: 2020/4/26 10:34 | ||
*/ | ||
public class SafeThread { | ||
private final static SafeThread INSTANCE = new SafeThread(); | ||
|
||
private Thread thread; | ||
private int interval; | ||
|
||
private Timer pingTimer; | ||
private TimerTask pingTask; | ||
|
||
private OnThreadHandleListener listener; | ||
|
||
private SafeThread() { | ||
} | ||
|
||
public static SafeThread getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public void initConfig(int interval, OnThreadHandleListener listener) { | ||
this.interval = interval; | ||
this.listener = listener; | ||
startThread(); | ||
startPing(); | ||
} | ||
|
||
private void startThread() { | ||
if (thread == null) { | ||
thread = new Thread(runnable); | ||
} | ||
|
||
if (thread.isInterrupted() || !thread.isAlive()) { | ||
thread.start(); | ||
} | ||
} | ||
|
||
private void startPing() { | ||
if (pingTimer == null) { | ||
pingTimer = new Timer(); | ||
} | ||
|
||
if (pingTask == null) { | ||
pingTask = new TimerTask() { | ||
@Override | ||
public void run() { | ||
if (thread == null || thread.isInterrupted() || !thread.isAlive()) { | ||
startThread(); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pingTimer.schedule(pingTask, 30 * 1000, 30 * 1000); | ||
} | ||
|
||
final NamedRunnable runnable = new NamedRunnable("SafeThread") { | ||
|
||
@Override | ||
protected void execute() { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
|
||
if (listener != null) { | ||
listener.readThread(); | ||
} | ||
try { | ||
Thread.sleep(interval); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
interface OnThreadHandleListener { | ||
void readThread(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import org.sheedon.serial.DataCheckBean; | ||
import org.sheedon.serial.DataConverter; | ||
import org.sheedon.serial.Dispatcher; | ||
import org.sheedon.serial.NamedRunnable; | ||
import org.sheedon.serial.SafetyByteBuffer; | ||
import org.sheedon.serial.Util; | ||
import org.sheedon.serial.internal.CharsUtils; | ||
|
@@ -20,7 +19,7 @@ | |
* @Email: [email protected] | ||
* @Date: 2020/2/21 9:27 | ||
*/ | ||
public class SerialPort { | ||
public class SerialPort implements SafeThread.OnThreadHandleListener{ | ||
|
||
private android.serialport.SerialPort serialPort; | ||
private InputStream inputStream; | ||
|
@@ -31,8 +30,6 @@ public class SerialPort { | |
private DataConverter<SafetyByteBuffer, DataCheckBean> converter; | ||
private SafetyByteBuffer serialData = new SafetyByteBuffer(); | ||
|
||
private Thread thread; | ||
|
||
private int interval; | ||
|
||
/** | ||
|
@@ -51,16 +48,15 @@ public SerialPort(String path, int baudRate, int flags, int interval, | |
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; | ||
|
||
serialPort = new android.serialport.SerialPort(new File(path), baudRate, flags); | ||
|
||
inputStream = serialPort.getInputStream(); | ||
outputStream = serialPort.getOutputStream(); | ||
|
||
thread = new Thread(runnable); | ||
thread.start(); | ||
SafeThread.getInstance().initConfig(interval,this); | ||
|
||
} | ||
|
||
|
@@ -73,37 +69,14 @@ public void setCallback(SerialRealCallback callback) { | |
this.callback = callback; | ||
} | ||
|
||
// 串口 Runnable | ||
final class SerialRunnable extends NamedRunnable { | ||
|
||
SerialRunnable(String name) { | ||
super("SerialRunnable %s", name); | ||
} | ||
|
||
@Override | ||
protected void execute() { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
readThread(); | ||
try { | ||
Thread.sleep(interval); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 解析数据 | ||
*/ | ||
private void readThread() { | ||
|
||
byte[] data = getDataByte(); | ||
if (data == null || callback == null) | ||
return; | ||
@Override | ||
public void readThread() { | ||
byte[] data = getDataByte(); | ||
if (data == null || callback == null) | ||
return; | ||
|
||
serialData.append(data); | ||
dealWithData(); | ||
} | ||
serialData.append(data); | ||
dealWithData(); | ||
} | ||
|
||
private volatile boolean isStartDealWithData = false; | ||
|
@@ -192,10 +165,5 @@ public void closeSerialPort() { | |
outputStream = null; | ||
} | ||
|
||
if (thread != null) { | ||
thread.interrupt(); | ||
thread = null; | ||
} | ||
|
||
} | ||
} |