首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Qt Creator中采集外部串行设备的实时数据?

如何在Qt Creator中采集外部串行设备的实时数据?
EN

Stack Overflow用户
提问于 2014-06-19 06:17:55
回答 2查看 2.5K关注 0票数 1

我正在尝试从arduino收集实时数据(使用Qt类QSerialPort),并将其实时绘制为图形(使用类QCustomPlot)。我是使用串行设备的新手,所以我不太确定在QSerialPort类中使用哪个函数来收集数据。下面是我设置序列号的当前代码:

代码语言:javascript
复制
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这里是我的实时时隙数据的代码...

代码语言:javascript
复制
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;
  }
}

任何关于如何实时获取数据和/或更好的实现的帮助都将不胜感激。谢谢:)

EN

回答 2

Stack Overflow用户

发布于 2014-06-19 11:58:51

您可以将QSerialPortreadyRead信号连接到插槽,以便在新数据到达时读取数据:

代码语言:javascript
复制
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));

readData中,您可以调用readAll()将所有可用数据读取到QByteArray,并将数组传递给另一个函数进行绘图:

代码语言:javascript
复制
void MainWindow::readData()
{
    QByteArray data = serial->readAll();

    plotMyData(data);
}
票数 0
EN

Stack Overflow用户

发布于 2015-01-14 09:53:37

I wrote a full demo application,它显示了如何从串口读取数据。

  • 指定打开有效连接所需的设置,在本例中为Arduino device;
  • 实现负责读取数据并将其打印到控制台的类。

对于某些设备(如Arduino),在open()成功后调用setDataTerminalReady(true);是必不可少的。因此,对于那些在使用Terminal Example时遇到问题的人,请记住将void MainWindow::openSerialPort()修改为:

代码语言:javascript
复制
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"));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24295900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档