我試圖通過兩個端口(即COM5和COM7)發送圖像。 以下代碼的功能最強。最重要的部分代碼是captureAndsaveImage
方法。使用多個端口通過兩個獨立的Arduam和Arduino Uno發送圖像
問題是,當我使用兩個串行端口;圖像越來越扭曲,他們覺得自己正在混合起來。
我的問題:是否可以同時使用兩個端口?我應該做什麼以免混淆?
不要介意第二圖像的黑圈,可能發生由於在第二相機的一些信號損失。
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;
}
}
編輯:我在captureAndsaveImage
方法所使用的調試語句像
if(inputStream.available()>0){
System.out.println(inputStream.toString());}
和我有一樣
輸出COM5 opened
COM7 opened
Found image:
[email protected]45a
, Saved image:COM5images/0.bmp
Found image:
[email protected]e28
, Saved image:COM7images/0.bmp
Found image:
[email protected]45a
, Saved image:COM5images/1.bmp
Found image:
[email protected]e28
, Saved image:COM7images/1.bmp
Found image: 2, Saved image:COM5images/2.bmp
Found image:
[email protected]e28
, Saved image:COM7images/2.bmp
Found image:
[email protected]45a
, Saved image:COM5images/3.bmp
Found image:
[email protected]e28
, Saved image:COM7images/3.bmp
Found image: 4, Saved image:COM5images/4.bmp
Found image:
[email protected]e28
, Saved image:COM7images/4.bmp
Found image:
[email protected]45a
, Saved image:COM5images/5.bmp
Found image:
[email protected]e28
, Saved image:COM7images/5.bmp
Found image: 6, Saved image:COM5images/6.bmp
Found image: 6, Saved image:COM7images/6.bmp
Found image:
[email protected]45a
, Saved image:COM5images/7.bmp
Found image:
[email protected]e28
, Saved image:COM7images/7.bmp
Found image: 8, Saved image:COM5images/8.bmp
Found image:
[email protected]e28
, Saved image:COM7images/8.bmp
Found image:
[email protected]45a
, Saved image:COM5images/9.bmp
的事情,我看到的是,一些線條像
Found image: 6, Saved image:COM5images/6.bmp
,其中大部分是
Found image:
[email protected]e28
, Saved image:COM7images/5.bmp
的原因是什麼?據我所知[email protected]28
這應該是inputStream的地址。但爲什麼在某些情況下不會發生? (我是串口通訊的初學者。)
謝謝@Michael –