Click here to Skip to main content
14,874,239 members
Articles / Programming Languages / C#
Posted 19 May 2007


60 bookmarked

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
Shows how to communicate via Bluetooth with a Lego Mindstorms NXT robot using C#
Screenshot - nxtBlueTooth1.jpg


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,, 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.ReadTimeout = 1500; 
BluetoothConnection.Write(Command, 0, Command.Length); 
for(int i=0;i<ReplyLength ;i++) 

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 

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


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


  • NXTBlueToothTester v0.2: 19th May, 2007


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

Member 1013221629-Jun-13 10:02
MemberMember 1013221629-Jun-13 10:02 
Questionhelp me Pin
forwhatreason17-Nov-12 15:46
Memberforwhatreason17-Nov-12 15:46 
Clayton Barão26-Jan-12 11:09
MemberClayton Barão26-Jan-12 11:09 
Questionint32 encoding Pin
iwieiwas30-Jul-11 3:55
Memberiwieiwas30-Jul-11 3:55 
GeneralMy vote of 3 Pin
Nima Heydarzadeh17-Jul-11 4:39
MemberNima Heydarzadeh17-Jul-11 4:39 
Generaltest Pin
andy pol25-Mar-11 20:53
Memberandy pol25-Mar-11 20:53 
Generaltest Pin
andy pol25-Mar-11 20:52
Memberandy pol25-Mar-11 20:52 
GeneralNXT lamp On - Off Pin
theo927311-Oct-10 2:48
Membertheo927311-Oct-10 2:48 
smoothik2-Feb-10 21:19
Membersmoothik2-Feb-10 21:19 
pierre poliakoff3-Feb-10 20:49
Memberpierre poliakoff3-Feb-10 20:49 
Hextor6663-Jun-10 2:07
MemberHextor6663-Jun-10 2:07 
GeneralHelpme please Pin
hendryck1-Nov-08 12:52
Memberhendryck1-Nov-08 12:52 
QuestionRecord Microphone input Pin
pankaj motiyar15-Jul-08 1:58
Memberpankaj motiyar15-Jul-08 1:58 
QuestionHow to get mailboxTest.rbt? Pin
chak05283-Sep-07 17:57
Memberchak05283-Sep-07 17:57 
GeneralMS Robotics Studio Pin
AC223-May-07 4:36
MemberAC223-May-07 4:36 Pin
adb77722-May-07 13:35
Memberadb77722-May-07 13:35 
NewsNXT# Pin
vassalli22-May-07 12:51
Membervassalli22-May-07 12:51 
GeneralNice Pin
yojiq20-May-07 10:07
Memberyojiq20-May-07 10:07 
GeneralRe: Nice Pin
glauben21-May-07 17:16
Memberglauben21-May-07 17:16 
GeneralRe: Nice Pin
yojiq21-May-07 21:40
Memberyojiq21-May-07 21:40 
GeneralRe: Nice Pin
Member 937812123-Aug-12 10:21
MemberMember 937812123-Aug-12 10:21 
Generalinteresting Pin
Sacha Barber19-May-07 20:31
mvaSacha Barber19-May-07 20:31 

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.