QSerialPort crash on write()



  • Hello,

    I find that this code runs fine in Debug mode but in Release mode causes my program to crash a few seconds after opening.

    If remove the line that writes to the serial port then it doesn't crash.

    I can't figure out why it is crashing!

    MyClass::MyClass()
    {
        // Serial port connection
        m_serialPort = new QSerialPort(this);
        connect(m_serialPort, &QSerialPort::readyRead, this, &MyClass::readSerialData);
        openSerialPort();
    }
    
    void MyClass::openSerialPort()
    {
        QString portName = "";
    
        const QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
        for (const QSerialPortInfo &info : infos) {
            if (info.isBusy() == false) {
                portName = info.portName();
            }
        }
    
        m_serialPort->setPortName(portName);
        if (m_serialPort->open(QIODevice::ReadWrite)) {
            // connected
        } else {
            qWarning() << "Cannot open serial port";
        }
    
        m_serialPort->write("TESTWRITE", 9);
    }
    
    


  • can you try replacing m_serialPort->write("TESTWRITE", 9); with

    QTextStream portStream(m_serialPort);
    portStream << "TESTWRITE";
    


  • Thanks for the suggestion VRonin.

    I tried this but still causes program to crash after 10 or 15 seconds.


  • Lifetime Qt Champion

    Hi,

    Can you show a stack trace ?



  • @SGaist said in QSerialPort crash on write():

    Can you show a stack trace ?

    Can you get a meaningful stack trace in release mode?

    BTW the problem does not appear to be in this snippet


  • Lifetime Qt Champion

    Indeed, it's harder. I somehow missed the "Release" part...

    On a side note, it's pretty unusual to call write if the port opening failed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.