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文件打包的應用程序:
- msvcp120d.dll
- msvcr120d.dll
- Qt5Core.dll
- Qt5Cored .dll
- Qt5SerialPort.dll
- Qt5SerialPortd.dll
- Qt5Widgets.dll
- qwindows.dll
當我跑到另一個系統上的應用(也是Windows 10),該程序立即崩潰 - 是的,我沒有動SerialCommand.dat到正確位置在C盤上。有什麼想法嗎?
'1'您不得重新分配CRT的調試版本。這些是Visual Studio的一部分。您可能也不應該爲Qt重新分配調試二進制文件。 '2'你是什麼意思*「程序崩潰」*?這是一個相當模糊的描述。它顯示一個對話框嗎?如果是這樣,它說什麼?或者它立即終止? – IInspectable
該程序立即終止,我可以嘗試刪除DDL的,但這真的會有所作爲?順便說一下,當我說立即終止,我的意思是對話框打開,但我收到消息說:「USBCommunication6已停止工作。」 (是的,我最初稱這個程序爲USBCommunication;我從來沒有打算改名字)@IInspectable –
*「真的會有所作爲嗎?」 - 是的。重新分配Visual Studio附帶的CRT的調試版本是不合法的。而且這也意味着您正在部署應用程序的調試版本。沒有理由發佈調試版本的應用程序。構建發佈配置。顧名思義,這意味着要發佈給用戶。 – IInspectable