控制台输出在C ++中的scanf()方法上暂停

时间:2016-04-22 23:26:28

标签: c++ io bluetooth serial-port scanf

我在库中使用以下方法来读取来自蓝牙设备的数据:

BTSerialPortBinding::Read(buffer, length)

Reads data from the bluetooth device, returns number of bytes read

buffer: pointer to buffer to hold received data
length: maximum namber of bytes to read

使用printf("%s", incomingData);时该方法效果很好 将数据输出到控制台。

然而,当我使用(scanf(incomingData, "6,%d,%d,%d\r\n", &x, &y, &z) == 3)时,控制台似乎保持打开状态,但一旦尝试此行,就不会输出任何内容。

数据连续出现,并以此形式为例:

6, 211, 233, 232; 
6, 392, 29, 93;
6, 42, 82, 94; 

我希望扫描并提取最后三个值,并将它们存储在一个int变量中。下面是我的代码,我使用的是在Visual Studio 2015上运行的Windows 10。

        char incomingData[256] = "";    
        int dataLength = 255;
        int readResult = 0;

        while (1)
        {
            readResult = bt->Read(incomingData, dataLength);
            incomingData[readResult] = 0;

            // this prints fine
            printf("%s", incomingData);

            int x, y, z;

            //stuck here 
            if (scanf(incomingData, "6,%d,%d,%d\r\n", &x, &y, &z) == 3) {

               printf("x:%d,y:%d,z:%d\n", x, y, z);
            }
            Sleep(500);
        }

1 个答案:

答案 0 :(得分:0)

如果您打算传递要解析的字符串,而不是sscanf()从终端执行阻止读取,则需要scanf(),这意味着在此之前不会发生任何其他事情完成 - 它可能不会,因为你不期望在终端上提供数据。

您可能还需要以特定于平台的方式对线路终结器进行思考。

相关问题