2016-08-09 33 views
0

我想做一个从Arduino输出值并在Qt中绘制形状的项目。我不确定QCustomPlot是否可以做到这一点。你能给我一些建议吗?例如,我创建了一个用于输入位置(x,y)到Arduino并进行计算的Qt GUI,然后Arduino将值信号发送给Qt,并在我想要的位置上绘制形状。可能吗?qcustomplot中的信号来自Arduino的信号

+0

当然这是可能的。从这里开始https://github.com/dmontanari/qplotduino,这是一个完成所有工作的项目 – demonplus

回答

0

几年前,我使用QWTQextSerialPort来做类似的事情。你必须使用串口连接你的Arduino(在Windows COM1,COM2 ...中),并且你将能够从缓冲区读取/写入数据。

现在,Qt为本任务集成了一个本地支持,检查QtSerialPort Support,配置您的端口请检查此类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(); 

享受!