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

u013350698 2017-08-03 07:19:20

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 读取串口数据 每次的字节都不一样 而且数据会丢失 得不到完整数据
...全文
307 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2017-08-04
  • 打赏
  • 举报
回复
先检查串口设置,这个问题一般是串口参数错

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧