急!!用Java串口程序接收单片机发送的串口数据时,遗漏了很多数据,求大神帮忙!!

It_BeeCoder 2017-07-13 09:37:00
最近在做一个小项目,需要用Java串口程序接收单片机发送的串口数据,单片机是1s发送100个数据,也就是10ms接收一个数据,但实际上我1s内接收的数据远少于100个,遗漏了很多,试了网上的好多方法,但还是有问题,真心求求知道或遇到过此问题的大神帮忙解答!下面贴一下我的主要代码,我是用一个线程来接收数据的:
public void run(){
try{
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException e) {

}
byte[] readB=new byte[200];
byte[] readC=new byte[200];
int nBytes=0;
String Buff="";
String Buffer="";
try{
while(inputStream.available()>0){

nBytes = inputStream.read(readB);
}
sHex.printHexString(readB);//将读出的字符数组数据,直接转换成十六进制。
// System.out.println("readBt=" + sHex.printHexString(readB));
}catch(IOException e){
System.err.println(e.toString());
}
sHex.printHexString(readB);的代码是:
public static void printHexString( byte[] b) {//作用是将数据打一个时间戳保存在文本文件中

SimpleDateFormat format=new SimpleDateFormat();
for (int i = 0; i < b.length; i++) {
String msg="";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSS");
msg+=sdf.format(date);
String hex = Integer.toHexString(b[i] & 0xFF)+" ";
if (hex.length() == 2) {
hex = '0' + hex;
}
String str=hex.toUpperCase()+" "+msg+ "\r\n";
writeMethod2(str);
}

}

接收到的数据是:
00 20170712145017234
00 20170712145017235
00 20170712145017235
00 20170712145017235
00 20170712145017235
00 20170712145017236
00 20170712145017236
00 20170712145017236
00 20170712145017236
00 20170712145017237
00 20170712145017237
00 20170712145017237
00 20170712145017238
00 20170712145017238
00 20170712145017238
00 20170712145017238
00 20170712145017239
00 20170712145017239
00 20170712145017239
00 20170712145017239
00 20170712145017240
00 20170712145017240
00 20170712145017240
00 20170712145017241
00 20170712145017241
00 20170712145017241(前面00是接收到的数据,后面是时间,精确到三位毫秒数,能够看到这里直接是从241毫秒到490毫秒了,漏了好多数据)
38 20170712145017490
34 20170712145017491
0D 20170712145017495
0A 20170712145017496
0D 20170712145017496
0A 20170712145017497
32 20170712145017497
30 20170712145017498
37 20170712145017498
39 20170712145017499
0D 20170712145017499
0A 20170712145017500
32 20170712145017500
30 20170712145017501
39 20170712145017501
39 20170712145017501

很着急,求大神帮帮忙!!!

...全文
305 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_38722979 2017-07-16
  • 打赏
  • 举报
回复
机制没错。
多试几次,都是那个时间没有数据?
安个comm monitor.查看是否那个时间段,包是否发出?
在单发 那个数据包,设断点,中断里是否有收到?
如果都能收到,线程堵塞?单个线城还是?设成一个线程试试。
It_BeeCoder 2017-07-13
  • 打赏
  • 举报
回复
怎么没人啊,自己顶~

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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