Communicating with LEGO NXT via Bluetooth in Delphi

falthorn used Ask the Experts™
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.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. Software Engineer
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;


Hi MerijnB,

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

Thank you for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial