簡體   English   中英

通過來自Arduino的信號在qcustomplot中繪畫

[英]paint in qcustomplot by signal from Arduino

我想做一個從Arduino輸出值並在Qt中繪制形狀的項目。 我不確定QCustomPlot可以做到這一點。 你能給我一些建議嗎?

例如,我創建了一個Qt GUI,用於向Arduino輸入position(x,y)並進行計算,然后Arduino將值信號發送到Qt並在所需的位置繪制形狀。 可能嗎?

幾年前,我使用QWTQextSerialPort進行了類似的操作 您必須使用串行端口(在Windows COM1,COM2 ...中)連接Arduino,然后才能從緩沖區讀取/寫入數據。

現在,Qt集成了對此任務的本機支持,請檢查QtSerialPort支持 ,以配置您的端口,請檢查此類QSerialPort 如何讀取數據? 使用QByteArray,例如:

 QByteArray responseData = serial->readAll();
 while(serial->waitForReadyRead(100))
       responseData += serial->readAll();

現在將所有字節存儲在double類型的QVector中。

QVector<double> data;
QDataStream stream(line);
stream >> data;

該數據將由QCustomPlot繪制。 例:

int numSamples = data.size();
QVector<double> x(numSamples), y(numSamples); // initialize with entries 0..100
for (int i=0; i<numSamples; ++i)
{
  x[i] = i/50.0 - 1; // x goes from -1 to 1
  y[i] = data[i]; // In this line copy the data from your Arduino Buffer and apply what you want, maybe some signal processing
}
// create graph and assign data to it:
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// give the axes some labels:
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();

請享用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM