For some reason, this crashes with "File Error!" The enhanced firmware adds "-1". If I comment out the b = through a8 = lines, it works fine.
The code is to receive (from the computer) the button values and axis info for a joystick. Any ideas why it fails?
-SavedCoder
To God be the glory, great things He has done;
So loved He the world that He gave us His Son,
Who yielded His life an atonement for sin,
And opened the life gate that all may go in.
savedcoder wrote:
For some reason, this crashes with "File Error!" The enhanced firmware adds "-1". If I comment out the b = through a8 = lines, it works fine.
The code is to receive (from the computer) the button values and axis info for a joystick. Any ideas why it fails?
Well, you've said it yourself, so I'm stating the obvious: Have you checked if joyvals has the correct format? What happens if an empty or a malformed message arrives? Also check if you really can receive bluetooth messages to a char-array this way (or if you have to use a string).
You could start by displaying the the joyvals array on the scree, i.e. displaying i and joyvals for i = 0 to 19, one by one with a little wait between. Then you can see WHERE your program crashes. Continue with this careful debugging and think about what bad things could happen (no message sent from computer, no message received, message received too late), etc. Then add some proper error handling.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming http://www.mindstorms.rwth-aachen.de MotorControl now also in Python, .net, and Mathematica
linusa wrote:Well, you've said it yourself, so I'm stating the obvious: Have you checked if joyvals has the correct format? What happens if an empty or a malformed message arrives?
Empty / malformed messages shouldn't make a difference, since the program only does some math on the array and stores the result. It's not like it makes decisions based on the values.
linusa wrote:You could start by displaying the the joyvals array on the scree, i.e. displaying i and joyvals for i = 0 to 19, one by one with a little wait between. Then you can see WHERE your program crashes.
I don't see how accessing a simple char-array could crash the program.
linusa wrote:Also check if you really can receive bluetooth messages to a char-array this way (or if you have to use a string).
The documentation just says "buffer". A char-array is about the simplest buffer.
To God be the glory, great things He has done;
So loved He the world that He gave us His Son,
Who yielded His life an atonement for sin,
And opened the life gate that all may go in.
You should definitely check the return value for possible error codes. The docs do not mention all the possible values but anything other than NO_ERR (0) means you do not have a valid message. The code replaces whatever you had in joyvals with whatever it read from the mailbox so if it read nothing from the mailbox then joyvals will have no bytes (other than the terminating null) in it (not 60).
//If there is a valid message in local mailbox, read it
if (!IS_ERR(Status) && MessageSize > 0 )
{
//!!! Also check for EMPTY_MAILBOX status?
//Size destination string
AllocStatus = cCmdDVArrayAlloc(DestDVIndex, MessageSize);
if (IS_ERR(AllocStatus))
return AllocStatus;
//Get Message
//!!! Should more aggressively enforce null termination before blindly copying to dataspace
Status = cCmdMessageRead(QueueID, cCmdDVPtr(DestDVIndex), MessageSize, *(ArgV[2]));
}
else
{
//Clear destination string
AllocStatus = cCmdDVArrayAlloc(DestDVIndex, 1);
if (IS_ERR(AllocStatus))
return AllocStatus;
//Successful allocation, make sure first byte is null terminator
*(UBYTE*)(cCmdDVPtr(DestDVIndex)) = '\0';
}
As you can see if there are no bytes to read (MessageSize == 0) it allocates 1 byte for the destination string and sets that byte to null. So if ReceiveMessage returns something other than NO_ERR then joyvals will be an empty string (joyvals[0] == 0 and any index > 0 will result in File Error -1 abort).
Oh... Thanks.
The documentation I looked at was the NXC guide that came with BricxCC.
To God be the glory, great things He has done;
So loved He the world that He gave us His Son,
Who yielded His life an atonement for sin,
And opened the life gate that all may go in.