Question

I'm attempting to make use of the newly included QtSerialPort in Qt v5.1.1 but I'm running into some issues when trying to compile that I have little experience with. From my searching it seems that the QtSerialPort from qt 5.1 doesn't come built and ready to use, am I correct in this? How would I prepare it using either Qt Creator or Visual Studio 2010? I just recently upgraded from 4.8.4 and inherited the project so I'm not sure what all was done previously. I've found instructions for compiling with various Linux versions but nothing for Windows seemed consistent or straightforward so I was hoping someone here had some more concrete instructions.

Thank You!

EDIT: I'll add a specific error or two that I'm getting, maybe that'll help identify what I'm doing wrong.

error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QSerialPort::metaObject(void)const " (?metaObject@QSerialPort@@UBEPBUQMetaObject@@XZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSerialPort::QSerialPort(class QObject *)" (__imp_??0QSerialPort@@QAE@PAVQObject@@@Z) referenced in function "public: __thiscall serial_client::serial_client(class QObject *)" (??0serial_client@@QAE@PAVQObject@@@Z)

These are just two of 21 errors that show up at compile time. As I mentioned in the comments neither Visual Studio 2010 or Qt Creator seem to recognize the QtSerialPort libraries.

Was it helpful?

Solution

Alright, after trying a bunch of different stuff and combining information from different places I managed to get everything figured out and working.

The reason it didnt work of course was simple, the module hadn't been included via the Qt5 Plug-in to Visual Studio, and this was for the simple reason that it wasn't shown as an available module. An additional problem was that I had not installed Qt 5.1.1 with Source Dependencies installed, I had simply opted for the default installation which does not include whatever QtSerialPort needed. After a reinstallation I modified my VS Project Settings in the following places:

  1. Project Properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories, added line: $(QTDIR)\include\QtSerialPort
  2. Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, added line: QT_SERIALPORT_LIB
  3. Project Properties -> Configuration Properties -> Linker -> Input-> Additional Dependencies, added line: Qt5SerialPort.lib

Note: $(QTDIR) = C:\Qt\Qt5.1.1\5.1.1\msvc2010_opengl\

Afterwards I added Qt5SerialPort.dll and Qt5SerialPortd.dll to my Release and Debug folder within my project respectively.

I'm fairly certain these are the only changes I've made. I hope this is of use to someone else!

OTHER TIPS

I have made a little project with Qt5.0.2 with QSerialPort,that runs fine on Linux and Windows XP/7. Compiling it on Linux was pretty hassle free, as all libraries have already been in place. To compile it on Windows I used QtCreator (not Visual Studio) installed on Windows (no cross compile).

First you must check, if QtCreator can find QSerialPort, then it actually should already work to compile it under QtCreator under Windows. To deploy the program you have to make sure that all used QtLibraries are in place. Easiest way is to just put the in the same directory as your program. For my program this looks something like this:

  • +platforms (directory with following two dlls)

    • -qminimal.dll
    • -qwindows.dll

    • icudt51.dll

    • icuin51.dll
    • icuuc51.dll
    • libEGL.dll
    • libGLESv2.dll
    • Qt5Core.dll
    • Qt5Gui.dll
    • Qt5SerialPort.dll
    • Qt5Widgets.dll

Care should be taken, that the used dlls match your compiler! 32bit vs 64bit as well as matching the used compiler (MinGW or Visual Studio Compiler).

code snippet to open my serial port:

void MainWindow::openSerialPort() 
{
struct Settings p;

/* Use name of ComPort from Combobox */
p.name = ui->cboComPort->currentText();
p.baudRate = 38400;
p.dataBits = QSerialPort::Data8;
p.parity = QSerialPort::NoParity;
p.stopBits = QSerialPort::OneStop;
p.flowControl = QSerialPort::NoFlowControl;
p.stringBaudRate = "38400";
p.stringDataBits = "8";
p.stringParity = tr("no parity");
p.stringFlowControl = tr("no flow control");
p.stringStopBits = tr("1 stopbit");

serial->setPortName(p.name);
if (serial->open(QIODevice::ReadWrite)) {
    if (serial->setBaudRate(p.baudRate)
            && serial->setDataBits(p.dataBits)
            && serial->setParity(p.parity)
            && serial->setStopBits(p.stopBits)
            && serial->setFlowControl(p.flowControl)) {

        //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.stringStopBits).arg(p.stringParity).arg(p.stringFlowControl));

    } else {
        serial->close();
        QMessageBox::critical(this, tr("Error"), serial->errorString());

        ui->statusBar->showMessage(tr("Configure error"));
    }
} else {
    QMessageBox::critical(this, tr("Error"), serial->errorString());

    ui->statusBar->showMessage(tr("Open error"));
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top