Skip to content

Commit

Permalink
新增停止串口,bug修改
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxudong committed May 20, 2020
1 parent 78f7a78 commit b62cd1b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 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 9
versionName "1.2.5"
versionCode 10
versionName "1.2.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
18 changes: 18 additions & 0 deletions serial/src/main/java/org/sheedon/serial/serialport/SafeThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ protected void execute() {
}
};

public void close() {
if (pingTimer != null) {
pingTimer.cancel();
pingTimer = null;
}

if (pingTask != null) {
pingTask.cancel();
pingTask = null;
}

if (thread != null) {
thread.interrupt();
thread = null;
}

}

interface OnThreadHandleListener {
void readThread();
}
Expand Down
14 changes: 10 additions & 4 deletions serial/src/main/java/org/sheedon/serial/serialport/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @Email: [email protected]
* @Date: 2020/2/21 9:27
*/
public class SerialPort implements SafeThread.OnThreadHandleListener{
public class SerialPort implements SafeThread.OnThreadHandleListener {

private android.serialport.SerialPort serialPort;
private InputStream inputStream;
Expand Down Expand Up @@ -58,7 +58,7 @@ public SerialPort(String path, int baudRate, int flags, int interval,
outputStream = serialPort.getOutputStream();

safeThread = new SafeThread();
safeThread.initConfig(interval,this);
safeThread.initConfig(interval, this);

}

Expand Down Expand Up @@ -113,7 +113,8 @@ private void dealWithData() {
* @throws NullPointerException 如果inputStream为空,就抛出异常
*/
private synchronized byte[] getDataByte() throws NullPointerException {
if (inputStream == null) throw new NullPointerException("is null");
if (inputStream == null)
return null;
try {
int size = inputStream.available();
if (size > 0) {
Expand All @@ -140,7 +141,8 @@ public boolean sendMessage(String data) {
* @param bytes 显示的16进制的字符串
*/
private boolean setData(byte[] bytes) throws NullPointerException {
if (outputStream == null) throw new NullPointerException("outputStream为空");
if (outputStream == null)
return false;
try {
outputStream.write(bytes);
return true;//发送成功
Expand All @@ -154,6 +156,10 @@ private boolean setData(byte[] bytes) throws NullPointerException {
* 关闭串口
*/
public void closeSerialPort() {
if(safeThread != null){
safeThread.close();
safeThread = null;
}
if (serialPort != null) {
serialPort.close();
serialPort = null;
Expand Down

0 comments on commit b62cd1b

Please sign in to comment.