How to read synchronously (blocking) from serial port without using threads?

  • Hello,
    I am developing the project in which I am accessing two serial ports , of which one is communicating with modbus slave device and other is communicating with serial device over UART TTL . In my project, on click of push button , first command should be sent from software to serial device and it should wait for the response .Later the modbus command will be sent and software should wait for the response. I need the response data from both the device to calculate and display some data. I have tried using waitforreadyread() and using readyread signal to connect the function for reading the data but to no use. I am unable to store the data in qbytearray.I have seen that in blocking master example , thread is used. Is there any way to block the master until it receives the entire data? Unresponsiveness of GUI is fine with me till the data is received.

  • Lifetime Qt Champion

    @iomkar from your description, you dont need threads nor blocking. just connect a slot to the readyRead signal and process the answer there. then send out the next command the same way.


  • @aha_1980 hello, Thank you for your response.I had tried that but issue is that I want to block the code till the entire data is received,because I have other commands to execute only after the data is received. Using just readyRead signal and slot to read the data does not stop the code and later commands are being executed. I have used waitforreadyRead to stop the execution till the data is received. While I was trying different things after posting query here, when I put some delay before sending response in my slave serial device which is nothing but microcontroller, I receive the data correctly but when the delay is removed, I don't get any data on my qtserialport.
    Is there something like after transmitting data from qtserialport, rx buffer of qtserialport is disabled for some duration before it starts accepting data?
    Please let me know if any of above is not clearly described to you

  • Lifetime Qt Champion

    @iomkar once again, don't block the main thread! you already noticed that does not work. (and even if it works, it may break anytime)

    simplest solution for your case would be to send the next command from the slot when the first one has finished.

Log in to reply