I'm trying to send two strings from the NXT to the PC to display a header and a sensor reading value. Here's the code on the NXT:
- Code: Select all
DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();
while(!Button.ESCAPE.isPressed()) {
//send sensor data
int c = (int)compass.getDegrees();
int s = (int)sonar.getDistance();
dos.writeChars("compass: " + Integer.toString(c) + '\n');
dos.flush();
dos.writeChars("sonar: " + Integer.toString(s) + '\n');
dos.flush();
Thread.sleep(100);
}
And on the PC:
- Code: Select all
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
while(true) {
try {
System.out.println(dis.readLine() + ", " + dis.readLine() + " | " + i);
System.out.flush();
} catch (IOException ioe) {
System.out.println("IO Exception reading bytes:");
System.out.println(ioe.getMessage());
break;
}
}
And this is my current output on the PC (note the extra spaces between characters):
- Code: Select all
c o m p a s s : 2 1 4 , s o n a r : 4 7
As you might guess, the structure of my program is generally based on BTSend/BTReceive. I tried making the modifications discussed in this thread with no avail. Any thoughts on why I might be getting these extra spaces? I am assuming the problem is on the NXT side.
The larger view of my problem:
What I'm trying to do is send a continuous stream of sensor data from my robot to the PC. I'd like to be able to send the data as just two ints, the compass reading and the sensor value. But, I don't know of any way to differentiate between the two on the PC (assuming they arrive in alternating order isn't a sufficient solution). The reason I'm writing the sensor readings as a string is so I can parse them on the PC side. Is there any way to embed some kind of "metadata" in the BT transmission itself so I don't have to parse strings?
In RobotC, I would direct the different sensor readings to different mailboxes on the NXT, or use "arguments" to specify what type of data was in the bluetooth transmission. As it stands, I'm not sure of any way to do this in Lejos without simply creating multiple connections for each sensor -- which is clearly a suboptimal solution.
Thanks in advance for your help. :)
