我正在研究Qt,但是我遇到了一個問題,對於我來說我無法修復。我已經嘗試了很多不同的組合,但是它仍然沒有給我輸出我想要的。我希望有人能幫助我。Qt讀取串行輸入,然後將其分割成單獨的變量
QStringList buffer_split = serialBuffer.split(","); // split the serialBuffer string, parsing with ',' as the separator
// Check to see if there less than 3 tokens in buffer_split.
// If there are at least 3 then this means there were 2 commas,
// means there is a parsed temperature value as the second token (between 2 commas)
if(buffer_split.length() < 3){
// no parsed value yet so continue accumulating bytes from serial in the buffer.
serialData = arduino->readAll();
serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
serialData.clear();
}else{
// the second element of buffer_split is parsed correctly, update the temperature value on temp_lcdNumber
serialBuffer = "";
qDebug() << buffer_split << "\n";
parsed_data = buffer_split[1];
}
上述解決方案已經爲我工作,反過來我正在讀的值通過串口發送如:
0,0,0,0,0,0
以上就是如何parsed_data
正在讀從串口信息, 哪個是對的。
我遇到的問題是分裂,然後將它們存儲在單獨的變量中以啓動一些if語句。到目前爲止,我似乎無法得到它的工作。
如果有人可以幫助我,我將不勝感激
謝謝
我不清楚你的問題。正如你用parsed_data = buffer_split [1]寫的那樣,你可以訪問每個單獨的值並存儲它。你卡在哪裏? –
當我嘗試存儲例如buffer_split [2]時,它似乎不起作用。尋找這樣的東西。我希望這可以讓你更容易理解。例如im發送的數組是45,0,67,0,0,13,我需要將每個數字存儲在一個變量中。 int num1 = parsed_data [0]這將是45,詮釋num2 = parsed_data [1]這將是0和詮釋num3 = parsed_data [2]這將是67 –