java 为什么我从串口没有接收到完整的字符串 而且每次接收的字符串个数都不同 

时间:2022-08-02 23:43:32
inputStream=serialPort.getInputStream();
try {
 byte[] readBuffer= new byte[1024];   
 int receivedData = inputStream.read(readBuffer);
   System.out.println(   new String(readBuffer)); 
                    } catch (IOException e) {}
每次接收字符串的个数都不同 没有接收到完整的字符串 请指教

6 个解决方案

#1


catch (IOException e)  {//这里面写点东西吧.不写怎么知道错没错啊} 

#2


现在能接收到字符串 但是发送的字符串不能全部接收完
而且每次收到的字符串的个数还不同...

#3


int receivedData = inputStream.read(); 试试

#4



int receivedData	
for (receivedData = inputStream.read(readBuffer); receivedData > 0; receivedData = inputStream.read(readBuffer))
;



加个循环读。

#5


楼上的FOR循环什么也读不出来啊!!!
int receivedData = inputStream.read(); 只能得到一个整形的数值的

#6




得用循环读吧,因为不见得你每次一次性就能把数据读完。

#1


catch (IOException e)  {//这里面写点东西吧.不写怎么知道错没错啊} 

#2


现在能接收到字符串 但是发送的字符串不能全部接收完
而且每次收到的字符串的个数还不同...

#3


int receivedData = inputStream.read(); 试试

#4



int receivedData	
for (receivedData = inputStream.read(readBuffer); receivedData > 0; receivedData = inputStream.read(readBuffer))
;



加个循环读。

#5


楼上的FOR循环什么也读不出来啊!!!
int receivedData = inputStream.read(); 只能得到一个整形的数值的

#6




得用循环读吧,因为不见得你每次一次性就能把数据读完。