Finally I found some time to play with my NXT set, and to learn some java.
I choice lejos as best solution for this.
Hearing a lot of informations of Curiosity rover, I decided to build my own room rover, instead of mars rover;)
The first issue is communication between NXT brick and my PC. Of course it must be wireless, so Bluetooth is the only option.
I paired NXT with my PC, everythink seems to be working, I can upload program over BT. But, when I checking paired devices on NXT, the list is empty - it's really strange.
Is it a normal behavior?
I'm not sure, but I suppose that it can be connected with my second problem.
I tried many times to send data between NXT and PC, and all my trial fails.
I enclose source code below.
NXT CODE:
- Code: Select all
import java.io.DataInputStream;
import java.io.IOException;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
public class Start {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LCD.clear();
LCD.drawString("Waiting...", 0, 0);
BTConnection btc = Bluetooth.waitForConnection();
LCD.drawString("Connected...", 0, 0);
DataInputStream iStream = btc.openDataInputStream();
if(iStream != null)
{
LCD.drawString("W4 Data...", 0, 0);
int DataFromMC = 0;
int counter = 1;
while(DataFromMC != -1)
{
LCD.drawInt(DataFromMC, 0, counter);
LCD.refresh();
counter++;
if(counter > 3) counter = 1;
try {
LCD.drawString("Before read", 0, 0);
DataFromMC = iStream.readInt();
LCD.drawString("After read", 0, 0);
} catch (IOException ioe)
{
LCD.drawString("Read excepion", 0, 0);
}
}
LCD.drawString("Disconnecting", 0, 0);
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
LCD.drawString("CCStream", 0, 0);
}
btc.close();
LCD.drawString("Finishing;)...", 0, 0);
while(Button.ENTER.isUp());
}
}
PC CODE:
- Code: Select all
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import lejos.pc.comm.NXTConnector;
public class Start {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Temporary for test
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
NXTConnector conn = new NXTConnector();
System.out.println("Connecting");
if(conn.connectTo())
{
System.out.println("Output stream creating");
DataOutputStream oStream = new DataOutputStream(conn.getOutputStream());
System.out.println("Type integer (-1) for end");
int x = 0;
while(x != -1)
{
try {
x = Integer.parseInt(br.readLine());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
oStream.writeInt(12);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//oStream.flush();
}
try {
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Behavior:
After program runs, I see "Waiting".
After PC part runs, I see "Before read" and 0 i next line on NXT,
and,
on PC.Connecting
BlueCove version 2.1.0 on winsock
Output stream creating
Type integer (-1) for end
When i type something on PC, next line appears and I can type next number, and so on.
Unfortunately on NXT i see always the same "Before read" and 0 in second line.
Do you have any ideas?
Some information's:
PC with Windows 7 32bit
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
lejos 0.9.1 (rev.6595)
Regards
Przemek.
