我正在尝试从arduino收集实时数据(使用Qt类QSerialPort),并将其实时绘制为图形(使用类QCustomPlot)。我是使用串行设备的新手,所以我不太确定在QSerialPort类中使用哪个函数来收集数据。下面是我设置序列号的当前代码:
QSerialPort serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial.setPortName("com4");
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
serial.open(QIODevice::ReadWrite);
setupRealtimeDataDemo(ui->customPlot);
}...and这里是我的实时时隙数据的代码...
void MainWindow::realtimeDataSlot()
{
// calculate two new data points:
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
double key = 0;
#else
double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
#endif
static double lastPointKey = 0;
if (key-lastPointKey > 0.01) // at most add point every 10 ms
{
double value0 = qSin(key); //sin(key*1.6+cos(key*1.7)*2)*10 + sin(key*1.2+0.56)*20 + 26;
double value1 = qCos(key); //sin(key*1.3+cos(key*1.2)*1.2)*7 + sin(key*0.9+0.26)*24 + 26;
// add data to lines:
ui->customPlot->graph(0)->addData(key, value0);
ui->customPlot->graph(1)->addData(key, value1);
// set data of dots:
ui->customPlot->graph(2)->clearData();
ui->customPlot->graph(2)->addData(key, value0);
ui->customPlot->graph(3)->clearData();
ui->customPlot->graph(3)->addData(key, value1);
// remove data of lines that's outside visible range:
ui->customPlot->graph(0)->removeDataBefore(key-8);
ui->customPlot->graph(1)->removeDataBefore(key-8);
// rescale value (vertical) axis to fit the current data:
ui->customPlot->graph(0)->rescaleValueAxis();
ui->customPlot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
ui->customPlot->replot();
// calculate frames per second:
static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
ui->statusBar->showMessage(
QString("%1 FPS, Total Data points: %2")
.arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
.arg(ui->customPlot->graph(0)->data()->count()+ui->customPlot->graph(1)->data()->count())
, 0);
lastFpsKey = key;
frameCount = 0;
}
}任何关于如何实时获取数据和/或更好的实现的帮助都将不胜感激。谢谢:)
发布于 2014-06-19 11:58:51
您可以将QSerialPort的readyRead信号连接到插槽,以便在新数据到达时读取数据:
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));在readData中,您可以调用readAll()将所有可用数据读取到QByteArray,并将数组传递给另一个函数进行绘图:
void MainWindow::readData()
{
QByteArray data = serial->readAll();
plotMyData(data);
}发布于 2015-01-14 09:53:37
I wrote a full demo application,它显示了如何从串口读取数据。
对于某些设备(如Arduino),在open()成功后调用setDataTerminalReady(true);是必不可少的。因此,对于那些在使用Terminal Example时遇到问题的人,请记住将void MainWindow::openSerialPort()修改为:
void MainWindow::openSerialPort()
{
SettingsDialog::Settings p = settings->settings();
serial->setPortName(p.name);
serial->setBaudRate(p.baudRate);
serial->setDataBits(p.dataBits);
serial->setParity(p.parity);
serial->setStopBits(p.stopBits);
serial->setFlowControl(p.flowControl);
if (serial->open(QIODevice::ReadWrite)) {
console->setEnabled(true);
console->setLocalEchoEnabled(p.localEchoEnabled);
ui->actionConnect->setEnabled(false);
ui->actionDisconnect->setEnabled(true);
ui->actionConfigure->setEnabled(false);
ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
// HACK
serial->setDataTerminalReady(true);
} else {
QMessageBox::critical(this, tr("Error"), serial->errorString());
ui->statusBar->showMessage(tr("Open error"));
}
}https://stackoverflow.com/questions/24295900
复制相似问题