Robot App Store

image description

Robopedia

How to send message from NXT to an external device


Question


I am using the NXT to control an Arduino based robot.
I am able to receive commands on the Arduino computer from NXT via bluetooth.
I am trying to understand the commands sent from the NXT

I have been reading the “Programming LEGO NXT” section of RobotAppStore’s knowledge base and have learned a lot – it was great.

Do you know if there is a source to understand what I am receiving?

My issue (if you will indulge me) is that I have captured via Tera Term (terminal SW) what is sent by the NXT but can’t sort it out (after spending much time reading your blog and Lego docs)
I simply have the NXT send the number ‘1’ as a [number] using the Send message block and I receive at my Arduino:

9 0 128 9 0 5 0 0 [128 63] 0

The number in brackets seem to reflect the number ‘1’ because when I increase to ‘2’, ‘3’ and so on up to ‘8’ only these numbers change otherwise the sequence remains the same (see below).

128 63 – 1
0 64 - 2
64 64 – 3
128 64 – 4
160 64 – 5
192 64 – 6

Based on your web site I assume the second number is the MSB and the first LSB (convert to HEX) – but the numbers don’t increase linearly or by some fixed multiple.
Any other direction or pointers would be greatly appreciated.

Answer


The following is the definition of the command bytes:
9 0 128 9 0 5 0 0 [128 63] 0

9 0 – Size of the command. LSB first, that means 09 -> 9 bytes (128, 9, 0, 5, 0, 0, 128, 63, 0);

128- Command type. 128 converted to hex -> 0x80. 0x80 means “Direct command” without the reply message. You may use this command with a reply message to get a feedback on a success/failure (change 0x80 byte to-> 0x00) but it will slow down the connection a bit. I wouldn’t recommend that;

9- Command name. 9-> 0x09 means “Message Write”. In case you would like to communicate from Arduino to NXT, just change the command name byte to 0x13 to “Message Read” (command’s parameter bytes should be changed as well);

0- Mailbox number. NXT may use up to 10 communication channels (0 till 9). The number is actually a zero-base index in the mailbox array. If you are using the official LEGO mindstorms NXT software, you would probably notice that the mailbox number of the “Send message” block start from 1 till 10. The software translates the input number to a zero base index (input number minus 1);

5- Size of the message. In this case only one byte is used to store the message size. 5 bytes (0, 0, 128, 63, 0). Pay attention to the limitation of 64 bytes per command. Since we’ve already used 6 bytes (including this byte as well) for the command set up, it leaves us maximum of 58 bytes for the message itself.

0 0 128 63 0- The message data. The original message data string is converted to ASCII encoded bytes that are passed to the command “as is” (no “LSB first” manipulations). Please pay attention to the last byte 0. 0-> 0x00- is the “null terminator” that must be added to the end of every command data string parameter. This rule is consistent to all NXT’s commands. In this case the actual data is 0 0 128 63 which makes completely no sense :)

The C# command that encodes and converts string data to ASCII byte array is
System.Text.Encoding.ASCII.GetBytes(message);
Similar command in Java is:
data.getBytes("ASCII");

If you are using the G-NXT official software to program NXT, simply send the number as [text].
For the number ‘1’ as text you will receive the following output:
6 0 128 9 0 2 49 0

Explanation:
60 (or 06) – 6 Bytes
128 (80 HEX) – ‘Direct Command’
9 – Write
0 – Mailbox
2 – Message length = 2B
49 – ASCII – number one (YEAH!!!)
0 – Null (end of message)
read here cheats why men cheat on beautiful women
my wife cheated now what husbands that cheat looking for affair
signs of a cheater website married men that cheat
gabapentin and ic gabapentin and ic gabapentin and ic



Related Robopedia portals

Find out further reading in the following topic-based portals.
[Add or Edit Article]