簡體   English   中英

在另一個系統上運行時,Qt串行端口程序崩潰

[英]Qt serial port program crashes when run on another system

我在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("\n\ntest");

    return 0;
}

我將Qt設置為發布模式,並使用以下.dll文件打包了該應用程序:

  1. msvcp120d.dll
  2. msvcr120d.dll
  3. Qt5Core.dll
  4. Qt5Cored.dll
  5. Qt5SerialPort.dll
  6. Qt5SerialPortd.dll
  7. Qt5Widgets.dll
  8. qwindows.dll

當我在另一個系統(也是Windows 10)上運行該應用程序時,該程序立即崩潰-是的,我確實將SerialCommand.dat移到了C驅動器上的正確位置。 有什么想法嗎?

您正在打包dll的調試版本(以d結尾),而不是發行版本。

閱讀Qt部署文檔 ,尤其是最后一部分(從“ Application Dependencies”到最后)。

依賴關系遍歷器可以幫助您找到實際的依賴關系,從理論上講, windeployqt應該可以自動創建包含所有所需文件的發行目錄。

另請參見另一個問題 ,但這是針對使用QML的應用程序的,因此它比您的情況更為復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM