无法使用 qextserialport 声明串口

Cha*_*lie 0 c++ qt serial-port qextserialport

我已经看到了几个问题并在谷歌上搜索了很多,但我找不到使用 qextserialport 声明串行端口对象的方法,以便从 arduino 读取和写入。我尝试了用户在How to write on serial port using Qextserialport 中所做的尝试,但编译器给了我以下错误:

undefined reference to `QextSerialPort::QextSerialPort(QString const&, QextSerialPort::QueryMode, QObject*)'
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtExtSerialPort/qextserialport.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_BTN_Connect_clicked()
{

    int index=ui->Selector_Port->currentIndex();
    QextSerialPort *port = new QextSerialPort("/dev/ttyACM0");
}
Run Code Online (Sandbox Code Playgroud)

为什么会出现这个错误?我使用的是 Debian 8.2 和 QT 4.8.6

编辑: 添加行后:

CONFIG += qesp_linux_udev
QT += extserialport
Run Code Online (Sandbox Code Playgroud)

到项目文件,我遇到以下错误:

"Project MESSAGE: Warning: unknown QT: extserialport"
Run Code Online (Sandbox Code Playgroud)

Ore*_*era 5

错误消息由链接器生成。这意味着它找不到QextSerialPort库二进制文件。

根据Qt4 中的QextSerialPort 手册 QextSerailPort只能在兼容模式下使用

可用作静态或共享库,或简单地将组件集成到应用程序中。

最简单的解决方案就是QextSerailPort与您的主项目一起构建。只需将其包含到您的项目文件 ( .pro) 中:

include(pathToQesp/compat/qextserialport.pri)
Run Code Online (Sandbox Code Playgroud)

您不需要QT += extserialport,因为在兼容模式下它不用作 Qt 模块。


最简单的方法

  • 新建一个文件夹; 去吧:mkdir test && cd test
  • git clone https://github.com/qextserialport/qextserialport
  • 在此文件夹中创建一个新的 Qt 项目,例如使用 name extserialtest,因此您有两个文件夹:
    • qextserialportQextSerailPort
    • extserialtest 与您的 Qt 项目
  • 将该行添加include (../qextserialport/src/qextserialport.pri)extserialtest.pro
  • 编写您的项目代码:#include <qextserialport.h>new QextSerialPort("/dev/ttyACM0");

在 Linux 中的 Qt 4.8.3 上验证它开箱即用。