Search code examples
c++qtserial-portqextserialport

Can't declare serial port using qextserialport


I have seen several questions and googled a lot and I can't find a way to declare a serial port object using qextserialport, in order to read and write to/from an arduino. I tried what the user did in How to write on serial port using Qextserialport, but the compiler gives me the following error:

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

Here is the code I'm using:

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

Why does this error appear? I'm using Debian 8.2 and QT 4.8.6

EDIT: After adding the lines:

CONFIG += qesp_linux_udev
QT += extserialport

to the project file, I'm having the following error:

"Project MESSAGE: Warning: unknown QT: extserialport"

Solution

  • The error message is generated by linker. It means that it cannot find QextSerialPort library binaries.

    According to QextSerialPort Manual QextSerailPort in Qt4 can be used only in Compat Mode:

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

    The simplest solution is just to build QextSerailPort together with your main project. Just include it to your project file (.pro):

    include(pathToQesp/compat/qextserialport.pri)
    

    You do not need QT += extserialport, since in the Compat Mode it is not used as a Qt module.


    The simplest HowTo

    • create a new folder; go to it: mkdir test && cd test
    • git clone https://github.com/qextserialport/qextserialport
    • create a new Qt project in this folder, for example with name extserialtest, so you have two folders:
      • qextserialport with QextSerailPort package
      • extserialtest with your Qt project
    • add the line include (../qextserialport/src/qextserialport.pri) into extserialtest.pro
    • write your project code: #include <qextserialport.h> and new QextSerialPort("/dev/ttyACM0");

    Verified on Qt 4.8.3 in Linux that it works out of the box.