![](/img/trans.png)
[英]How to send data from Arduino-uno using Bluetooth module HC-05 and read it in Android?
[英]Using multiple ports to send images via two separate pair of Arducam and arduino Uno
我正在尝试通过两个端口(即COM5和COM7)发送图像。 以下代码最有效。 代码中最重要的部分是captureAndsaveImage
方法。
问题是当我同时使用两个串口时; 图像变得失真,感觉像是混乱了。
我的问题:是否可以同时使用两个端口? 我应该怎么做才能避免混淆?
不要介意第二个图像的黑圈,它可能是由于第二个摄像头中的某些信号损失而发生的。
public class ReadPort {
private static final char[]COMMAND = {'*', 'R', 'D', 'Y', '*'};
private static final int WIDTH = 320; //640;
private static final int HEIGHT = 240; //480;
SerialPort serialPort,serialPort2;
public int[][] rgb2 = new int[WIDTH][HEIGHT];
public static void main(String[] args) {
ReadPort reader= new ReadPort();
}
public ReadPort() {
int[][]rgb = new int[HEIGHT][WIDTH];
try {
serialPort = SerialPort.getCommPort("COM7");
serialPort.openPort();
inputStream = serialPort.getInputStream();
serialPort.setComPortParameters(1000000,
8,
SerialPort.ONE_STOP_BIT,
SerialPort.NO_PARITY);
if(serialPort.isOpen()){
System.out.println("COM5 opened");
}
serialPort2 = SerialPort.getCommPort("COM5");
serialPort2.openPort();
inputStream2 = serialPort2.getInputStream();
serialPort2.setComPortParameters(1000000,
8,
SerialPort.ONE_STOP_BIT,
SerialPort.NO_PARITY);
if(serialPort2.isOpen()){
System.out.println("COM7 opened");
}
int counter = 0;
while(true) {
captureAndsaveImage( inputStream2,counter, rgb, "COM5");
captureAndsaveImage(inputStream, counter, rgb, "COM7");
counter++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void captureAndsaveImage(InputStream inputStream, int counter,int[][] rgb,String name) throws IOException{
while(!isImageStart(inputStream, 0)){};
System.out.print("Found image: " + counter);
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int temp =read(inputStream);
rgb[y][x] = ((temp&0xFF) << 16) | ((temp&0xFF) << 8) | (temp&0xFF);
}
}
BMP bmp = new BMP();
bmp.saveBMP("c:/out/" +name+"images/"+ counter + ".bmp", rgb);
System.out.println(", Saved image:"+name+"images/"+counter+".bmp");
}
private static int read(InputStream inputStream) throws IOException {
int temp = (char) inputStream.read();
//System.out.print(temp);
if (temp == -1) {
throw new IllegalStateException("Exit");
}
return temp;
}
private static boolean isImageStart(InputStream inputStream, int index) throws IOException {
if (index < COMMAND.length) {
if (COMMAND[index] == read(inputStream)) {
return isImageStart(inputStream, ++index);
} else {
return false;
}
}
return true;
}
}
编辑 :我使用了像这样的调试语句
if(inputStream.available()>0){
System.out.println(inputStream.toString());}
在captureAndsaveImage
方法中,我得到的输出像
COM5 opened
COM7 opened
Found image:
0com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/0.bmp
Found image:
0com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/0.bmp
Found image:
1com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/1.bmp
Found image:
1com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/1.bmp
Found image: 2, Saved image:COM5images/2.bmp
Found image:
2com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/2.bmp
Found image:
3com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/3.bmp
Found image:
3com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/3.bmp
Found image: 4, Saved image:COM5images/4.bmp
Found image:
4com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/4.bmp
Found image:
5com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/5.bmp
Found image:
5com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/5.bmp
Found image: 6, Saved image:COM5images/6.bmp
Found image: 6, Saved image:COM7images/6.bmp
Found image:
7com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/7.bmp
Found image:
7com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/7.bmp
Found image: 8, Saved image:COM5images/8.bmp
Found image:
8com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/8.bmp
Found image:
9com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@7f31245a
, Saved image:COM5images/9.bmp
我观察到的是有些线条像
Found image: 6, Saved image:COM5images/6.bmp
他们大多数是
Found image:
5com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/5.bmp
是什么原因? 据我所知com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
这应该是inputStream的地址。 但是,为什么在某些情况下它没有发生呢? (我是串行通信的初学者。)
欢呼! 我已经解决了我的问题。 即使解决方案并不完美。
我在captureAndsaveImage
方法的开头放置了一段代码,例如
while(inputStream.available()>0){
int temp=read(inputStream);
}
现在,我的图像越来越清晰。 我有一个模糊的想法,它是如何工作的,但是如果有人能对此给出逻辑,我将很高兴。
编辑:我观察到扭曲的图像出现在奇数帧中。 因此,上面的代码仅跳过那些显示即使没有混合的帧的帧。 :/
您的代码似乎非常混乱。 例如,在您打开COM5的地方,调试消息说它正在打开COM7,反之亦然。
但是,导致您在问题中提出的问题的错误在于以下几行代码:
while(true) {
captureAndsaveImage( inputStream2,counter, rgb, "COM5");
captureAndsaveImage(inputStream, counter, rgb, "COM7");
counter++;
}
如您所见,您正在将来自两个图像源的数据存储到同一数组rgb
。 您的代码具有rgb2
,因此我怀疑您打算将其中一个与COM5一起使用,而将另一个与COM7一起使用,尽管数组声明在不同范围内很奇怪。 我建议您检查您的代码,并最好在引入第二个串行端口/数据源之前集中精力使用一个串行端口/数据源。
编辑:阅读您的评论并查看您的错误,我发现了另一个错误:
private static boolean isImageStart(InputStream inputStream, int index) throws IOException {
if (index < COMMAND.length) {
if (COMMAND[index] == read(inputStream)) {
return isImageStart(inputStream, ++index);
}
else {
return false;
}
}
return true;
}
在这里,如果发现您错过了流中的起始字符,则isImageStart()
可能返回true。 本质上,由于您递归调用isImageStart,因此如果您以不包含命令字符的流开头,则将运行直到达到COMMAND.length
,此时下一个递归调用将跳过if (index < COMMAND.length)
并返回true。 因此,如果您过早(或太晚)开始阅读,则isImageStart()
仍将返回true。 然后,在CaptureAndSaveImage()
,您仍然继续在输入流上调用read,并且很可能正在从前一个流中读取陈旧的数据。 到那时为止,该流可能是有效的,并且取决于输入数据的速度,您将混合使用先前的图像和当前接收的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.