JAVA串口 数据丢失 读取字节分段

u013350698 u013350698 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

public static byte[] readFromPort(SerialPort serialPort)
throws ReadDataFromSerialPortFailure,
SerialPortInputStreamCloseFailure {
InputStream in = null;
byte[] bytes = null;
try {
in = serialPort.getInputStream();
// 获取buffer里的数据长度
int bufflenth = in.available();
while (bufflenth != 0) {
// 初始化byte数组为buffer中数据的长度
bytes = new byte[bufflenth];
in.read(bytes);
bufflenth = in.available();
}
} catch (IOException e) {
throw new ReadDataFromSerialPortFailure();
} finally {
try {
if (in != null) {
in.close();
in = null;
}
} catch (IOException e) {
throw new SerialPortInputStreamCloseFailure();
}
}
return bytes;
}

用RXTX 读取串口数据 每次的字节都不一样 而且数据会丢失 得不到完整数据
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
u013350698
Java EE 67,535 成员 225,843 内容