Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort crash on write()

QSerialPort crash on write()

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MichaelR
    wrote on last edited by
    #1

    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);
    }
    
    
    VRoninV 1 Reply Last reply
    0
    • M MichaelR

      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);
      }
      
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

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

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MichaelR
        wrote on last edited by
        #3

        Thanks for the suggestion VRonin.

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you show a stack trace ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          VRoninV 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Can you show a stack trace ?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved