2016-02-11 36 views
0

我在Windows 10 PC上修改了這段代碼,它編譯並運行時沒有崩潰。Qt串口程序在另一個系統上運行時崩潰

#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盤上。有什麼想法嗎?

+0

'1'您不得重新分配CRT的調試版本。這些是Visual Studio的一部分。您可能也不應該爲Qt重新分配調試二進制文件。 '2'你是什麼意思*「程序崩潰」*?這是一個相當模糊的描述。它顯示一個對話框嗎?如果是這樣,它說什麼?或者它立即終止? – IInspectable

+0

該程序立即終止,我可以嘗試刪除DDL的,但這真的會有所作爲?順便說一下,當我說立即終止,我的意思是對話框打開,但我收到消息說:「USBCommunication6已停止工作。」 (是的,我最初稱這個程序爲USBCommunication;我從來沒有打算改名字)@IInspectable –

+0

*「真的會有所作爲嗎?」 - 是的。重新分配Visual Studio附帶的CRT的調試版本是不合法的。而且這也意味着您正在部署應用程序的調試版本。沒有理由發佈調試版本的應用程序。構建發佈配置。顧名思義,這意味着要發佈給用戶。 – IInspectable

回答

0

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

閱讀Qt deployment docs,特別是最後一部分(從「應用程序依賴關係」到最後)。

dependency walker有助於找到實際的依賴關係,理論上windeployqt應該自動完成創建具有所需文件的發佈目錄的工作。

另請參閱此other question,但它適用於使用QML的應用程序,因此它比您的情況更復雜。

相關問題