Click here to Skip to main content
Click here to Skip to main content

Communicating with LEGO NXT via Bluetooth in C#

, 19 May 2007
Rate this:
Please Sign up or sign in to vote.
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.

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

// 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

// 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)

About the Author

pierre poliakoff
Team Leader
Belgium Belgium
No Biography provided

Comments and Discussions

 
QuestionREAD MESSAGE DOESN'T WORKS PinmemberMember 1013221629-Jun-13 10:02 
Questionhelp me Pinmemberforwhatreason17-Nov-12 15:46 
QuestionCOLOR SENSOR PinmemberClayton Barão26-Jan-12 11:09 
Questionint32 encoding Pinmemberiwieiwas30-Jul-11 3:55 
GeneralMy vote of 3 PinmemberNima Heydarzadeh17-Jul-11 4:39 
Generaltest Pinmemberandy pol25-Mar-11 20:53 
Generaltest Pinmemberandy pol25-Mar-11 20:52 
GeneralNXT lamp On - Off Pinmembertheo927311-Oct-10 2:48 
GeneralPLEASE PLEASE HELP Pinmembersmoothik2-Feb-10 21:19 
AnswerRe: PLEASE PLEASE HELP Pinmemberpierre poliakoff3-Feb-10 20:49 
GeneralRe: PLEASE PLEASE HELP PinmemberHextor6663-Jun-10 2:07 
GeneralHelpme please Pinmemberhendryck1-Nov-08 12:52 
QuestionRecord Microphone input Pinmemberpankaj motiyar15-Jul-08 1:58 
QuestionHow to get mailboxTest.rbt? Pinmemberchak05283-Sep-07 17:57 
GeneralMS Robotics Studio PinmemberAC223-May-07 4:36 
GeneralMindsqualls.net Pinmemberadb77722-May-07 13:35 
I was in a similar situation...wanting to use c# to control my nxt.
 
I settled on the mindsqualls .net api - take a gander: http://www.mindsqualls.net
 
Cheers!
aaron backer
http://legodefenseindustries.blogspot.com
http://www.itgroundhog.com

NewsNXT# Pinmembervassalli22-May-07 12:51 
GeneralNice Pinmemberyojiq20-May-07 10:07 
GeneralRe: Nice Pinmemberglauben21-May-07 17:16 
GeneralRe: Nice Pinmemberyojiq21-May-07 21:40 
GeneralRe: Nice PinmemberMember 937812123-Aug-12 10:21 
Generalinteresting PinmemberSacha Barber19-May-07 20:31 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 May 2007
Article Copyright 2007 by pierre poliakoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid