Introduction

I wanted to remote control my Lego Mindstorms NXT robot from a program written in C#. Having found very little useful information on Google, I decided to write my own program to see how the protocol used by Lego works.

The program I wrote (see illustration above) is only for experimental purposes. It allows retrieving the NXT version, the NXT name, to read mailboxes content and to write data into NXT mailboxes. All the data sent to and from the NXT are displayed in hexadecimal.

Using the Code

To use the program, you must first know which COM port your PC uses to communicate by Bluetooth with the NXT brick. The best way to know is to open the Bluetooth Control Panel and to consult the services associated with the NXT brick.

Once the COM port is known, select it and press the Connect button. If everything goes right and you get access to the COM port, all the other buttons become active. Otherwise, the program crashes. The next thing to do is to try the communication. The simplest command to send the NXT is Get Version. This will always work, whatever the program running on the NXT brick.

To try the mailboxes communication, I have written a Lego Mindstorm program, mailboxTest.rbt, that writes in output mailbox1 the number received on input mailbox1, increased by one. This allows me to test the in and out mailbox communication.

Note that to write to the PC, i.e. the master, the NXT must use communication channel ZERO.

Points of Interest

Communicating with the NXT via Bluetooth

Hint 1: I confirm what many other people have reported: Bluetooth communication with Lego NXT is very sensitive to the Bluetooth drivers used. Most of the time, it is strongly recommended to NOT install -- or if they are already installed, to uninstall -- the drivers provided by the Bluetooth adapter manufacturer and to use the Microsoft Bluetooth default drivers instead.

Hint 2: As long as the Lego Mindstorms NXT development environment is running, if it is configured to download programs to the NXT brick by Bluetooth, no other program is allowed to access the NXT brick via Bluetooth. That's why I always prefer to download the NXT programs by USB to leave the Bluetooth channel useable for my applications.

Sending Messages on Bluetooth

The communication via Bluetooth with the NXT brick is very easy. It is a simple COM port and what is great is that you only have to open the COM port; no baud rate or parity settings are needed. The .NET 2.0 Framework provides a useful class, system.io.ports.serialport, that completely handles the COM port. All you have to do is to select a COM port, open the port and read or write the bytes.

Understanding the Protocol

Since the PC is the master, each communication will be started by a request from the PC and followed by a reply from the NXT brick. Each message begins with 2 bytes giving the message length (warning: the Least Significant Byte is sent first). Since my messages are always shorter than 256 bytes, I set the Most Significant Byte to zero.