Hi Guys,
Yesterday i posted a question about a problem i had concerning inputstream reading and i was helped.
I find myself in similar situation but this time i know that I am doing the right thing but yet it is not working for me.
I am reading from an inputstream but i get different value. No matter how i change the data i send i get the same string ("toForklift-42") as the value. At first i prefix the value i send with "toForklift-" but i have changed that string to different strings yet i get the same string in the bracket. i Even changed the number 42 to a different number but yet when i run the program, i get the same string in the console. Below is what am sending : as
Two classes for sending and receiving.
Am using the leJOS NXJ NXTConnector to make the connection and open the streams.
// sender class
class PanButton implements Runnable {
DataInputStream dis;
DataOutputStream dos;
TouchSensor touch = new TouchSensor(SensorPort.S4);
PanicButtonCrossing(DataInputStream is, DataOutputStream os) {
dos = os;
dis = is;
}
public void run() {
while (!touch.isPressed()) {}
// If you get a message: KILL EVERYTHING
Motor.A.stop();
Motor.B.stop();
Motor.C.stop();
try { // send 42
int value = 42;
dos.writeChars("ggggggggg" + 455 + "\n");
dos.flush();
Sound.systemSound(true, 3);
} catch (IOException ioe) {
LCD.drawString("Write Exception", 0, 0);
}
System.exit(1);
}
// Reader classs
public class InputReaderCrossing implements Runnable{
private DataInputStream dataIn;
private DataOutputStream dataOut;
public InputReaderCrossing(DataInputStream dataIn, DataOutputStream dataOut) {
this.dataIn = dataIn;
this.dataOut = dataOut;
this.sensor = sensor;
this.readLock = new Object();
}
public void run(){
while(true){
String dataFromCrossing1 = readLineFromCrossing();
System.out.println("CROSSING VALUE: " + dataFromCrossing1 + " :VALUEEEEE");
}
}
private String readLineFromCrossing() throws IOException{
StringBuffer sb = new StringBuffer();
synchronized(readLock){
while(true) {
char c = this.dataIn.readChar();
if (c == '\n') break;
sb.append(c);
}
return sb.toString();
}
}
}
I need your help please. i have spent 6 hours but can't find the the reason. I don't understand no matter what i send i get "toForklift-42".
At first i tried to send the 42 with the writeInt() methods but then on the reader class side i use readInt() but i get somethings like:
745687894
459877455
456987456
So i changed to the string to find out why and lo and behold i get that string no matter what i send. it is like that string is fixed in the input stream and nothing is sent. I don't know what is happening.
Need help