2015-09-27 72 views
-1

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

undefined reference to `QextSerialPort::QextSerialPort(QString const&, QextSerialPort::QueryMode, QObject*)' 

这里是代码我使用:

#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"); 
} 

为什么会出现这种错误出现在哪里?我使用Debian 8.2和QT 4.8.6

编辑:,我有以下错误

CONFIG += qesp_linux_udev 
QT += extserialport 

到项目文件:

"Project MESSAGE: Warning: unknown QT: extserialport" 
添加行后
+0

你的'.pro'文件中是否有'QT + = extserialport'行?你是否将它用作Qt模块或直接在'中包含项目。pro' include(qextserialport/src/qextserialport.pri)? –

+0

是的,这一行在项目文件中,但它不起作用。 – Charlie

+1

这是一个链接器错误。这意味着它无法找到执行'QextSerialPort'的二进制文件。 –

回答

4

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

根据QextSerialPort ManualQextSerailPort中的Qt4只能在兼容模式使用:

Can be used as static or shared library, or simply integrate the component into application.

最简单的办法就是用你的主要项目共筑QextSerailPort。只要将它包括到您的项目文件(.pro):

include(pathToQesp/compat/qextserialport.pri) 

你不需要QT += extserialport,因为它不是作为一个Qt模块兼容模式


最简单的方法文档

  • 创建一个新的文件夹;去它:mkdir test && cd test
  • git clone https://github.com/qextserialport/qextserialport
  • 此文件夹中创建一个新的Qt项目,例如用名extserialtest,所以你有两个文件夹:
    • qextserialportQextSerailPort
    • extserialtest与Qt工程
  • include (../qextserialport/src/qextserialport.pri)添加到extserialtest.pro
  • 写你的项目代码:#include <qextserialport.h>new QextSerialPort("/dev/ttyACM0");

在Linux中验证了的Qt 4.8.3,它的作品开箱。

+0

它就像一个魅力!非常感谢您的详细解答;) – Charlie