[nxc] bluetooth java app to nxt

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
darkkor
Posts: 4
Joined: 17 Sep 2011, 10:51

[nxc] bluetooth java app to nxt

Post by darkkor »

Hi,

i'm trying t create a java application that send a message from my computer to the NXT brick. I'm using the bluecove library and writing in nxc the NXT program to read this message. There's no problem sending messages from the brick to the computer, but i can't read anything in the brick. I'm not sure why. Any help?

Thanks!!
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [nxc] bluetooth java app to nxt

Post by afanofosc »

I can help but I need a lot more information. What is the NXC code that you are currently using? What do you mean by "I can't read anything in the brick"? When you send messages from the PC to the brick are you strictly following the published specifications for the direct command protocol over bluetooth? What sort of message are you sending to the brick? If it is the MessageWrite direct command then what is the mailbox number you are using? Is your message null terminated?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
darkkor
Posts: 4
Joined: 17 Sep 2011, 10:51

Re: [nxc] bluetooth java app to nxt

Post by darkkor »

Ok, thanks for all.

i think the problem is i'm not using the protocol in the right way. I'm not sure what i have to send to the NXT.
I'm using this NXC code to receive messages:

Code: Select all

string BTReceiveMessage(byte connection, byte mailbox, byte flush){ //receive a BT message
   string msg;
   BTWait(connection);
   int a=   ReceiveMessage(mailbox,flush,msg);
   NumOut( 0 , LCD_LINE4, a);
   BTWait(connection);
   return msg;
}
i've tried to use diferent mailbox, 1, 0x01, also use this function insede a for to read all the mailboxes.

In the Java side the code is simple i use an outputStrem and write directly the byte sequence. This is the last sequence I tried:
0x80, 0x09, 0x01, 0x05, 0xEA, 0x01, 0x00

Any advice?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [nxc] bluetooth java app to nxt

Post by afanofosc »

In the Java side the code is simple i use an outputStrem and write directly the byte sequence. This is the last sequence I tried:
0x80, 0x09, 0x01, 0x05, 0xEA, 0x01, 0x00
So with Bluetooth you need 2 bytes at the start that are the full size of the message. This is documented in the LEGO BT SDK. Please read that. Mailbox 1 is actually number 0 so make sure you are reading/writing the same mailbox. If you use MAILBOX1 in your NXC code then you need to write to mailbox 0 in your Java code. Then if you say that you are writing 5 bytes you really need to write 5 more bytes in your message (i.e., 2 more than you have in your example). The docs say the length bytes are lsb/msb so it would be 0x09, 0x00 followed by MessageWrite direct command bytes.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
darkkor
Posts: 4
Joined: 17 Sep 2011, 10:51

Re: [nxc] bluetooth java app to nxt

Post by darkkor »

Ok I see. I read how the protocol works and with some example I found on the net I make it works :D The java application send a message and the NXT print it on the screen.

But something strange happens. The first message received with the instruction ReceiveMessage is empty. However, with the strLen instruction it seems to have 64 characters. It's not a big issue but i would like to know why it's happening. I'm still testing and improving the code. Any idea is welcome.

And thanks for all!!
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [nxc] bluetooth java app to nxt

Post by afanofosc »

From c_cmd.h:

Code: Select all

//MAX_MESSAGE_SIZE including null-terminator
//!!! Capped at 59 unless USB protocol assumptions are changed!
//
#define MAX_MESSAGE_SIZE    59
And in c_cmd.c where it handles the MessageWrite direct command:

Code: Select all

        //If Count is illegal or MsgData is not null-terminated,
        // we can't accept it as a valid string
        if (Count == 0 || Count > MAX_MESSAGE_SIZE || pData[Count - 1] != 0x00)
        {
          RCStatus = ERR_RC_ILLEGAL_VAL;
          break;
        }
If you were checking the response for the direct command you would see

Code: Select all

#define ERR_RC_ILLEGAL_VAL -64 //0xC0 Data contains out-of-range values
returned for the status.

So the maximum strlen value is 58 since 59 includes the null at the end and strlen does not count the null in the string length.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
darkkor
Posts: 4
Joined: 17 Sep 2011, 10:51

Re: [nxc] bluetooth java app to nxt

Post by darkkor »

Yes, i thougth it has to be something like that, i will ensure the length is not out of range and try it again.

Thanks for all, you are a true Master!! ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests