2

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.

4

2 回答 2

3

好吧,在尝试了一堆不同的东西并结合了来自不同地方的信息之后,我设法弄清楚了一切并开始工作。

它不起作用的原因当然很简单,该模块没有通过 Qt5 插件包含在 Visual Studio 中,原因很简单,即它没有显示为可用模块。另一个问题是我没有安装 Qt 5.1.1 并安装了 Source Dependencies,我只是选择了默认安装,它不包含任何 QtSerialPort 所需的内容。重新安装后,我在以下位置修改了我的 VS 项目设置:

  1. 项目属性 -> 配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加行:$(QTDIR)\include\QtSerialPort
  2. 项目属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理器定义,添加行:QT_SERIALPORT_LIB
  3. Project Properties -> Configuration Properties -> Linker -> Input-> Additional Dependencies,添加行:Qt5SerialPort.lib

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

之后,我分别将 Qt5SerialPort.dll 和 Qt5SerialPortd.dll 添加到我的项目中的 Release 和 Debug 文件夹中。

我相当肯定这些是我所做的唯一更改。我希望这对其他人有用!

于 2013-11-13T01:48:15.470 回答
1

我用 Qt5.0.2 和 QSerialPort 做了一个小项目,在 Linux 和 Windows XP/7 上运行良好。在 Linux 上编译它非常轻松,因为所有库都已经到位。为了在 Windows 上编译它,我使用了安装在 Windows 上的 QtCreator(不是 Visual Studio)(没有交叉编译)。

首先你必须检查,如果 QtCreator 可以找到 QSerialPort,那么它实际上应该已经可以在 Windows 下的 QtCreator 下编译它。要部署程序,您必须确保所有使用的 QtLibraries 都已到位。最简单的方法是将其放在与您的程序相同的目录中。对于我的程序,这看起来像这样:

  • +platforms(包含以下两个 dll 的目录)

    • -qminimal.dll
    • -qwindows.dll

    • icudt51.dll

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

应注意,使用的 dll 与您的编译器匹配32 位与 64 位以及匹配使用的编译器(MinGW 或 Visual Studio 编译器)。

打开我的串口的代码片段:

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"));
}
}
于 2013-11-08T06:25:37.713 回答