1

ロボットとGUIアプリケーションをGUIで実行しています。ロボット側にwhileループがあり、常にGUIにデータを送信しています。

値を送信する前に、GUIが後で読み取る必要のある連続する値の数を決定するために使用する値を最初に送信します。たとえば、次のようなものを送信します。

dataout.writeInt(2);
dataout.writeInt(50);
dataout.writeInt(506);
dataout.writeInt(50);
dataout.flush 

ここで、GUIは2を読み取り、ケース2では、次の2つの整数を読み取ります。

GUI側では、入力ストリームから継続的に読み取っているスレッドのrun()内にあるiwhileループがあります。

GUIのループ内に、switchcaseステートメントがあります。

while(true){
int val = dataIn.readIn()

switch(val){

    case 1:
            int color = readInt();
      break;

case 2:
         int me= readInt();
         int you= readInt();
      break;

case 3:
         int megg = readInt();
         int youss = readInt();
          int mes = readInt();
         int youe = readInt();
      break;

}

} 

tは私が望むように機能していません。これは私が得るものです:

最初のintを読み取った後、入力ストリームから読み取っている一連の数値を取得します。それらの数字がどこから来ているのかわかりません。

送信した番号が読めない場合はブロックする必要があると思いましたが、そうではありません。

上記の例では、これは私が得るものです:

2
1761635840
1946182912
1845523456
1761636096
1845523200
1006658048
16274152968 

2以降のすべての数字はどこから来たのかわかりません。2を送信した後の番号は読み取られません。

Thread.sleep(1000)を挿入しようとしましたが、機能しません。

私は何が間違っているのですか?助けが必要

コード

//This code on the robot


public class ForkliftColorSensorReader implements Runnable{

 DataOutputStream outputStream;
    ColorSensor colorSensor;


public ForkliftColorSensorReader(ColorSensor colorSensor, DataOutputStream outputStream) {

        this.outputStream = outputStream;
        this.colorSensor = colorSensor;
}


  public void run() {
        int code = 1;

        while (code == 1){

     try {
        Thread.sleep(1000);
    outputStream.writeInt(10);
    outputStream.flush();
    outputStream.writeInt(2);
    outputStream.flush();
    } catch (Exception e) {

                                }         
                 }



     try {
        Thread.sleep(1000);
    outputStream.writeInt(20);
    outputStream.flush();
    outputStream.writeInt(4);
    outputStream.flush();
    } catch (Exception e) {

                                }         
                 }

  }

}



//This code on the GUI

public class Receive  implements Runnable{


int num = this.dataIn.readInt();

public void run(){
switch(num){
    case 10:

    int color = this.dataIn.read();

    break;


    case 20:

    int c = this.dataIn.read();

    break;


default;


}

}

}


// I am using NXTConnector from the GUI to make the connection to the robot. 
//I then use the DataOutputstream from the connection to read the data
4

1 に答える 1

1

テキストintentions<bはあなたにとって何か意味がありますか?入力ストリームからそれを読んでいます。それが、これらの数値が特定の文字セットに対応するものです。私の推測では、出力ストリームに HTML を少し書き込んでいると思います。DataOutputStream にのみ書き込みを行っており、基になる OutputStream にも同時に書き込みを行っていませんか? また、それは実際にコードを読んだときにどのように見えるのでしょうか? もしそうなら、readInt() メソッドはどのように定義されていますか?

編集:上記を解決するためのスニピット。

int input = 184549376 ;
byte[] bytes = { (byte)(input >> 24), (byte)(input >> 16),
        (byte)(input >> 8), (byte)(input) };
System.out.printf("int: %d hex: %08X string: %s",
        input, input, new String(bytes));

編集 #2 : あなたのコードでは、あなたはで書いてwriteInt()、で読んでread()ます。で書き込まれたフィールドを読み取るには、 を使用する必要があります。 、使用しているものは、1バイトのデータを読み取り、intに格納します。あなたが望むものではありません。readInt()writeInt()InputStream.read()

于 2010-07-28T13:39:41.300 に答える