/** 补全读取串口返回数据处理Runnable的代码: * 使用serialPortHandler调用,每3秒循环执行自身 * 当model中reportFlag为真(开始上报状态)且返回消息长度大于0时才继续执行分析逻辑 * 分析读取到的返回信息(转换为String类型后分析),当其不为空且包含[TE,HU,IL,PU,AI,NO,CO]中任意一个字符时(不同传感器的数据标识),才继续执行数据更新逻辑 * 将返回信息中的其它字符替换为""(使用正则表达式"\\p{C}"),之后构造Message对象,Message的what设置为3,obj设置为处理后的返回信息字符串 * 调用UIHandler的sendMessage方法来根据收到的传感器信息更新界面 */ public Runnable serialPortRunnable = new Runnable() {
时间: 2024-04-04 20:35:34 浏览: 17
@Override
public void run() {
// 每3秒循环执行自身
serialPortHandler.postDelayed(this, 3000);
if (model.reportFlag && model.receiveData.length() > 0) {
// 分析读取到的返回信息
String receivedData = model.receiveData.toString();
if (receivedData != null && receivedData.matches(".*[TE,HU,IL,PU,AI,NO,CO].*")) {
// 将返回信息中的其它字符替换为""
String filteredData = receivedData.replaceAll("\\p{C}", "");
// 构造Message对象
Message message = new Message();
message.what = 3;
message.obj = filteredData;
// 调用UIHandler的sendMessage方法来根据收到的传感器信息更新界面
uiHandler.sendMessage(message);
}
// 清空接收到的数据
model.receiveData.setLength(0);
}
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)