File tree Expand file tree Collapse file tree
SerialPortLibrary/src/main/java/com/kongqw/serialportlibrary
app/src/main/java/com/kongqw/serialport Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 11package 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}
Original file line number Diff line number Diff line change 11package com .kongqw .serialportlibrary .thread ;
22
3+ import android .os .SystemClock ;
34import android .util .Log ;
45
56import java .io .IOException ;
1415public 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 ();
Original file line number Diff line number Diff line change 1616import com .kongqw .serialportlibrary .SerialPortManager ;
1717
1818import java .io .File ;
19+ import java .io .IOException ;
1920import java .util .Arrays ;
2021
2122public 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
You can’t perform that action at this time.
0 commit comments