Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...
Qt 6.11 is out! See what's new in the release blog

Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 3 Posters 2.0k 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.
  • M Offline
    M Offline
    MHermann
    wrote on last edited by
    #1

    Hi!

    At the moment I am implementing the CAN.
    I am writing my frames to the CAN via QCanBusDevice::writeFrame(). In that case I want to receive and evaluate the response immediately.
    At the same time I am using a slot that is connected to the framesReceived()-signal, too. Because I also have to receive and evaluate frames without a request from my side.
    Now I get the following error:
    QCanBusDevice::waitForFramesReceived() must not be called recursively. Check that no slot containing waitForFramesReceived() is called in response to framesReceived() or errorOccurred(CanBusError) signals

    Has anyone an idea how I can solve this problem?

    Here are some code snippets:

    ...
    connect(m_device, SIGNAL(framesReceived(void)), this, SLOT(slotFramesReceived(void)));
    ...
    
    QCanBusFrame Can::sendFrame(QCanBusFrame writeFrame)
    {
        QCanBusFrame recvFrame;
    
        m_device->writeFrame(writeFrame);
        while (m_device->waitForFramesReceived(100)) { // Is true if signal 'framesReceived()' is emitted.
            if (m_device->framesAvailable()) {
                recvFrame = m_device->readFrame();
                // Check if recvFrame contains expected response
                // if (yes)
                    break;
            }
        }
    
        return recvFrame;
    }
    
    void Can::slotFramesReceived(void)
    {   // private slot
    }
    aha_1980A 1 Reply Last reply
    0
    • M MHermann

      Hi!

      At the moment I am implementing the CAN.
      I am writing my frames to the CAN via QCanBusDevice::writeFrame(). In that case I want to receive and evaluate the response immediately.
      At the same time I am using a slot that is connected to the framesReceived()-signal, too. Because I also have to receive and evaluate frames without a request from my side.
      Now I get the following error:
      QCanBusDevice::waitForFramesReceived() must not be called recursively. Check that no slot containing waitForFramesReceived() is called in response to framesReceived() or errorOccurred(CanBusError) signals

      Has anyone an idea how I can solve this problem?

      Here are some code snippets:

      ...
      connect(m_device, SIGNAL(framesReceived(void)), this, SLOT(slotFramesReceived(void)));
      ...
      
      QCanBusFrame Can::sendFrame(QCanBusFrame writeFrame)
      {
          QCanBusFrame recvFrame;
      
          m_device->writeFrame(writeFrame);
          while (m_device->waitForFramesReceived(100)) { // Is true if signal 'framesReceived()' is emitted.
              if (m_device->framesAvailable()) {
                  recvFrame = m_device->readFrame();
                  // Check if recvFrame contains expected response
                  // if (yes)
                      break;
              }
          }
      
          return recvFrame;
      }
      
      void Can::slotFramesReceived(void)
      {   // private slot
      }
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MHermann said in Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...:

      Has anyone an idea how I can solve this problem?

      Simply: Don't mix the waitForXxxx() functions and signals-slots. That applies for all other communication classes (QTcpSocket, QSerialPort, ...) also.

      As you already have a slot slotFramesReceived(), simply check the response there (maybe set a flag after writeFrame() that you expect an answer and clear it once you got the response.

      The waitForXxxx() functions will block the event loop and should therefore only be used in threads.

      Qt has to stay free or it will die.

      M 1 Reply Last reply
      2
      • aha_1980A aha_1980

        @MHermann said in Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...:

        Has anyone an idea how I can solve this problem?

        Simply: Don't mix the waitForXxxx() functions and signals-slots. That applies for all other communication classes (QTcpSocket, QSerialPort, ...) also.

        As you already have a slot slotFramesReceived(), simply check the response there (maybe set a flag after writeFrame() that you expect an answer and clear it once you got the response.

        The waitForXxxx() functions will block the event loop and should therefore only be used in threads.

        M Offline
        M Offline
        MHermann
        wrote on last edited by
        #3

        @aha_1980 : Thanks for the answer. But it is not clear for me yet...
        I have to return the received frame in sendFrame() to the calling function.
        How can I do this, when I receive the answer in slotFramesReceived()?

        jsulmJ 1 Reply Last reply
        0
        • aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MHermann said in Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...:

          I have to return the received frame in sendFrame() to the calling function.

          If you really need to do this (Why?), then you have to move the CAN communication to a second thread. You can use waitForFramesReceived() there.

          As said before, if you call waitForFramesReceived() in the main thread, you block the event loop and will not get new events (in the worst case that could mean, you will not receive CAN frames as you block the processing logic in QCanBusDevice)

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • M MHermann

            @aha_1980 : Thanks for the answer. But it is not clear for me yet...
            I have to return the received frame in sendFrame() to the calling function.
            How can I do this, when I receive the answer in slotFramesReceived()?

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

            @MHermann Agree with @aha_1980 : why do you want to return the response from a sendFrame() function? sendFrame() is responsible for sending, right? I would not expect from it to return the response.

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

            1 Reply Last reply
            1
            • M Offline
              M Offline
              MHermann
              wrote on last edited by MHermann
              #6

              @aha_1980 and @jsulm : I think you are right. I changed my concept and now I am using only signals and slots. Thanks for the hints.

              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