我正在尝试通过两个端口(即COM5和COM7)发送图像。
以下代码最有效。代码中最重要的部分是captureAndsaveImage方法。

问题是当我同时使用两个串口时;图像变得失真,它们感觉好像混在一起了。

我的问题:是否可以同时使用两个端口?我应该怎么做才能避免混淆?

java - 使用多个端口通过两个单独的Arducam和arduino Uno发送图像-LMLPHP
java - 使用多个端口通过两个单独的Arducam和arduino Uno发送图像-LMLPHP

不要介意第二个图像的黑圈,它可能是由于第二个摄像头中的某些信号损失而发生的。

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

        }


现在,我的图像越来越清晰。我不清楚它是如何工作的
但我希望有人能对此给出逻辑。

编辑:我观察到扭曲的图像出现在奇数帧中。因此,上面的代码仅跳过那些显示即使没有混合的帧的帧。 :/

07-24 09:45
查看更多