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("nntest");
return 0;
}
我将Qt设置为发布模式,并将应用程序与以下.dll文件打包:
- MSVCP120d.dll
- MSVCR120d.dll
- Qt5核心.dll
- Qt5核心.dll
- Qt5SerialPort.dll
- Qt5SerialPortd.dll
- Qt5Widgets.dll
- 问视窗.dll
当我在另一个系统(也是 windows 10)上运行该应用程序时,程序立即崩溃 - 是的,我确实将 SerialCommand.dat 移动到 C 驱动器上的正确位置。有什么想法吗?
您正在打包 dll 的调试版本(以 d
结尾)而不是发布版本。
阅读Qt部署文档,尤其是最后一部分(从"应用程序依赖关系"到结尾)。
依赖关系查看器有助于查找实际的依赖关系,理论上windeployqt
应该自动创建包含所需所有文件的发布目录的工作。
另请参阅另一个问题,但它适用于使用 QML 的应用程序,因此它比您的情况更复杂。
相关文章:
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- C++具有移动语义的可变参数工厂会导致运行时崩溃
- 使用数组的单向链表:它在运行时崩溃
- "Unable to open file libc++abi.dylib" .程序生成,但在运行时崩溃。使用 xcode 10.1 (10B61)
- 尝试打开 SDL2 窗口时与 D-Bus 相关的运行时崩溃
- 为什么对齐的参数编译正确,但在运行时崩溃
- 这个C++代码有什么问题?它在运行时崩溃
- 在 Xcode 6 中运行 cpp-netlib - 编译但在运行时崩溃
- 代码在 HoughCircles 的 OpenCV 函数圈运行时崩溃
- 在Qt/QML中的每一帧更新图像时运行时崩溃
- 将包含指向该类型队列的指针的结构推送到队列上时发生运行时崩溃
- OpenCV简单代码编译成功,但在运行时崩溃
- GLEW编译,但在运行时崩溃
- openCV程序可编译,但在运行时崩溃
- 为什么这些指针数组会导致运行时崩溃
- Opencv应用程序在运行时崩溃,错误代码为0x0000142
- QSerialPort 在开发环境之外运行时崩溃
- C# / C++应用程序在从Windows运行时崩溃,但不是从Visual Studio
- 构建运行良好,但在运行时崩溃
- Qt串行端口程序在另一个系统上运行时崩溃