Solved Error: QCanBusDevice::waitForFramesReceived() must not be called recursively...
-
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) signalsHas 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 }
-
@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 afterwriteFrame()
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. -
@aha_1980 : Thanks for the answer. But it is not clear for me yet...
I have to return the received frame insendFrame()
to the calling function.
How can I do this, when I receive the answer inslotFramesReceived()
? -
@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) -
-