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.3k 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.
  • 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 Offline
                    Christian EhrlicherC Offline
                    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