Page 1 of 1
Send/Receive Number messages using Bluetooth
Posted: 09 Aug 2011, 14:56
by aswinn01
I am wring a simple Java application to remote control a robot.
I cannot decipher NXT Bluetooth Number messages i.e. I don't know how to read 4 byte integer (2 is represented as 0x00 0x00 0x00 0x40, 3 is 0x00 0x00 0x40 0x40 etc.)
Can anyone help?
Re: Send/Receive Number messages using Bluetooth
Posted: 09 Aug 2011, 19:01
by afanofosc
aswinn01 wrote:I am wring a simple Java application to remote control a robot.
I cannot decipher NXT Bluetooth Number messages i.e. I don't know how to read 4 byte integer (2 is represented as 0x00 0x00 0x00 0x40, 3 is 0x00 0x00 0x40 0x40 etc.)
Can anyone help?
I think you are reading or writing these messages incorrectly. 2 definitely is 0x02, 0x00, 0x00, 0x00, 0x00 (4 bytes plus trailing zero) when I send/read a numeric bluetooth message. The value is a 4 byte little endian number followed by a null. A boolean message is either 0x01, 0x00 or 0x00, 0x00 (true or false).
Code: Select all
SetLength(Result, 4);
tag := edtNum.Value;
Result := Chr(Lo(Word(tag))) +
Chr(Hi(Word(tag))) +
Chr(Lo(HiWord(tag))) +
Chr(Hi(HiWord(tag)));
Code: Select all
orig^ := kNXT_DirectCmdNoReply;
inc(orig);
orig^ := kNXT_DCMessageWrite;
inc(orig);
orig^ := inbox;
inc(orig);
orig^ := Byte(len+1); // add null terminator
inc(orig);
i := 1;
while i <= len do
begin
orig^ := Ord(msg[i]);
inc(orig);
inc(i);
end;
orig^ := 0; // set last byte to null
Hope this helps,
John Hansen
Re: Send/Receive Number messages using Bluetooth
Posted: 10 Aug 2011, 12:52
by aswinn01
Thanks for the swift reply.
I had assumed that the number would be presented as a little endian 4 byte integer.
However, after several hours of testing I have worked out that my NXT seems to be passing numbers as 4 bytes in the IEEE 754 single precision binary floating-point format: binary32.
All the best,
Andrew.
Re: Send/Receive Number messages using Bluetooth
Posted: 10 Aug 2011, 16:13
by afanofosc
You must be running a program compiled using the NXT-G compiler version 2.0 on your NXT, then. The 1.0 block for sending a numeric bluetooth message always used an integer (4 byte) type internally when calling the FLATTEN opcode. The 2.0 blocks tend to use the 4-byte float type exclusively so that would explain why a numeric bluetooth message sent using a 2.0 NXT-G SendMessage block would be no longer compatible with a numeric message sent using a 1.0 NXT-G SendMessage block. That seems like an unfortunate choice that was made by LEGO/NI since it means that you cannot send numbers between two NXTs running programs compiled by different versions of the NXT-G compiler (even if both are running the same firmware version).
If you were receiving a numeric bluetooth message from an NXT running a program compiled using the 1.1 version of the NXT-G compiler or programs compiled with any version of NBC/NXC then the format will be a 4 byte little endian integer.
John Hansen
Re: Send/Receive Number messages using Bluetooth
Posted: 10 Aug 2011, 18:34
by aswinn01
MY NXT program is using NXT-G 2.0. It is helpful to know that the NXC language uses 4 byte integers. I will be starting to use the NXC language as soon as I've read the book (which I got delivered yesterday).
I'll create methods for reading and writing float and int data for my projects.
Best regards,
Andrew.
Re: Send/Receive Number messages using Bluetooth
Posted: 10 Aug 2011, 20:11
by afanofosc
I will plan on adding a set of API functions to send/receive float values via Bluetooth in NBC/NXC and also in the BricxCC Message tool window. Thanks for bringing this issue to my attention.
John Hansen