Problem with DataInputStream

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

Moderators: 99jonathan, roger, imaqine

Problem with DataInputStream

Postby nikita190 » Fri Jun 01, 2012 11:55 pm

Before I worked without problem readInt () but not anymore. And I do not understand what the problem. Now I separated from my code and robots throws me the following exception:
Code: Select all
Class 87  -> java.io.EOFExeption
  at 50:64 -> Method 50: Holi.main(java.lang.String[]) PC 7983 Signature id 0
  at 233:17 ->Method 233: java.io.DataInputStream.readByte0() PC 14546 Signature id 528
  at 234:2 -> Method 234: java.io.DataInputStream.readInt() PC 14572 Signature id 536


code on the robot
Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.*;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
 
public class Holi
{

   private static BTConnection conn;
   private static DataInputStream dis;
   private static DataOutputStream dos;
   
 
    public static void main(String[] args)
    {
      System.out.print("\nEsperando");
      conn=Bluetooth.waitForConnection();
      System.out.print("\nConectado");
      dis=conn.openDataInputStream();
      dos=conn.openDataOutputStream();
      int conectado = 1;
         
      while(!Button.ESCAPE.isDown())
      {
         System.out.print("Lectura:");
         int cont=0;
         for(int i=0;i<100;i++)
         {
            try {
               cont=dis.readInt();
               System.out.print("\nRecivi "+cont);
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
    }
}


code on the PC
Code: Select all
public class prueba
{
   static NXTConnector conn;
   public static DataOutputStream dos;
   public static DataInputStream dis;

         
   
   public static void main(String[] args)
   {
      System.out.print("\n Conectando.");
      conn = new NXTConnector();
      conn.addLogListener(new NXTCommLogListener(){ 
         public void logEvent(String message) {
            System.out.println("BTSend Log.listener: "+message);
         }
         public void logEvent(Throwable throwable) {
            System.out.println("BTSend Log.listener - stack trace: ");
             throwable.printStackTrace();
         }
      });
      
      boolean connected = conn.connectTo("Nika");
      if (!connected)
      {
         System.err.println("No se pudo conectar con NIKITA");
         System.exit(1);
      }

      dos = new DataOutputStream(conn.getOutputStream());
      dis = new DataInputStream(conn.getInputStream());
      try {
         Thread.sleep(2000);
      } catch (InterruptedException e1) {
         e1.printStackTrace();
      }
      for(int i=0;i<100;i++)
      {
         try {
            dos.writeInt(i);
            dos.flush();
            System.out.print("\nenvie "+i);
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}


It is a basic test code simply

I'm using:
- Jdk-6u25-windows-i586
- Lejos_nxj_0.8.5 (I've also tried 0.9.0)
- Netbeans 7.1
- Win 7--64
nikita190
New User
 
Posts: 18
Joined: Mon Mar 26, 2012 10:48 pm

Re: Problem with DataInputStream

Postby gloomyandy » Sat Jun 02, 2012 9:28 am

Hi,
Your program is doing what I would expect it to. Your PC program is writing 100 ints and then exiting which will close the connection, your NXT program is reading 100 ints, and is then looping round to read a further 100 ints (you have a while loop around the for loop the reads the ints), when it tries to read the 1st int in the second iteration of the for loop then it will find that the connection has been closed by the PC program and so will throw the EOF exception...

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

Re: Problem with DataInputStream

Postby nikita190 » Sat Jun 02, 2012 5:08 pm

Ok, I understand your point in this case. But I still doubt that in the other program gets stuck reading, ie to reach the readInt () does nothing else, and he does not read data sent. And it's strange because the same code (other than the one submitted) worked well but not now.
nikita190
New User
 
Posts: 18
Joined: Mon Mar 26, 2012 10:48 pm

Re: Problem with DataInputStream

Postby skoehler » Sat Jun 02, 2012 5:26 pm

Andy is correct about the program you posted in this thread. The NXT is trying to read more than 100 ints. Does the error still happen, if the NXT only reads 100 ints? Does the EOFException happen before the NXT has successfully read 100 ints?
Or are you talking about another program/problem in your last post?
skoehler
leJOS Team Member
 
Posts: 1413
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem with DataInputStream

Postby gloomyandy » Sat Jun 02, 2012 9:51 pm

If you had code that worked before, then something has changed. Have you made any changes to either the PC or NXT code? If you can't work out what the problem is you need to step back and work out exactly what is happening. Check that you are actually sending the data (and flushing the streams), make sure that you are not issuing more reads than you think you are. We can't really help without more details of what is actually going wrong...

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

Re: Problem with DataInputStream

Postby nikita190 » Sun Jun 03, 2012 1:38 am

skoehler wrote:Andy is correct about the program you posted in this thread. The NXT is trying to read more than 100 ints. Does the error still happen, if the NXT only reads 100 ints? Does the EOFException happen before the NXT has successfully read 100 ints?
Or are you talking about another program/problem in your last post?


gloomyandy wrote:»
Hi,
Your program is doing what I would expect it to. Your PC program is writing 100 ints and then exiting which will close the connection, your NXT program is reading 100 ints, and is then looping round to read a further 100 ints (you have a while loop around the for loop the reads the ints), when it tries to read the 1st int in the second iteration of the for loop then it will find that the connection has been closed by the PC program and so will throw the EOF exception...


the error in the case expressed the exception occurs before reading.
The code is equal to previous work, no changes have been made ​​either in or on the PC.
Changed Brick thinking the problem could go there but stayed the same.
In the case of my program does not throw any error, it just comes to readInt () and no action. But if you send messages to your PC.
nikita190
New User
 
Posts: 18
Joined: Mon Mar 26, 2012 10:48 pm

Re: Problem with DataInputStream

Postby gloomyandy » Sun Jun 03, 2012 8:04 am

Hi, as I've already said without more information we can't really help. Are you sending data to the PC? Your test program does not do that. Try making your test program more like your actual program until you can reproduce the actual problem. Then post that code here.

Good luck

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

Re: Problem with DataInputStream

Postby nikita190 » Sun Jun 03, 2012 7:26 pm

gloomyandy wrote:Hi, as I've already said without more information we can't really help. Are you sending data to the PC? Your test program does not do that. Try making your test program more like your actual program until you can reproduce the actual problem. Then post that code here.

Good luck

Andy



My test program represents my real program, only the test throws me an exception, and the other nothing, it just sticks in RedINT (), without any kind of action. I honestly do not know what else to read, send achievement information but not read.
nikita190
New User
 
Posts: 18
Joined: Mon Mar 26, 2012 10:48 pm

Re: Problem with DataInputStream

Postby gloomyandy » Sun Jun 03, 2012 8:25 pm

We have already explained that the exception in your test program is to be expected basically your test program has a programming error in it. Try removing the line "while(!Button.ESCAPE.isDown())" and it should work without throwing the exception.

Without more information about the actual problem there is nothing more we can do to help. Clearly since your test program does not reproduce the problem in your actual program there must be some differences. All I can suggest is that you try making the test program more like your actual program...

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

Re: Problem with DataInputStream

Postby skoehler » Sun Jun 03, 2012 8:41 pm

nikita190 wrote:... and the other nothing, it just sticks in RedINT (), without any kind of action.

The readInt() method waits until the other side (in this case the program on the PC) writes 4 bytes. Is it possible, that you don't understand the concept of blocking I/O as implemented by the DataInputStream class?
If you want to do other stuff in parallel to waiting for data to arrive, then you have to use Threads.
skoehler
leJOS Team Member
 
Posts: 1413
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

more stuff