Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Communicating with LEGO NXT via Bluetooth in C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (13 votes)
19 May 2007CPOL4 min read 245.6K   5.9K   60   22
Shows how to communicate via Bluetooth with a Lego Mindstorms NXT robot using C#
Screenshot - nxtBlueTooth1.jpg

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.

Screenshot - nxtBlueTooth2.jpg

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.

Screenshot - nxtBlueTooth3.jpg

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.

C#
SerialPort BluetoothConnection= new SerialPort(); 
BluetoothConnection.PortName = "COM7"; 
// Replace this COM port by the appropriate one on your computer
BluetoothConnection.Open(); 
BluetoothConnection.ReadTimeout = 1500; 
BluetoothConnection.Write(Command, 0, Command.Length); 
for(int i=0;i<ReplyLength ;i++) 
{ 
    textBox2.Text+=BluetoothConnection.ReadByte().ToString("X2"); 
} 
BluetoothConnection.Close(); 

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.

To Send a Message

C#
// Declare a 2 bytes vector to store the message length header 
Byte[] MessageLength= {0x00, 0x00}; 

//set the LSB to the length of the message 
MessageLength[0]=(byte)Command.Length; 

//send the 2 bytes header 
BluetoothConnection.Write(MessageLength, 0, MessageLength.Length); 

// send the message itself 
BluetoothConnection.Write(Command, 0, Command.Length); 

To Retrieve the Reply

C#
// retrieve the reply length 
int length = 
    BluetoothConnection.ReadByte() + 256 * BluetoothConnection.ReadByte(); 

// retrieve the reply data 
for(int i=0;i<length ;i++) 
{
    textBox2.Text+=BluetoothConnection.ReadByte().ToString("X2")+" "; 
} 

Example of Commands for the NXT Brick

Note: All those commands must be prefixed by a 2-byte header indicating the command length as explained above.

Get Version

Byte 2: 0x01 
Byte 3: 0x88 

This very simple command returns the NXT Version. This is useful to check that the communication between the PC an the NXT brick is working OK.

Get Info

Byte 2: 0x01 
Byte 3: 0x9B 

This command is very similar to the Get Version command, but it gives other information, e.g. the NXT name.

Read Mailbox

Byte 2: 0x00 
Byte 3: 0x13 
Byte 4: Mailbox number+9 (e.g. for mailbox 1 use 0x0A) 
Byte 5: 0x00 
Byte 6: 0x01 you can use 0x00 instead to leave the byte in the mailbox 

The NXT reply to this command is the mailbox content or an error message if the mailbox is empty.

Write a Boolean in a Mailbox

Byte 2: 0x00 
Byte 3: 0x09 
Byte 4: Mailbox number -1 (e.g. for mailbox 1 use Ox00 
Byte 5: 0x02 (this is the datalength: 1 byte + a 1 byte terminator) 
Byte 6: The Data to write (either 0X00 or 0X01) 
Byte 7: 0x00 (terminator) 

The NXT reply to this command is either OK or an error message, e.g. if no NXT program is running.

Write a 32-bit Integer in a Mailbox

Byte 2: 0x00 
Byte 3: 0x09 
Byte 4: Mailbox number -1 (e.g. for mailbox 1 use Ox00 
Byte 5: 0x05 (this is the datalength: 1 byte + a 1 byte terminator) 
Byte 6: Data byte 0 (the LSB) 
Byte 7: Data byte 1 
Byte 8: Data byte 2 
Byte 9: Data byte 3 (the MSB) 
Byte 10: 0x00 (terminator) 

The NXT reply to this command is either OK or an error message, e.g. if no NXT program is running.

Resources

All the information needed to communicate by Bluetooth with the Lego NXT, including the full list of commands, is available on the Lego website in the documentation Bluetooth Developer Kit (BDK).

History

  • NXTBlueToothTester v0.2: 19th May, 2007

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaltest Pin
andy pol25-Mar-11 20:53
andy pol25-Mar-11 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.