2

我正在尝试从用 RobotC 编程的 NXT 向 PC 上的 Qt 应用程序发送消息。

从手机发送字符串数据时,PC 端运行良好。

在NXT上,我首先尝试使用该sendMessage()功能。这可行,但只能发送整数,并且它添加了一种使 PC 端解码复杂化的标头。我可以使用调试器看到这些东西。

然后我尝试了该nxtWriteRawBluetooth()功能,该功能允许在没有标头的情况下发送数据。但是官方文档不是很清楚,示例看起来离题了。我在网上搜索了一下,但没有找到任何有用的资源。RobotC 示例也没有帮助。

这是NXT 上的RobotC 代码。它编译并运行没有问题。(但没有按预期工作)

////////////////////////////////////////////////////
//                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);
}

我探索了一些可能性:

  • 手动将流索引设置为 1、2 或 3 并不能解决问题
  • 手动设置消息长度没有帮助
  • 将消息长度减少到一个字节无济于事

我也尝试了该cCmdMessageWriteToBluetooth()功能,但没有成功。

欢迎任何建议=)谢谢

附加信息:我在 Windows 8 上使用 RobotC 4.9

4

0 回答 0