0

I modified this code on a Windows 10 PC, and it compiled and ran without crashing.

#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;
}

I set Qt to release mode, and packaged the application with the following .dll files:

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

When I ran the application on another system (also windows 10), the program crashes immediately - and yes, I did move SerialCommand.dat to its correct location on the C drive. Any thoughts?

Z Rev
  • 84
  • 1
  • 13
  • `1` You must not redistribute the debug version of the CRT. Those are part of Visual Studio. You probably also shouldn't redistribute the debug binaries for Qt. `2` What do you mean by *"the program crashes"*? That's a fairly fuzzy description. Does it show a dialog? If so, what does it say? Or does it terminate immediately? – IInspectable Feb 11 '16 at 16:09
  • The program terminates immediately, and I can try removing the ddl's, but would that really make a difference? And btw, when I say terminates immediately, I mean the dialogue box opens, but i get the message saying "USBCommunication6 has stopped working." (And yes, I called this program USBCommunication originally; I just never bothered to change the name) @IInspectable – Z Rev Feb 11 '16 at 17:40
  • *"would that really make a difference?"* - Yes. It is not legal to redistribute the debug version of the CRT that ships with Visual Studio. And it also means that you are deploying a debug version of your application. There is no reason ever to ship a debug version of an application. Build a release configuration. As the name implies this is meant to be released to users. – IInspectable Feb 12 '16 at 15:17

1 Answers1

0

You're packaging the debug versions of the dlls (ending with a d) instead of release ones.

Read the Qt deployment docs, especially the last part (from "Application Dependencies" to the end).

The dependency walker helps find the actual dependencies, and in theory windeployqt should automate the work of creating a release dir with all the files you need.

See also this other question, but it's for an app using QML, so it's more complex than your case.

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33