writeChars producing extra spaces

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

writeChars producing extra spaces

Postby shasan » Sun Nov 09, 2008 6:26 pm

The narrow version of my problem:

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. :)
shasan
New User
 
Posts: 5
Joined: Fri Nov 07, 2008 2:21 pm
Location: Chapel Hill, NC

Postby gloomyandy » Sun Nov 09, 2008 6:54 pm

Hi,
Well I suspect that the spacing has something to do with the fact that in Java strings use Unicode and so characters are always 2 bytes in length. However since both the nxt and the PC should be doing the same I'm not really sure what is going on. To answer your other question it would be easy to write a method to do what you want....

Code: Select all
void sendData(DataOutputStream s, int typ, int value)
{
  synchronized(s)
  {
    s.writeInt(typ);
    s.writeInt(value);
  }
}


On the PC just reverse the process and use the typ field to differentiate between the two. Note that the synchronized block ensures that if you call the function from two different threads at the same time there is no way for the typ and value of the two writes to become intermingled...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4187
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby shasan » Sun Nov 09, 2008 8:36 pm

Thanks, that is a smart solution. I'll go ahead and use that approach.

If anyone figures out why the extra spaces are appearing, though, I'd still be interested to know.

Thanks again!
shasan
New User
 
Posts: 5
Joined: Fri Nov 07, 2008 2:21 pm
Location: Chapel Hill, NC

Re: writeChars producing extra spaces

Postby skoehler » Fri Jun 29, 2012 8:38 am

writeChars writes two bytes per character, but readLine reads one byte per character and interpretated them as ISO-8859-1 or something similar. Also, readLine is conceptionally broken and deprecated.
The counterpart of writeChars is readChars, hence you should use readChars on the PC-side. However, you will have to write and read the string's length in advance.
Or you use writeUTF on the NXT-side and readUTF on the PC-side.
skoehler
leJOS Team Member
 
Posts: 1454
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

cron
more stuff