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

Correct Way to handle SerialPort messages



  • Hello, I'm a student developing a UI that reads and sends CAN messages via Serial Port. Some of the code was done by other students in the past and I have some doubts about the correct way to handle incoming messages.

    So currently our serial port class reads the message, places it in a queue and signals the presence of a new message, and the class that sends the messages to our widgets runs a slot called pop that dequeues one message from the serial port class. My question is, would it be worse to send the message directly through the signal to our widgets, with no pop function? (like this: "emit new_message(CANdata msg);" CANdata is a struct)

    Thank you for your time.


  • Qt Champions 2017

    It is done to ensure that your UI not locked because of message processing. What they are doing is best practice. Not sure how your project is organised. If UI in one thread and Serial message reading in another thread, you can simply use the signal/slots. Queue is automatically care by the Qt itself.


  • Lifetime Qt Champion

    @DiogoPereira98 the best you can do here is: measure. I doubt the queue is really needed, as serial ports are slow. I would add some debug outputs tracing when queue is enqueued/dequeued and how big it is.

    If it rarely goes over 1...5 elements, its probably not needed.

    Otoh, never change a running code ;)


Log in to reply