Skip to content

Commit e6e9f6c

Browse files
Merge pull request #2 from evision-ai/feat/add-listener-for-IOException
add a listener if read thread encounters IOException
2 parents cc34b68 + 94e7621 commit e6e9f6c

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

SerialPortLibrary/src/main/java/com/kongqw/serialportlibrary/SerialPortManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ public void onDataReceived(byte[] bytes) {
198198
mOnSerialPortDataListener.onDataReceived(bytes);
199199
}
200200
}
201+
202+
@Override
203+
public void onIOException(IOException e) {
204+
if (null != mOnSerialPortDataListener) {
205+
mOnSerialPortDataListener.onIOException(e);
206+
}
207+
}
201208
};
202209
mSerialPortReadThread.start();
203210
}

SerialPortLibrary/src/main/java/com/kongqw/serialportlibrary/listener/OnSerialPortDataListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.kongqw.serialportlibrary.listener;
22

3+
import java.io.IOException;
4+
35
/**
46
* Created by Kongqw on 2017/11/14.
57
* 串口消息监听
@@ -20,4 +22,9 @@ public interface OnSerialPortDataListener {
2022
* @param bytes 发送的数据
2123
*/
2224
void onDataSent(byte[] bytes);
25+
26+
/**
27+
* 串口读写过程中遇到IO异常
28+
*/
29+
void onIOException(IOException e);
2330
}

SerialPortLibrary/src/main/java/com/kongqw/serialportlibrary/thread/SerialPortReadThread.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.kongqw.serialportlibrary.thread;
22

3+
import android.os.SystemClock;
34
import android.util.Log;
45

56
import java.io.IOException;
@@ -14,6 +15,7 @@
1415
public abstract class SerialPortReadThread extends Thread {
1516

1617
public abstract void onDataReceived(byte[] bytes);
18+
public abstract void onIOException(IOException e);
1719

1820
private static final String TAG = SerialPortReadThread.class.getSimpleName();
1921
private InputStream mInputStream;
@@ -50,13 +52,14 @@ public void run() {
5052
// 有的串口发送程序会产生一定的延迟,直接读取会导致串口消息分段
5153
// 因此需要根据设置情况等待一段时间,保证发送端将消息发送完毕
5254
if (mReadDelay > 0) {
53-
Thread.sleep(mReadDelay);
55+
SystemClock.sleep(mReadDelay);
5456
}
5557

5658
// 读取串口输入
5759
int size = mInputStream.read(mReadBuffer);
58-
if (-1 == size || 0 >= size) {
59-
return;
60+
if (0 >= size) {
61+
// 本次读取失败,重新等待输入
62+
continue;
6063
}
6164

6265
byte[] readBytes = new byte[size];
@@ -66,7 +69,10 @@ public void run() {
6669
Log.i(TAG, "run: readBytes = " + new String(readBytes));
6770
onDataReceived(readBytes);
6871
} catch (IOException e) {
72+
// 如果串口掉线会抛这个异常 java.io.IOException: I/O error
73+
// 此时必须退出线程,因为即使设备再重新连上,也必须关闭输入流再重新打开
6974
e.printStackTrace();
75+
onIOException(e);
7076
return;
7177
} catch (Exception e) {
7278
e.printStackTrace();

app/src/main/java/com/kongqw/serialport/SerialPortActivity.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
import com.kongqw.serialportlibrary.SerialPortManager;
1717

1818
import java.io.File;
19+
import java.io.IOException;
1920
import java.util.Arrays;
2021

2122
public class SerialPortActivity extends AppCompatActivity implements OnOpenSerialPortListener {
2223

2324
private static final String TAG = SerialPortActivity.class.getSimpleName();
2425
public static final String DEVICE = "device";
2526
private SerialPortManager mSerialPortManager;
26-
27+
2728
@Override
2829
protected void onCreate(Bundle savedInstanceState) {
2930
super.onCreate(savedInstanceState);
@@ -66,6 +67,16 @@ public void run() {
6667
}
6768
});
6869
}
70+
71+
@Override
72+
public void onIOException(final IOException e) {
73+
runOnUiThread(new Runnable() {
74+
@Override
75+
public void run() {
76+
showToast("设备离线 " + e.getMessage());
77+
}
78+
});
79+
}
6980
})
7081
.openSerialPort(device.getFile(), 115200);
7182

0 commit comments

Comments
 (0)