2015-09-27 34 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