版權(quán)聲明:本文為CSDN博主「pxw1992」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/pxw1992/article/details/115402870
android 串口通信 ,系統(tǒng)休眠后,有概率出現(xiàn)數(shù)據(jù)丟包,在網(wǎng)上找到一篇文章,將串口讀數(shù)據(jù)部分代碼采取下面的方式,解決了該問題。
/**
* 串口讀數(shù)據(jù)線程
*/
public void run() {
byte[] buf = new byte[6];
while (!isInterrupted()) {
try {
if (null == mInputStream) {
return;
}
if (mInputStream.available() <= 0) {
continue;
}else {
try {
Thread.sleep(50L);
} catch (Exception e) {
e.printStackTrace();
}
}
int read = mInputStream.read(buf);
if (read > 0) {
byte[] readBytes = new byte[read];
System.arraycopy(buf, 0, readBytes, 0, read);
onDataReceived(readBytes);
Log.i(TAG, "run: readBytes = " + byte2hex(readBytes));
}
try {
Thread.sleep(10L);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log.e(TAG, "read thread over");
}