views:

46

answers:

2

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
+1  A: 

Does the text intentions<b mean anything to you? You're reading that from your input stream; that's what those numbers correspond to in a certain character set. My guess is you've got a bit of HTML writing to your output stream. Are you sure you're ONLY writing to the DataOutputStream and not also the underlying OutputStream at the same time? Also, is that really what you read code looks like? If so, how is the readInt() method defined?

EDIT: Snippit for working out the above.

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));

EDIT #2: In your code, you write with writeInt() and read with read() This is exactly the sort of non-symmetry I was talking about. You must use readInt() to read a field that was written with writeInt(). InputStream.read(), what you are using, reads one byte of data and stores it in an int. Not what you want.

Mark Peters
You are right please. Thank you soo much. I am getting different numbers now. 167772160018454937616944988161845493761694498816Please show me me how you converted the numbers so that i can use it to solve the rest. Thanks for your help
Edy Moore
I was just using Windows calculator to convert to hex and then looking up each byte in an ASCII table. But I'll post a little Java snippit which does the same.
Mark Peters
By the way it doesn't seem like you're reading characters anymore. It would be useful to see how you're really writing and reading. In particular, any time you use a DataInput/OutputStream you must be **strictly** symmetrical in your operations and ordering.
Mark Peters
Thanks for your post. The robot is sending information continuously to the GUI and i have to read them to update the GUI. Any suggestions of doing it? I thought reading from the while loop is the best way.
Edy Moore
Reading from the while loop is fine. But you haven't given a correct example so I can't tell you whether you're doing it properly. For example, you have typos in your example (readIn?) and you haven't said yet whether `readInt()` is supposed to be `dataIn.readInt()` or if it's a method you've defined elsewhere. Plus, your examples don't line up: in the write, you write three ints after "2" but when you read you only read two ints after reading "2".
Mark Peters
OK Thanks . i am going to edit my post and give you the real example.
Edy Moore
What is surprising me is after i stop the robot, the the inputstream keeps on reading those numbers. I will post the example now. The readInt(); methods are the standard for the DataOutputstream. i have not define anything.
Edy Moore
Updated answer to reflect your code.
Mark Peters
Hi Mark, I would like to thank you so much for your help. It The problem was the read() in place of readInt(). It works perfectly now. Thanks
Edy Moore
@Kap: Good to hear it! If you consider this question answered, you can accept my answer using the checkmark next to the answer. That marks the question as answered and is a good habit to get into since it keeps things tidy. Plus, we both get rep. from it :-).
Mark Peters
A: 

Thanks Mark for your answer. I am sure that is the problem. I will check it tomorrow. Thank you very much.

kap
@kap - PLease accept any answers that answer your question. You do this by clicking the chevrons by the answer. If you keep not accepting answers you will be very unpopular and people will no longer answer your questions
Romain Hippeau