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. Code works using waitForReadyRead(), doesn't work when connecting slot to readyRead()
Qt 6.11 is out! See what's new in the release blog

Code works using waitForReadyRead(), doesn't work when connecting slot to readyRead()

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.4k Views 3 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.
  • A Offline
    A Offline
    Alexander Wilms
    wrote on last edited by Alexander Wilms
    #1

    I'm trying to read data from a serial connection.

    When I'm using a blocking function, the data is read and processed correctly but I can't display it in the GUI. So I tried reading the data like it's done in the Command Line Reader Async Example. But handleReadyRead() is never called and I don't know why. I'm new to Qt, so I might have overlooked something fundamental.

    void Window::read()
    {
        QSerialPort serialPort;
        serialPort.setPortName("COM4");
    
        serialPort.open(QIODevice::ReadOnly);
    
        // omitted some serial port configuration
    
        //    while(1)
        //    {
        //        if(serialPort.waitForReadyRead(1000))
        //        {
        //            //Data was returned
        //            QByteArray input = serialPort.readAll();
        //            //qDebug()<<"Response: "<< input;
        //            QList<QByteArray> extracted = input.split('\n');
        //            //qDebug()<<"Response: "<< extracted;
        //            qDebug()<<"Response: "<< extracted.at(1);
        //        }
        //    }
    
        connect(&serialPort, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
    }
    
    
    void Window::handleReadyRead()
    {
        QByteArray input = serialPort.readAll();
        //qDebug()<<"Response: "<< input;
        QList<QByteArray> extracted = input.split('\n');
        //qDebug()<<"Response: "<< extracted;
        qDebug()<<"Response: "<< extracted.at(1);
    }
    
    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      I suggest you to use waitForReadyRead() in a thread without the events approach.
      If you don't want to use threads please use readyRead() event without waitForReadyRead().

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alexander Wilms
        wrote on last edited by
        #3

        Thanks, I'll try usig threads.

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

          Hi and welcome to devnet,

          There's no need for threads here. What happens is that you declare serialPort in your read function thus it's a local variable that will be destroyed at the end of the function.

          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
          2

          • Login

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