Hi Guys,
I have a robot and a GUI application running on a GUI. I have a while loop on the robot side that is constantly sending data to the GUI.
Before i send a value, i send first a value which the GUI will use to determine how many consecutive values it must read afterwards for instance i send something like;
dataout.writeInt(2);
dataout.writeInt(50);
dataout.writeInt(506);
dataout.writeInt(50);
dataout.flush
Here the GUI reads 2 and then under the case 2, it will read the next two integers.
On the GUI side i have i while loop that is in a run() of a thread that is reading from the inputstream continuosly.
Inside the loop on the GUI i have a switch case statement.
Example
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 is not working as i want. This is what i get:
After it reads the first int, i get a series of numbers that it is reading from the inputstream. i don't know where those numbers come from.
I thought that if it cant read the numbers i send, then it must block, but it isn't.
For the example above this is what i get:
2
1761635840
1946182912
1845523456
1761636096
1845523200
1006658048
16274152968
All the numbers after the 2, i don't know where they come from. it doesn't read the numbers after the 2 i send.
I tried to insert some Thread.sleep(1000) but is not working.
What am i doing wrong? Need help
CODE
//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