私は2つのポート、つまりCOM5とCOM7を介して画像を送信しようとしています。 次のコードが最も役に立ちます。コードの最も重要な部分はcaptureAndsaveImage
メソッドです。Arducamとarduino Unoの2つの別々のペアを介して複数のポートを使用して画像を送信する
問題は、私が両方のシリアルポートを使用している場合です。画像が歪んで、彼らは混乱しているように感じます。
私の質問:両方のポートを同時に使用することはできますか?混乱しないように私は何をすべきですか?
それが原因第2のカメラでいくつかの信号損失に起こったかもしれない第二の画像の黒丸を気にしないでください。
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 –