Understanding DataInputStream

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

Moderators: 99jonathan, roger, imaqine

Understanding DataInputStream

Postby Rickz2020 » Thu Jan 19, 2012 11:56 pm

Hi all,

I am currently sending data from my android to my robot like this:
Lets say I am sending a 10 and a 33...

//sending from android
Code: Select all
public void send(int command, int value) {
      if (Dos == null) {
         return;
      }
      try {

         Dos.writeInt(command);
         Dos.writeInt(value);         
         Dos.flush();
      } catch (IOException ioe) {
      }
   }


I then read the data on the robot like this:
//reading on robot
Code: Select all
do {
// This does give me 10 and 33, but not all of the time.
      int command = _dis.readInt();
      int value = _dis.readInt();         

      Control(command, value);
      Delay.msDelay(1);
   } while (_dis.readInt() != 9999);


The problem is, sometimes the data can get a bit mixed up. It would be better if I could send over an array of two int's. EG:

Code: Select all
int[] myArray = new int[2];
command = myArray[0];
value = myArray[1];


I can only see a byte array method but am not sure how to use it.

I've tried:
//sending from Anrdoid
Code: Select all
public void send(int command, int value) {
      if (Dos == null) {
         return;
      }
      try {
         byte[] buffer = new byte[2];
         buffer[0] = (byte) command;
         buffer[1] = (byte) value;
         Dos.write(buffer);
         Dos.flush();
      } catch (IOException ioe) {
      }
   }


//reading on robot
Code: Select all
byte[] buffer = new byte[2];

//DataInputStream
_dis.read(buffer);

//Here I would expect to see 10 and 33 at index 0 and 1, respectively.
int command = (int) buffer[0];
int value = (int) buffer[1];


but I'm not getting the expected results. Could someone please try explain to me how to read the byte array or perhaps point me in the right direction so that I may read up on it?

Thanks.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Understanding DataInputStream

Postby gloomyandy » Fri Jan 20, 2012 12:05 am

In your first example you are writing 2 ints but reading 3. Are you sure this is correct? Should you perhaps be testing one of the two first read returns against 9999?

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

Re: Understanding DataInputStream

Postby Rickz2020 » Fri Jan 20, 2012 1:03 am

Thanks mate, that has sorted the data mismatch.

That 9999 value is to be an indication to exit - something I've not yet implemented and forgot about it. Well spotted :D

I'd still like to know how to use the byte array. Please post an example use, if you have one handy.

Thanks.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff