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
