Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Correct Approach for accessing synchronous data form the serial Port



  • Hello All,

    Problem Statement: Currently i am interfacing with Arduino leonardo via QSerialPort.
    As per the requirement i need to do it in synchronous fashion, means can process the next task only after first task is completed.
    I cannot use the signal and slot mechanism as it works in different thread. So i was using waitForReadyRead() function. I have following queries:

    1. It creates a lag in UI so there a way to avoid it.
      I cannot create a thread as i need to wait for task to be completed.

    2. Can we combine the signal and slot and the waitForReadRead().
      I have read the document and it states that the signal is readReady signal is emmit only once so if i combine signal and slot along with waitForReady(). waitForReady() will go in indefinite wait time as readyReady first gets executed by slot and then it comes to loop of waitForRead().


  • Moderators

    @Kira said in Correct Approach for accessing asynchronous data form the serial Port:

    i need to do it in asynchronous fashion, means can process the next task only after first task is completed.

    This sentence is wrong. If you need to do the task asynchronously, then you shouldn't care about the order of execution. I will reply assuming you mean that the task should be done synchronously.

    I cannot use the signal and slot mechanism as it works in different thread

    This assumption is incorrect. Qt's asynchronous APIs do not use threads, only signals and slots and an event loop. You can put objects in threads, but it is your conscious choice, not a default behaviour.

    It creates a lag in UI so there a way to avoid it.
    I cannot create a thread as i need to wait for task to be completed.

    Make your code as asynchronous as the QSerialPort API if you want to avoid the lag and keep the execution order. That is, leave your data handling to some function, which will respond only to a signal which you emit when previous chunk of data has finished being processed. Then your next chunk will start being processed, then the next one etc. If your processing is too slow, add a QQueue or QBuffer to keep the data waiting to be processed there.



  • @sierdzio : Thanks for the reply.
    Corrected the following part.



  • To me you are describing a rather simple QStateMachine setup


Log in to reply