ロボットと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