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. QT SERIAL PORT THREADING CRASH APP
Forum Updated to NodeBB v4.3 + New Features

QT SERIAL PORT THREADING CRASH APP

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 6 Posters 1.2k Views
  • 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.
  • C Offline
    C Offline
    canrollas
    wrote on last edited by
    #1

    This is the IO thread of my serial device which I override it. I can connect to the device and monitor it. But when I press the stop button which gives the stop hex code to the serial device, It suddenly crashes the app without error. Can anyone help me with it?

    PS: For anyone who says why don't you use the readyread function? Basically, my serial device ( circuit ) sends every 2 ms and if there is no reading it deletes the data from its buffer. ready read function makes data leak from the buffer.

    Kind regards.

    #include "IOThread.h"
    
    IOThread::IOThread()
    {
    
    }
    void IOThread::setUpSerialPort(QString portName,int baudRate){
    
        serialPort.setPortName(portName);
        serialPort.setDataBits(QSerialPort::Data8);
        serialPort.setParity(QSerialPort::NoParity);
        serialPort.setStopBits(QSerialPort::OneStop);
        serialPort.setFlowControl(QSerialPort::NoFlowControl);
        if(baudRate==1200){
            serialPort.setBaudRate(QSerialPort::Baud1200);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==2400){
            serialPort.setBaudRate(QSerialPort::Baud2400);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==4800){
            serialPort.setBaudRate(QSerialPort::Baud4800);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==9600){
            serialPort.setBaudRate(QSerialPort::Baud9600);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==19200){
            serialPort.setBaudRate(QSerialPort::Baud19200);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==38400){
            serialPort.setBaudRate(QSerialPort::Baud38400);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==57600){
            serialPort.setBaudRate(QSerialPort::Baud57600);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else if(baudRate==38400){
            serialPort.setBaudRate(QSerialPort::Baud38400);
            if(serialPort.open(QIODevice::ReadWrite)){
                qInfo()<<"Device successfully connected";
                isOpen = true;
    
            }else{
                emit sendMessageToSerialDevice(serialPort.errorString());
            }
        }
        else{
            qInfo()<<"Baud Rate is not valid!! It is connecting via 9600 baudrate";
            emit sendMessageToSerialDevice("Baud Rate is not valid!! It is connecting via 9600 baudrate");
            serialPort.setBaudRate(QSerialPort::Baud9600);
    
        }
    
    }
    void IOThread::run(){
        qInfo()<<"Thread is started!";
        qInfo()<<"This is array of the thread:"<<orderList;
        if(isOpen == true){
    
            if(orderList.isEmpty()==false){
                foreach (int data, orderList) {
                    qInfo()<<"This is looper of the thread:"<<data<<orderList;
    
                    executeOrder(data);
                }
                orderList.clear();
            }
    
    
            while(monitoringStatus==true){
                if(orderList.isEmpty()==false){
                    qInfo()<<"Order list status"<<orderList<<Qt::endl;
    
                    foreach (int data, orderList) {
    
                        executeOrder(data);
    
                    }
                    orderList.clear();
                    this->msleep(10);
    
                }
                this->msleep(10);
                QByteArray dataString;
    
                dataString  = serialPort.readAll();
    
    
    
                if(dataString.isEmpty()==false){
                    qInfo()<<"Reader loop has started!"<<dataString.toHex()<<Qt::endl;
    
                }
                this->msleep(1);
    
            }
        }else{
            emit sendMessageToSerialDevice("Device is not open!! pls connect it first");
        }
    
    }
    
    void IOThread::executeOrder(int order){
        qInfo()<<"Order is executing:"<<order;
        if(order==0){
            if(connectionStatus == false){
                QByteArray b("0149");
                QByteArray bytes = QByteArray::fromHex(b);
    
                serialPort.write(bytes);
                serialPort.waitForBytesWritten();
    
                connectionStatus = true;
                emit sendConnectionStatus(true);
    
    
                qInfo()<<"Starting the device!"<<Qt::endl;
            }else{
                emit sendMessageToSerialDevice("Device is already connected!");
    
            }
    
        }else if(order == 1){
            if(connectionStatus==true){
                QByteArray b("0236");
                QByteArray bytes = QByteArray::fromHex(b);
    
                serialPort.write(bytes);
                serialPort.waitForBytesWritten();
                serialPort.close();
    
                isOpen = false;
    
                connectionStatus = false;
    
                emit sendConnectionStatus(false);
    
                qInfo()<<"Stopping the device!"<<Qt::endl;
    
    
            }else{
                emit sendMessageToSerialDevice("First connect to the device via makcon!");
    
            }
    
    
        }else if(order==2){
            qInfo()<<"This is general monitor start";
    
            if(connectionStatus == true){
                if(monitoringStatus==false){
                    qInfo()<<"Order 3 is excuting if block in IOTHread";
    
                    QByteArray b("0919");
                    QByteArray bytes = QByteArray::fromHex(b);
                    if(monitoringStatus == false){
    
                        serialPort.write(bytes);
                        serialPort.waitForBytesWritten();
                        qInfo()<<"Starting the monitoring device!"<<Qt::endl;
    
                    }
    
    
    
                    monitoringStatus = true;
                    emit sendMonitoringStatus(true);
    
                }else{
                    emit sendMessageToSerialDevice("Monitoring is already started!");
                }
    
            }else{
                emit sendMessageToSerialDevice("First connect to the device via makcon!");
            }
    
    
    
        }else if(order == 3){
            qInfo()<<"This is general monitor stop";
            // Kill all jedi masters :D
            if(connectionStatus == true){
                if(monitoringStatus == true){
                    QByteArray b("0A53");
                    QByteArray bytes = QByteArray::fromHex(b);
                    serialPort.write(bytes);
                    serialPort.waitForBytesWritten();
                    qInfo()<<"Stopping the monitoring device!"<<Qt::endl;
                    monitoringStatus = false;
                    emit sendMonitoringStatus(false);
                }else{
                    emit sendMessageToSerialDevice("First start the monitoring device!");
                }
            }else{
                emit sendMessageToSerialDevice("First connect to the device via makcon!");
    
            }
    
        }else{
            qInfo()<<"Order is not found!";
        }
    }
    
    
    void IOThread::addDataCommandToQList(int order){
        orderList.append(order);
        qInfo()<<"Data appending to commandList"<<order;
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • C canrollas

      This is the IO thread of my serial device which I override it. I can connect to the device and monitor it. But when I press the stop button which gives the stop hex code to the serial device, It suddenly crashes the app without error. Can anyone help me with it?

      PS: For anyone who says why don't you use the readyread function? Basically, my serial device ( circuit ) sends every 2 ms and if there is no reading it deletes the data from its buffer. ready read function makes data leak from the buffer.

      Kind regards.

      #include "IOThread.h"
      
      IOThread::IOThread()
      {
      
      }
      void IOThread::setUpSerialPort(QString portName,int baudRate){
      
          serialPort.setPortName(portName);
          serialPort.setDataBits(QSerialPort::Data8);
          serialPort.setParity(QSerialPort::NoParity);
          serialPort.setStopBits(QSerialPort::OneStop);
          serialPort.setFlowControl(QSerialPort::NoFlowControl);
          if(baudRate==1200){
              serialPort.setBaudRate(QSerialPort::Baud1200);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==2400){
              serialPort.setBaudRate(QSerialPort::Baud2400);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==4800){
              serialPort.setBaudRate(QSerialPort::Baud4800);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==9600){
              serialPort.setBaudRate(QSerialPort::Baud9600);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==19200){
              serialPort.setBaudRate(QSerialPort::Baud19200);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==38400){
              serialPort.setBaudRate(QSerialPort::Baud38400);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==57600){
              serialPort.setBaudRate(QSerialPort::Baud57600);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else if(baudRate==38400){
              serialPort.setBaudRate(QSerialPort::Baud38400);
              if(serialPort.open(QIODevice::ReadWrite)){
                  qInfo()<<"Device successfully connected";
                  isOpen = true;
      
              }else{
                  emit sendMessageToSerialDevice(serialPort.errorString());
              }
          }
          else{
              qInfo()<<"Baud Rate is not valid!! It is connecting via 9600 baudrate";
              emit sendMessageToSerialDevice("Baud Rate is not valid!! It is connecting via 9600 baudrate");
              serialPort.setBaudRate(QSerialPort::Baud9600);
      
          }
      
      }
      void IOThread::run(){
          qInfo()<<"Thread is started!";
          qInfo()<<"This is array of the thread:"<<orderList;
          if(isOpen == true){
      
              if(orderList.isEmpty()==false){
                  foreach (int data, orderList) {
                      qInfo()<<"This is looper of the thread:"<<data<<orderList;
      
                      executeOrder(data);
                  }
                  orderList.clear();
              }
      
      
              while(monitoringStatus==true){
                  if(orderList.isEmpty()==false){
                      qInfo()<<"Order list status"<<orderList<<Qt::endl;
      
                      foreach (int data, orderList) {
      
                          executeOrder(data);
      
                      }
                      orderList.clear();
                      this->msleep(10);
      
                  }
                  this->msleep(10);
                  QByteArray dataString;
      
                  dataString  = serialPort.readAll();
      
      
      
                  if(dataString.isEmpty()==false){
                      qInfo()<<"Reader loop has started!"<<dataString.toHex()<<Qt::endl;
      
                  }
                  this->msleep(1);
      
              }
          }else{
              emit sendMessageToSerialDevice("Device is not open!! pls connect it first");
          }
      
      }
      
      void IOThread::executeOrder(int order){
          qInfo()<<"Order is executing:"<<order;
          if(order==0){
              if(connectionStatus == false){
                  QByteArray b("0149");
                  QByteArray bytes = QByteArray::fromHex(b);
      
                  serialPort.write(bytes);
                  serialPort.waitForBytesWritten();
      
                  connectionStatus = true;
                  emit sendConnectionStatus(true);
      
      
                  qInfo()<<"Starting the device!"<<Qt::endl;
              }else{
                  emit sendMessageToSerialDevice("Device is already connected!");
      
              }
      
          }else if(order == 1){
              if(connectionStatus==true){
                  QByteArray b("0236");
                  QByteArray bytes = QByteArray::fromHex(b);
      
                  serialPort.write(bytes);
                  serialPort.waitForBytesWritten();
                  serialPort.close();
      
                  isOpen = false;
      
                  connectionStatus = false;
      
                  emit sendConnectionStatus(false);
      
                  qInfo()<<"Stopping the device!"<<Qt::endl;
      
      
              }else{
                  emit sendMessageToSerialDevice("First connect to the device via makcon!");
      
              }
      
      
          }else if(order==2){
              qInfo()<<"This is general monitor start";
      
              if(connectionStatus == true){
                  if(monitoringStatus==false){
                      qInfo()<<"Order 3 is excuting if block in IOTHread";
      
                      QByteArray b("0919");
                      QByteArray bytes = QByteArray::fromHex(b);
                      if(monitoringStatus == false){
      
                          serialPort.write(bytes);
                          serialPort.waitForBytesWritten();
                          qInfo()<<"Starting the monitoring device!"<<Qt::endl;
      
                      }
      
      
      
                      monitoringStatus = true;
                      emit sendMonitoringStatus(true);
      
                  }else{
                      emit sendMessageToSerialDevice("Monitoring is already started!");
                  }
      
              }else{
                  emit sendMessageToSerialDevice("First connect to the device via makcon!");
              }
      
      
      
          }else if(order == 3){
              qInfo()<<"This is general monitor stop";
              // Kill all jedi masters :D
              if(connectionStatus == true){
                  if(monitoringStatus == true){
                      QByteArray b("0A53");
                      QByteArray bytes = QByteArray::fromHex(b);
                      serialPort.write(bytes);
                      serialPort.waitForBytesWritten();
                      qInfo()<<"Stopping the monitoring device!"<<Qt::endl;
                      monitoringStatus = false;
                      emit sendMonitoringStatus(false);
                  }else{
                      emit sendMessageToSerialDevice("First start the monitoring device!");
                  }
              }else{
                  emit sendMessageToSerialDevice("First connect to the device via makcon!");
      
              }
      
          }else{
              qInfo()<<"Order is not found!";
          }
      }
      
      
      void IOThread::addDataCommandToQList(int order){
          orderList.append(order);
          qInfo()<<"Data appending to commandList"<<order;
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @canrollas said in QT SERIAL PORT THREADING CRASH APP:

      It suddenly crashes the app without error.

      Did you run your app through debugger to see what exactly happens?

      Also, are you sure you need threads at all? Qt is an asynchronous framework.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • jsulmJ jsulm

        @canrollas said in QT SERIAL PORT THREADING CRASH APP:

        It suddenly crashes the app without error.

        Did you run your app through debugger to see what exactly happens?

        Also, are you sure you need threads at all? Qt is an asynchronous framework.

        C Offline
        C Offline
        canrollas
        wrote on last edited by
        #3

        @jsulm Sir,look you have answered my other question same way. I need thread in my specific situation ( can not be explained ) .

        jsulmJ 2 Replies Last reply
        0
        • C canrollas

          @jsulm Sir,look you have answered my other question same way. I need thread in my specific situation ( can not be explained ) .

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @canrollas Then please run through debugger until it crashes and post the stack trace here. Debugging is the first thing to do in case of a crash.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • C canrollas

            @jsulm Sir,look you have answered my other question same way. I need thread in my specific situation ( can not be explained ) .

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @canrollas Also, where do you call setUpSerialPort? You need to understand that you should create all needed variables (like serialPort) inside run() method, else they will live in the thread which starts your io thread.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply
            0
            • jsulmJ jsulm

              @canrollas Then please run through debugger until it crashes and post the stack trace here. Debugging is the first thing to do in case of a crash.

              C Offline
              C Offline
              canrollas
              wrote on last edited by
              #6

              @jsulm WhatsApp Image 2022-10-07 at 16.13.57.jpeg

              this is functions
              WhatsApp Image 2022-10-07 at 16.15.59.jpeg WhatsApp Image 2022-10-07 at 16.16.13.jpeg

              jsulmJ 1 Reply Last reply
              0
              • C canrollas

                @jsulm WhatsApp Image 2022-10-07 at 16.13.57.jpeg

                this is functions
                WhatsApp Image 2022-10-07 at 16.15.59.jpeg WhatsApp Image 2022-10-07 at 16.16.13.jpeg

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @canrollas As you can see it crashes in executeOrder in file IOThread.cpp line 237 - what is in that line?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @canrollas Also, where do you call setUpSerialPort? You need to understand that you should create all needed variables (like serialPort) inside run() method, else they will live in the thread which starts your io thread.

                  C Offline
                  C Offline
                  canrollas
                  wrote on last edited by canrollas
                  #8

                  this is execute function

                  else if(order == 3){
                          qInfo()<<"This is general monitor stop";
                          // Kill all jedi masters :D
                          if(connectionStatus == true){
                              if(monitoringStatus == true){
                                  QByteArray b("0A53");
                                  QByteArray bytes = QByteArray::fromHex(b);
                                  serialPort.write(bytes);
                                  serialPort.waitForBytesWritten();
                                  qInfo()<<"Stopping the monitoring device!"<<Qt::endl;
                                  monitoringStatus = false;
                                  emit sendMonitoringStatus(false);
                              }else{
                                  emit sendMessageToSerialDevice("First start the monitoring device!");
                              }
                          }else{
                              emit sendMessageToSerialDevice("First connect to the device via makcon!");
                  
                          }
                  

                  this is caller function

                  @jsulm void SerialDevice::connectToCanBus(){
                  
                      QString portname ;
                  
                  
                      foreach (auto looper, serialPortInfo->availablePorts()) {
                          qInfo()<<"This is looping device on the machine:"<<looper.description()<<looper.portName();
                          if(looper.description()=="Makersan USB to CAN Converter" || looper.description()=="STMicroelectronics Virtual COM Port" && looper.portName().contains("tty")==true || looper.portName().contains("COM")==true){
                              qInfo()<<"The device has found in the loop:"<<looper.description();
                              portname = looper.portName();
                              thread->setUpSerialPort(looper.portName(),9600);
                              thread->addDataCommandToQList(0);
                  
                              if(thread->isRunning()==false){
                                  thread->start();
                              }
                          }
                  
                      }
                      if(portname.isEmpty()==true){
                          emit sendMessageToMainWindow("Device is not plugged or not detected");
                      }
                  
                  
                  }
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • C canrollas

                    this is execute function

                    else if(order == 3){
                            qInfo()<<"This is general monitor stop";
                            // Kill all jedi masters :D
                            if(connectionStatus == true){
                                if(monitoringStatus == true){
                                    QByteArray b("0A53");
                                    QByteArray bytes = QByteArray::fromHex(b);
                                    serialPort.write(bytes);
                                    serialPort.waitForBytesWritten();
                                    qInfo()<<"Stopping the monitoring device!"<<Qt::endl;
                                    monitoringStatus = false;
                                    emit sendMonitoringStatus(false);
                                }else{
                                    emit sendMessageToSerialDevice("First start the monitoring device!");
                                }
                            }else{
                                emit sendMessageToSerialDevice("First connect to the device via makcon!");
                    
                            }
                    

                    this is caller function

                    @jsulm void SerialDevice::connectToCanBus(){
                    
                        QString portname ;
                    
                    
                        foreach (auto looper, serialPortInfo->availablePorts()) {
                            qInfo()<<"This is looping device on the machine:"<<looper.description()<<looper.portName();
                            if(looper.description()=="Makersan USB to CAN Converter" || looper.description()=="STMicroelectronics Virtual COM Port" && looper.portName().contains("tty")==true || looper.portName().contains("COM")==true){
                                qInfo()<<"The device has found in the loop:"<<looper.description();
                                portname = looper.portName();
                                thread->setUpSerialPort(looper.portName(),9600);
                                thread->addDataCommandToQList(0);
                    
                                if(thread->isRunning()==false){
                                    thread->start();
                                }
                            }
                    
                        }
                        if(portname.isEmpty()==true){
                            emit sendMessageToMainWindow("Device is not plugged or not detected");
                        }
                    
                    
                    }
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @canrollas Which line in that code is 237 please?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J.HilkJ C 2 Replies Last reply
                    0
                    • jsulmJ jsulm

                      @canrollas Which line in that code is 237 please?

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @jsulm 237 is the waitForbytesWritten call


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      C 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @canrollas Which line in that code is 237 please?

                        C Offline
                        C Offline
                        canrollas
                        wrote on last edited by
                        #11

                        @jsulm Those are the lines
                        serialPort.write(bytes);
                        serialPort.waitForBytesWritten();

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @jsulm 237 is the waitForbytesWritten call

                          C Offline
                          C Offline
                          canrollas
                          wrote on last edited by
                          #12

                          @J-Hilk Can you see where is wrong or etc. ?

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            Valentin49
                            wrote on last edited by
                            #13

                            @jsulm @canrollas
                            I have the same issue both on Windows 10 and Ubuntu 20.04. Crash occurs on call to waitForBytesWritten()

                            +1 to solve this bug

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • V Valentin49

                              @jsulm @canrollas
                              I have the same issue both on Windows 10 and Ubuntu 20.04. Crash occurs on call to waitForBytesWritten()

                              +1 to solve this bug

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Valentin49 said in QT SERIAL PORT THREADING CRASH APP:

                              I have the same issue

                              then please create a minimal compileable example which reproduces your crash and create a new thread instead hijacking an old one

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • glapalmeG Offline
                                glapalmeG Offline
                                glapalme
                                wrote on last edited by
                                #15

                                Hi, I just stumbled acrros the same issue as yours when switching from Qt5.15.2 MinGW 32-bits to Qt6.3.2 MinGW 64-bits. My application was working fine with Qt5, but was constantly crashing when calling QSerialPort::waitForBytesWritten() when compiling using Qt6.

                                My original code:

                                // My QSerialPort object runs in its own thread
                                
                                QSerialPort *serial = new QSerialPort(this); 
                                // initializationn of serial object ...
                                
                                void MySerialClass::write( const QByteArray& bytes )
                                {
                                   serial->write(bytes);
                                   if(!serial->waitForBytesWritten()) 
                                   {
                                      qWarning() << "Write timeout!";
                                   }
                                }
                                

                                I found a fix by trial and error: I verify that there is indeed some bytes waiting to be written before calling the function:

                                void MySerialClass::write( const QByteArray& bytes )
                                {
                                   // write() returns the number of bytes that were actually written, or -1 if error
                                   quint64 bytes_written = serial->write(bytes);
                                   if(bytes_written != bytes.size() && !serial->waitForBytesWritten()) 
                                   {
                                      qWarning() << "Write timeout!";
                                   }
                                }
                                

                                Hope it works for you!

                                1 Reply Last reply
                                0

                                • Login

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