Exception closing the conexion with the NXJ

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Exception closing the conexion with the NXJ

Postby xelas1981 » Thu Mar 29, 2012 9:44 am

Hi,

I'm writting a code that comunicates the PC with the NXJ, more or lees is going well, but when I'm trying to desconnect the PC from the NXJ, it's giving me an exception on the NXJ

Exception:84
at: 191:17
at: 192:2
at: 39:60

I reduce the code a lot to show it to you, so basically, on the PC you will see a Frame with a Button, if you press it once, it will connect, and if you press it again it will disconnect. On the NXJ side, it's just waiting for the connection, and reading the information that it sends the PC, until it get the order, and close the connection, and here is when I get the error

This is the code for the PC
Code: Select all
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;


public class PC_to_NXT {   
   
   public static void main(String[] args) {
      
      CMD_Connect.setSize(30, 500);
      LAB_Connect.setForeground(Color.RED);
      LAB_Connect.setBorder(LineBorder.createGrayLineBorder());
      
      CMD_Layout = new GroupLayout(CMD_Panel);
      CMD_Panel.setLayout(CMD_Layout);
      CMD_Layout.setAutoCreateGaps(true);
      CMD_Layout.setAutoCreateContainerGaps(true);
      CMD_Layout.setHorizontalGroup(
            CMD_Layout.createSequentialGroup()
            .addGroup(CMD_Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
               .addComponent(CMD_Connect,200,200, 200)
               .addComponent(CMD_Prueba,200,200, 200))
            .addGroup(CMD_Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
               .addComponent(LAB_Connect,200,200, 200)
               .addComponent(CMD_Prueba2,200,200, 200))
            );
      CMD_Layout.setVerticalGroup(
            CMD_Layout.createSequentialGroup()
            .addGroup(CMD_Layout.createParallelGroup(GroupLayout.Alignment.CENTER, false)
                .addComponent(CMD_Connect,30,30, 30)
                .addComponent(LAB_Connect,30,30, 30))
            .addGroup(CMD_Layout.createParallelGroup(GroupLayout.Alignment.BASELINE, false)
                .addComponent(CMD_Prueba,30,30, 30)
                .addComponent(CMD_Prueba2,30,30, 30))
               
            );
      
      
      Frame.add(CMD_Panel,BorderLayout.SOUTH);
      
      
      
      CMD_Connect.addActionListener(new ActionListener() {
         
            public void actionPerformed(ActionEvent e)
            {
                if (CMD_Connect.getText().equals("Connect"))
                {
                   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("btspp://");
                     
                  if (!connected) {
                     System.err.println("Failed to connect to any NXT");
                     System.exit(1);
                  }
                  CMD_Connect.setText("Disconnect");
                  LAB_Connect.setText("Connected to the NXT");
                  LAB_Connect.setForeground(Color.green);
                }
                else
                {
                   try {
                      CMD_Connect.setText("Connect");
                      LAB_Connect.setText("Ready to connect");
                      LAB_Connect.setForeground(Color.RED);
                      Output = new DataOutputStream(conn.getOutputStream());
                      Output.writeInt(10);
                      Output.flush();
                     Output.close();
                     conn.close();
                  } catch (IOException ioe) {
                     System.out.println("IOException closing connection:");
                     System.out.println(ioe.getMessage());
                  }
                }
            }
        });
      
      
      Frame.setSize(600, 600);
      Frame.setLocationRelativeTo(null);
      Frame.setVisible(true);
      
      
      
      
   }
   
   public static NXTConnector conn;
   public static DataInputStream Input;
   public static DataOutputStream Output;
   
   public static JPanel CMD_Panel=new JPanel();
   public static JFrame Frame=new JFrame();
   public static GroupLayout CMD_Layout;
   
   public static JButton CMD_Connect = new JButton("Connect");
   public static JButton CMD_Prueba = new JButton("Prueba");
   public static JButton CMD_Prueba2 = new JButton("Prueba2");
   public static JLabel LAB_Connect = new JLabel("Ready to connect", JLabel.CENTER);
}


This is one for the NXJ
Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.LCD;
import lejos.nxt.NXT;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;


public class NXT_to_PC {

   public static void main(String [] args)  throws Exception
   {
        LCD.drawString("Waiting...",0,0);
      LCD.refresh();

       btc = Bluetooth.waitForConnection();
          
      LCD.clear();
      LCD.drawString("Connected",0,0);
      LCD.refresh();   

      Input = btc.openDataInputStream();
      Output = btc.openDataOutputStream();
      OptionFromPC = Input.readInt();
      do{
         OptionFromPC = Input.readInt();
      }while(OptionFromPC!=10);
      
      
         LCD.drawString("Closing...    ", 0, 0);
         LCD.refresh();
         Input.close();
         Output.close();
         btc.close();
      
         
         
         
      LCD.clear();
      LCD.drawString("Finished",3, 4);
      LCD.refresh();
      Thread.sleep(2000);
      
   }
   
   public static BTConnection btc;
   public static DataInputStream Input;
   public static DataOutputStream Output;
   
   public static int OptionFromPC;
}


Thanks a lot in advance

Alex
xelas1981
New User
 
Posts: 5
Joined: Mon Feb 27, 2012 7:08 pm

Re: Exception closing the conexion with the NXJ

Postby skoehler » Thu Mar 29, 2012 10:19 am

Please link verbose and copy/paste the output or use nxjdebugtool to deocode the stacktrace of the exception. See the tutorial for details and examples.
It should tell us, where the exception happened.

I assume you're using leJOS 0.9.1?
skoehler
leJOS Team Member
 
Posts: 1110
Joined: Thu Oct 30, 2008 4:54 pm

Re: Exception closing the conexion with the NXJ

Postby gloomyandy » Thu Mar 29, 2012 11:09 am

Your PC program only seems to write a single int (of value 10), but your NXT program will always try and read two ints, one with the read before the do while loop and one inside the loop. This means that the second read will be aborted by an EOFException when your pc program closes the connection after writing 10... So you need to change your code either get rid of the first read on the NXT side or add a write on the pc side...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3003
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Exception closing the conexion with the NXJ

Postby xelas1981 » Thu Mar 29, 2012 11:39 am

Thanks for yor answer gloomyandy, I didn't realice about that
xelas1981
New User
 
Posts: 5
Joined: Mon Feb 27, 2012 7:08 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff