[Qt-interest] Qextserialport read methods

Phil phillor at telstra.com
Tue Nov 24 05:29:52 CET 2009


Thank you for reading this.

Over the years I have used two methods to test when bytes are available to 
read from a serial port.

To use the first method I need to know how many bytes will be received and 
so I wait until 14 bytes, in this example, are received.

do
  {
    numBytes = port->bytesAvailable();

  }while (numBytes < 14);

int i = port->read(buff, numBytes);

I use the second method when I don't know how many bytes are going to be 
received. This is a bit hit and miss because I have to adjust the delay 
depending on the number of bytes and the baud rate.

sleep(1);  // a one second delay is usually longer than necessary here and       
QTimer might be a better option

numBytes = port->bytesAvailable();

if (numBytes > 0)
{
  int i = port->read(buff, numBytes);

I'm happy that the first method is technically correct but the second method 
seems a bit amateurish. Is there a better method when the number of bytes 
to be read is unknown?

-- 
Regards,
Phil



More information about the Qt-interest-old mailing list