简体   繁体   English

使用多个端口通过两对独立的Arducam和arduino Uno发送图像

[英]Using multiple ports to send images via two separate pair of Arducam and arduino Uno

I am trying to send images via two ports namely COM5 and COM7. 我正在尝试通过两个端口(即COM5和COM7)发送图像。 Following code does the most. 以下代码最有效。 Most significant part of the code is captureAndsaveImage method. 代码中最重要的部分是captureAndsaveImage方法。

The problem is when i use both serial ports; 问题是当我同时使用两个串口时; images are getting distorted they feel like they are getting mixed up. 图像变得失真,感觉像是混乱了。

My question: Is it possible to use both port simaltaneously? 我的问题:是否可以同时使用两个端口? What should i do such that there is no mixing up? 我应该怎么做才能避免混淆?

在此处输入图片说明 在此处输入图片说明

Don't mind second image's black circle it might have happened due to some signal losses in the second camera. 不要介意第二个图像的黑圈,它可能是由于第二个摄像头中的某些信号损失而发生的。

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;
    }

}

Edit : I used a debug statement like 编辑 :我使用了像这样的调试语句

    if(inputStream.available()>0){
            System.out.println(inputStream.toString());}

in the captureAndsaveImage method and i got output like 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

Things i observe is that some lines are like 我观察到的是有些线条像

 Found image: 6, Saved image:COM5images/6.bmp

and most of them are 他们大多数是

 Found image: 
5com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28
, Saved image:COM7images/5.bmp

What is the reason? 是什么原因? As far as i know com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28 this is supposed to be address of the inputStream. 据我所知com.fazecast.jSerialComm.SerialPort$SerialPortInputStream@6d6f6e28这应该是inputStream的地址。 But why it is not happening in some cases? 但是,为什么在某些情况下它没有发生呢? (I am beginner in Serial communication.) (我是串行通信的初学者。)

Hurray! 欢呼! I have solved my problem. 我已经解决了我的问题。 Even though solution isn't elegant. 即使解决方案并不完美。

I put a block of code in the start of captureAndsaveImage method like 我在captureAndsaveImage方法的开头放置了一段代码,例如

while(inputStream.available()>0){
            int temp=read(inputStream);

        }

And now I am getting cleared image. 现在,我的图像越来越清晰。 I have some vague idea how it worked but i would love if anyone can give logic for these. 我有一个模糊的想法,它是如何工作的,但是如果有人能对此给出逻辑,我将很高兴。

Edit: I observe that distorted images were coming in odd frames. 编辑:我观察到扭曲的图像出现在奇数帧中。 So the above code just skips those frames shows even frames which are not mixed up. 因此,上面的代码仅跳过那些显示即使没有混合的帧的帧。 :/ :/

Your code seems to be very disorganized. 您的代码似乎非常混乱。 For example, where you open COM5, your debug messages says it's opening COM7 and vice versa. 例如,在您打开COM5的地方,调试消息说它正在打开COM7,反之亦然。

However, the bug causing the issue you raised in your question is with these lines of code: 但是,导致您在问题中提出的问题的错误在于以下几行代码:

while(true) {
    captureAndsaveImage( inputStream2,counter, rgb, "COM5");
    captureAndsaveImage(inputStream, counter, rgb, "COM7");
    counter++;
}

As you can see, you're storing the data from both image sources into the same array, rgb . 如您所见,您正在将来自两个图像源的数据存储到同一数组rgb Your code has an rgb2 , so I suspect you meant to use one of those with COM5 and the other for COM7, though the array declarations being at different scopes is strange. 您的代码具有rgb2 ,因此我怀疑您打算将其中一个与COM5一起使用,而将另一个与COM7一起使用,尽管数组声明在不同范围内很奇怪。 I would suggest you review your code, and perhaps focus on getting things working with one serial port/data source before introducing a second. 我建议您检查您的代码,并最好在引入第二个串行端口/数据源之前集中精力使用一个串行端口/数据源。

Edit: reading your comment and reviewing your error, I found another bug: 编辑:阅读您的评论并查看您的错误,我发现了另一个错误:

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;
}

Here, isImageStart() could return true if it works out that you missed the starting character in a stream. 在这里,如果发现您错过了流中的起始字符,则isImageStart()可能返回true。 Essentially, since you recursively call isImageStart, if you start with a stream that doesn't contain the command character, you will run until you reach COMMAND.length , at which point, the next recursive call would skip the if (index < COMMAND.length) and return true. 本质上,由于您递归调用isImageStart,因此如果您以不包含命令字符的流开头,则将运行直到达到COMMAND.length ,此时下一个递归调用将跳过if (index < COMMAND.length)并返回true。 So, if you have a case where you started reading too soon (or too late), isImageStart() will still return true. 因此,如果您过早(或太晚)开始阅读,则isImageStart()仍将返回true。 Then, in CaptureAndSaveImage() , you still continue calling read on the inputstream and likely are reading stale data from the previous stream. 然后,在CaptureAndSaveImage() ,您仍然继续在输入流上调用read,并且很可能正在从前一个流中读取陈旧的数据。 Except by that point, the stream may be valid and depending on how fast data is coming in, you will have a mix of the previous image and the one currently being received. 到那时为止,该流可能是有效的,并且取决于输入数据的速度,您将混合使用先前的图像和当前接收的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM