Results 1 to 4 of 4

Thread: qserialport and reading bytes

  1. #1
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default qserialport and reading bytes

    Good Morning,
    i'm new in RS232 Programming.

    I need to read data from a serial RS232 hardware.
    The hardware can send 2 types of messages with a variable size
    the first is 20 bytes (binary notation)
    the other is 15 bytes. (binary notation)
    Every message has LF in the last byte.

    What is the best way to read theese data and reconstruct the messages ?

    Is there any example showing how to read from a serial a variable number of bytes ?

    i see :
    QByteArray inByteArray = serial->readAll();

    but in these way i dont' know how many bytes are read...and how to find the LF character to build the message to decode.
    I think the readAll can also contains pieces of two messages...

    Thanks in advance

    to init the serial i use :

    void MainWindow::initSerialPort()
    {
    serial->setPort("COM1");
    if (serial->open(QIODevice::ReadWrite))
    {
    serial->setRate(SerialPort::Rate19200);
    serial->setDataBits(SerialPort:ata8);
    serial->setParity(SerialPort::NoParity);
    serial->setStopBits(SerialPort::OneStop);
    serial->setFlowControl(SerialPort::NoFlowControl);
    }

    else {
    serial->close();
    QMessageBox::critical(this, tr("Error"),
    tr("Can't configure the serial port: %1,\n"
    "error code: %2")
    .arg("COM1").arg(serial->error()));
    ui->statusBar->showMessage(tr("Open error"));
    }

    }

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qserialport and reading bytes

    Quote Originally Posted by gab74 View Post
    i see :
    QByteArray inByteArray = serial->readAll();

    but in these way i dont' know how many bytes are read
    QByteArray::count(), QByteArray::size() or QByteArray::length()
    Your choice :-)

    Quote Originally Posted by gab74 View Post
    ...and how to find the LF character to build the message to decode.
    QByteArray::indexOf()

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: qserialport and reading bytes

    Ok in this way i can parse the received array of bytes and find the initial and final characher to identify a message.

    Do you think is this the best solution ?

    In onother way, I can read one byte at time...and parse one byte ... ?? What do you think about this ?

    Thank you very much !!!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qserialport and reading bytes

    The QSerialPort class will very likely have an internal buffer, so reading byte-per-byte shouldn't be that much different.

    I think this will mostly be a matter of what you find easier to implement.

    Cheers,
    _

Similar Threads

  1. Qt5 cmake and QSerialPort
    By Chris.Burner in forum Newbie
    Replies: 1
    Last Post: 21st April 2013, 17:13
  2. Replies: 4
    Last Post: 23rd October 2012, 09:40
  3. reading bytes out of a file
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2009, 17:55
  4. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 21:25
  5. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 06:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.