Communicating with LEGO NXT via Bluetooth in Delphi

Posted on 2007-08-10
Last Modified: 2016-05-20
I recently bought myself a Lego Mindstorm Robotics Kit.  The Kit comes with a unit called the NXT Brick.  This brick can be programmed using virtually any language using either Bluetooth or USB.  

As a project I have decided to try and connect Delphi to the NXT Brick.  As I have only started learning delphi, I am somewhat unfamiliar with how Pascal/Delphi send bytes of data.  So far I can get Delphi to connect to the NXT brick by simulating a COM port, but I am stuck as how to send data to the brick.

I am using the Synaser (Serial Port Synchronous) Library to get the PC to connect to the NXT Brick via bluetooth.

To send data to the NXT Brick using Perl, I would the following:

my $data = "\x00\x01\x88";

$data = pack("h2", length($data) ) . "\x00" . $data;

$nxt->write( $data );

Open in new window

What the NXT Brick is looking for, if using Bluetooth is:

Byte 0: 0x00 - Length, LSB
Byte 1: 0x00 - Length, MSB
Byte 2: 0x01 - Command Type
Byte 3: 0x88 - Command

Note: All those commands must be prefixed by a 2 bytes header indicating the command length

So, can you please help me work out how to send and receive data to and from the NXT Brick.  Any guidance will be most appreciated.
Question by:falthorn
    LVL 19

    Accepted Solution

    I'm not sure if I understand the format completely, but here is an example:

    const COMMAND = 'DUNNO';
    var Data: string;
       DataLength: integer;
     Data := 'h2';   // some sort of header? stole from your example
     DataLength := Length(COMMAND);

     // first add the LSB and MSB of the length
     Data := Data + Chr(LoByte(DataLength)) + Chr(HiByte(DataLength));
     // now add the 0 and the actual command
     Data := Data + Chr(0) + COMMAND;

    Author Comment

    Hi MerijnB,

    I tried the above, with a few minor changes and it worked a treat.

    Thank you for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Using in-flight Wi-Fi when you travel? Business travelers beware! In-flight Wi-Fi networks could rip the door right off your digital privacy portal. That’s no joke either, as it might also provide a convenient entrance for bad threat actors.
    This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
    Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now