I am trying a simple communication between PC and NXT, where each of them sends messages to the other.
What I have is:
a) on the NXT there is a NXC program running that reads messages from a mailbox and send messages to another mailbox. The code is taken from Daniele Benedetelli's NXC Tutorial and the relevant part is here.
- Code: Select all
task main(){
string in, out, iStr;
int i = 0;
while(true){
iStr = NumToStr(i);
out = StrCat("S",iStr);
TextOut(10,LCD_LINE1,"Slave Test");
TextOut(0,LCD_LINE2,"IN:");
TextOut(0,LCD_LINE4,"OUT:");
ReceiveRemoteString(5, true, in);
SendResponseString(1,out);
TextOut(10,LCD_LINE3,in);
TextOut(10,LCD_LINE5,out);
Wait(100);
i++;
}
}
b) Since I already tried the samples coming with the iCommand 0.7 release and they worked great, I thought I should try to write the PC program using iCommand but even if the PC and NXT connect fine nothing was received by either one. The relevant portion of code is:
- Code: Select all
NXTCommand.open();
NXTCommand.setVerify(true);
NXTCommand nxtComm = NXTCommand.getSingleton();
// send command to NXT
byte[] msgOut = new byte[1];
for (byte i = 0; i < 10; i++) {
msgOut[0] = i;
printByteMessage("Master sending...", msgOut);
nxtComm.messageWrite(msgOut, OUTBOX);
byte[] msgIn = nxtComm.messageRead(INBOX, (byte) 0, true);
printByteMessage("Received", msgIn);
}
System.out.println("Closing shop");
NXTCommand.close();
c) Since this didn't work and because I've seen in this forum that most of the recommendations are to use leJOS and not iCommand, I tried to do this. I used the BTSend sample:
- Code: Select all
NXTConnector conn = new NXTConnector();
// Connect to any NXT over Bluetooth
boolean connected = conn.connectTo("btspp://", NXTComm.LCP);
if (!connected) {
System.err.println("Failed to connect to any NXT");
System.exit(1);
}
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
for(int i=0;i<100;i++) {
try {
System.out.println("Sending " + (i*30000));
dos.writeInt((i*30000));
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
break;
}
try {
System.out.println("Received " + dis.readInt());
} catch (IOException ioe) {
System.out.println("IO Exception reading bytes:");
System.out.println(ioe.getMessage());
break;
}
}
but again without any success: the PC and NXT connect but they don't receive what the other is sending.
Can you please help me understand:
1. What I'm doing wrong
2. How to send a simple message from PC to the NXT (running the NXC) program and the other way.
Thanks a lot in advance for all the help! I really think iCommand/leJOS are great and want to learn to use them.
