As part of Enchanting, a program to help kids program their NXT, a user connects and NXT to their computer, and the software recognizes it, and they program it and might remove the NXT (or it could go to sleep), while they continue to code (and the cycle repeats). It is expected that the code editor will deal gracefully with the NXT connecting or disconnecting.
I'm using code that reads messages from the NXT (and acts upon them), and, when it gets an exception, it knows that the NXT has disconnected. This worked fine using leJOS 0.9.0, but I am having difficulties doing so with leJOS 0.9.1.
While I may well be doing things "wrong", and would be happy to have an alternative method, here is some shortened (but compiling code) that illustrates the issues, followed by sample output. Note that this code runs on the computer, and doesn't care about which firmware is on the NXT. Also note that you do need to change a couple of lines with '///', based on the comments, to make it compile for leJOS 0.9.0 or 0.9.1.
- Code: Select all
import java.io.*;
import lejos.nxt.remote.*;
import lejos.pc.comm.*;
// The purpose of this application is to test when the NXT is connected or disconnected
public class ConnectionTest {
private String nxtname = "";
private String address = "";
private int protocols = NXTCommFactory.USB; // | NXTCommFactory.BLUETOOTH;
/// private NXTCommand nxtcommand = new NXTCommand(); /// use in leJOS 0.9.0
private NXTCommand nxtcommand; /// use in leJOS 0.9.1
private NXTConnector nxtconnector = new NXTConnector();
public void delay(int ms) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// we are unlikely to be interrupted
throw new RuntimeException(e);
}
}
ConnectionTest() {
while (true) {
// Connect to an NXT
boolean connected = false;
System.out.print("Connecting");
while (!connected) {
delay(500);
System.out.print(".");
try {
connected = nxtconnector.connectTo(nxtname, address, protocols);
} catch (java.lang.RuntimeException e) {
System.out.println("\nRuntimeException occurred while trying to connect to NXT: " + e.getMessage());
}
}
System.out.println(" Connected.");
/// nxtcommand.setNXTComm(nxtconnector.getNXTComm()); /// use in leJOS 0.9.0
nxtcommand = new NXTCommand(nxtconnector.getNXTComm()); /// use in leJOS 0.9.1
// Communicate with the NXT until we are disconnected
try {
System.out.print("Reading data: ");
while (true) {
byte[] message = nxtcommand.messageRead((byte) 0, (byte) 0, true);
System.out.printf("%d, ", message.length);
delay(500);
}
} catch (IOException e) {
System.out.println("\nIOException occurred while reading messages: " + e.getMessage());
}
System.out.println("\nDisconnected.");
}
}
public static void main(String[] args) {
new ConnectionTest();
}
}
After setting environment variables, I compile and run this program thusly:
- Code: Select all
nxjpcc ConnectionTest.java && nxjpc ConnectionTest
Here are the results (which I expect) using leJOS 0.9.0:
(Note that the $ indicates the bash prompt on my OS computer, and ^C appears when I press control-C).
- Code: Select all
$ nxjpcc ConnectionTest.java && nxjpc ConnectionTest
Connecting....................Found NXT: NXT 00165313BBE5
Connected.
Reading data: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
IOException occurred while reading messages: Failed to send data
Disconnected.
Connecting.......................Found NXT: NXT 00165313BBE5
Connected.
Reading data: 0, 0, 0, 0, 0,
IOException occurred while reading messages: Failed to send data
Disconnected.
Connecting.............Found NXT: NXT 00165313BBE5
Connected.
Reading data: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
IOException occurred while reading messages: Failed to send data
Disconnected.
Connecting...^C$
I connect and disconnect several times, and everything works well.
Here is what happens using leJOS 0.9.1:
- Code: Select all
$ nxjpcc ConnectionTest.java && nxjpc ConnectionTest
Connecting.................Found NXT: NXT 00165313BBE5
Connected.
Reading data: 0, 0, 0, 0, 0, 0, ^C$
I finally press control-C to quit the application after it, well, stopped responding for many seconds. (I've let this run for a long time and never seen it start to respond again). Also, the 'java' process on my computer starts using 99% of the CPU while this is blocking, and of course goes away when I quit the program.
If I adjust the code slightly to only use bluetooth connections, and pair my device to my computer so they'll work, this is what I get with leJOS 0.9.0:
- Code: Select all
$ nxjpcc ConnectionTest.java && nxjpc ConnectionTest
Connecting.BlueCove version 2.1.0 on mac
. Connected.
Reading data: 0, 0, 0, 0, 0,
IOException occurred while reading messages: End of file
Disconnected.
Connecting. Connected.
Reading data: 0, 0, 0, 0, 0, 0, 0, 0, 0,
IOException occurred while reading messages: Failed to write [0xe00002d7]
Disconnected.
Connecting.^CBlueCove stack shutdown completed
$
It takes several seconds to connect, and must be fifteen seconds or so before it recognizes that I've turned off my NXT, but everything works right.
Here's what I get using bluetooth with leJOS 0.9.1:
- Code: Select all
$ nxjpcc ConnectionTest.java && nxjpc ConnectionTest
Connecting.BlueCove version 2.1.0 on mac
Connected.
Reading data: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
IOException occurred while reading messages: Failed to write [0xe00002d7]
Disconnected.
Connecting.
RuntimeException occurred while trying to connect to NXT: already connected
.
RuntimeException occurred while trying to connect to NXT: already connected
.
RuntimeException occurred while trying to connect to NXT: already connected
.
RuntimeException occurred while trying to connect to NXT: already connected
.
RuntimeException occurred while trying to connect to NXT: already connected
.
It went on and on, repeating those last two lines of output, and never did reconnect.
Is there something I should be doing differently, to know when the NXT has disconnected, and to reconnect to it when it becomes available again?
Thank you,
Clinton Blackmore
