2

I'm trying to send a message from an NXT, programmed with RobotC to a Qt Application on a PC.

The PC-Side works well, when sending string data from a phone.

On the NXT, I first Tried to use the sendMessage() function. This works, but it's only possible to send integer, and it adds a sort of header that complicate decoding on PC-side. I could see this stuff using the debugger.

I then tried the nxtWriteRawBluetooth() function, that allows to send data without the header. But the Official documentation is not really clear, and the example looks off-topic. I googled the web but didn't find any helpful resource. The RobotC examples didn't help either.

Here's the RobotC code on the NXT. It compiles and runs without problem. (But doesn't work as expected)

////////////////////////////////////////////////////
//                Bluetooth Functions             //
////////////////////////////////////////////////////

// Sets the Blootooth mode to "raw"
void btToRaw()
{
    setBluetoothRawDataMode();
    while (!bBTRawMode){
        wait1Msec(1);
    }
    nxtScrollText("Nxt in Raw Mode");
}

// Waits until a connection is set
void waitForConnection()
{
    while(nBTCurrentStreamIndex < 0){
        wait1Msec(100);
    }
    nxtScrollText("Connecte");
    wait1Msec(1000);
}

// Sends a message over BT using the raw method
int sendToBt(ubyte* sourse, int len)
{
    wait1Msec(100);
    nxtWriteRawBluetooth(nBTCurrentStreamIndex , sourse, len);
    wait1Msec(100);
    return 1;
}

////////////////////////////////////////////////////
//                     Main Task                  //
////////////////////////////////////////////////////

task main()
{

    waitForConnection(); // should already be OK at that time
    btToRaw(); // Sets mode to raw

    ubyte Bytes[1]; // Byte array to send (example lenth)
    Bytes[0] = 'A'; // Content to send (example content)

    // The goal is to send an array with multiple bytes but I simplified to identify the bug

    nxtScrollText("Send a message");
    TFileIOResult Result = nxtWriteRawBluetooth(nBTCurrentStreamIndex , Bytes, 1); // Sends the message

    // Verify result
    switch (Result)
    {
    case ioRsltSuccess:
        nxtScrollText("Send OK");
        break;

    case ioRsltCommPending:
        nxtScrollText("Send Pending..");
        break;
    case ioRsltCommChannelBad:
    default:
        nxtScrollText("Send Bad"); // And it's what I get on the screen =(
        break;
    }
    wait1Msec(1000);
}

I explored some possibilities:

  • Manually setting the stream index to 1, 2 or 3 doesn't solve the problem
  • Manually setting the message length doesn't help
  • Reducing the message length to one byte doesn't help

I also tried the cCmdMessageWriteToBluetooth() function, but with no success.

Any advice is welcome =) Thanks

Additional information: I'm using RobotC 4.9 on Windows 8

Kaktus
  • 153
  • 4
  • 23

0 Answers0