Qt串行端口程序在另一个系统上运行时崩溃

Qt serial port program crashes when run on another system

本文关键字:运行时 崩溃 系统 另一个 串行端口 程序 Qt      更新时间:2023-10-16

我在Windows 10 PC上修改了这段代码,它编译并运行而没有崩溃。

#include <QtSerialPort/QSerialPort>
#include <QTextStream>
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
QT_USE_NAMESPACE
int main(int argc, char *argv[])
{
    QCoreApplication coreApplication(argc, argv);
    int argumentCount = QCoreApplication::arguments().size();
    QStringList argumentList = QCoreApplication::arguments();
    QTextStream standardOutput(stdout);
    if (argumentCount == 1) {
        standardOutput << QObject::tr("Usage: %1 <serialportname>     [baudrate]").arg(argumentList.first()) << endl;
        return 1;
    }
    QSerialPort serialPort;
    QString serialPortName = argumentList.at(1);
    serialPort.setPortName(serialPortName);
    int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
    serialPort.setBaudRate(serialPortBaudRate);
    if (!serialPort.open(QIODevice::WriteOnly)) {
        standardOutput << QObject::tr("Failed to open port %1, error:     %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    }
    QFile dataFile("C:\SerialCommand.dat");
    if (!dataFile.open(QIODevice::ReadOnly)) {
        standardOutput << QObject::tr("Failed to open file for reading") <<     endl;
        return 1;
    }
    QByteArray writeData(dataFile.readAll());
    dataFile.close();
    if (writeData.isEmpty()) {
        standardOutput << QObject::tr("Either no data was currently available on the standard input for reading, or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    }
    qint64 bytesWritten = serialPort.write(writeData);
    if (bytesWritten == -1) {
        standardOutput << QObject::tr("Failed to write the data to port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    } else if (bytesWritten != writeData.size()) {
        standardOutput << QObject::tr("Failed to write all the data to port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    } else if (!serialPort.waitForBytesWritten(5000)) {
        standardOutput << QObject::tr("Operation timed out or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    }
    standardOutput << QObject::tr("Data successfully sent to port %1").arg(serialPortName) << endl;
    printf("nntest");
    return 0;
}

我将Qt设置为发布模式,并将应用程序与以下.dll文件打包:

  1. MSVCP120d.dll
  2. MSVCR120d.dll
  3. Qt5核心.dll
  4. Qt5核心.dll
  5. Qt5SerialPort.dll
  6. Qt5SerialPortd.dll
  7. Qt5Widgets.dll
  8. 问视窗.dll

当我在另一个系统(也是 windows 10)上运行该应用程序时,程序立即崩溃 - 是的,我确实将 SerialCommand.dat 移动到 C 驱动器上的正确位置。有什么想法吗?

您正在打包 dll 的调试版本(以 d 结尾)而不是发布版本。

阅读Qt部署文档,尤其是最后一部分(从"应用程序依赖关系"到结尾)。

依赖关系查看器有助于查找实际的依赖关系,理论上windeployqt应该自动创建包含所需所有文件的发布目录的工作。

另请参阅另一个问题,但它适用于使用 QML 的应用程序,因此它比您的情况更复杂。