[英]read specific number of bytes from serial in java
I am reading bytes from a SD using arduino to a java application. 我正在使用arduino从SD读取字节到Java应用程序。 this is my approach:
这是我的方法:
if(temp.contains("_i_;")){
imgByte = new ArrayList<>();
imgByte.clear();
for(int i=0; i< min(imgSize , 64) ;i++){
imgByte.add((byte)rx.getInputStream().read());
imgSize--;
}
Files.write(imgFile, toByteArray(imgByte),StandardOpenOption.APPEND );
System.out.println("Left: "+imgSize);
return true;
}
i ; 我 is just indicates a 64 packet is coming.
仅表示有64个数据包即将到来。
but it is too slow that i can't receive the picture on time. 但是我不能按时收到照片太慢了。 is there another way for doing that so i can read all 64 bytes in only one line?
有没有另一种方法可以做到这一点,所以我只能在一行中读取所有64个字节? and not iterating with a for loop?
而不是用for循环进行迭代? PS: toByteArray converts byte ArrayList to byte[] so i can write it to file .
PS:toByteArray将字节ArrayList转换为byte [],因此我可以将其写入file。 I am using jSerialComm library from serial communication
我正在通过串行通信使用jSerialComm库
你有没有尝试过
Files.copy(rx.getInputStream(), imgFile);
I solved the problem in the following way: 我通过以下方式解决了这个问题:
if(temp.contains("_i_;")){
imgByte = new ArrayList<>();
imgByte.clear();
byte[] data = new byte[64];
// for(int i=0; i< min(imgSize , 64) ;i++){
// imgByte.add((byte)rx.getInputStream().read());
rx.getInputStream().read(data, 0, min(imgSize , 64));
imgSize-=min(imgSize , 64);
// }
//Files.write(imgFile, toByteArray(imgByte),StandardOpenOption.APPEND );
Files.write(imgFile, data,StandardOpenOption.APPEND );
System.out.println("Left: "+imgSize);
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.