Communicating with LEGO NXT via Bluetooth in Delphi

Hi,
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.
falthornAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

const COMMAND = 'DUNNO';
var Data: string;
   DataLength: integer;
begin
 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;
end;
 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
falthornAuthor Commented:
Hi MerijnB,

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

Thank you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Wireless Networking

From novice to tech pro — start learning today.