Results 1 to 3 of 3

Thread: QSerialport read/readline/readall missing first char.

  1. #1
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QSerialport read/readline/readall missing first char.

    I am an issue with reading from an RS232 serial port

    I have an embedded app which is producing a null terminated string as follows.

    ##123456789$ (with zero at end)

    I use...

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial = new QSerialPort(this);
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); // Confgure signal
    }

    to create the serial object.

    The following code configures..

    QString serialPortName = "COM11";
    serial->setPortName(serialPortName);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort:ata8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);


    I open the port with thew following:

    if (serial->open(QIODevice::ReadOnly))
    {
    ui->statusBar->showMessage(tr("Comms Connected"));
    }


    and use the following to print the resulting string on a linedit on the form for debuggting purposes..

    void MainWindow::readData()
    {
    QByteArray data = serial->readLine(); //Get data from serial
    ui->lineEdit->setText(data); //Put data on linedit

    }

    My issue is, I am ALWAYS losing the first charachter (The first # in this case)

    I have also tried ReadAll and still loses the first character, whatever it is, however long the string.

    To verify the RS232 data I am using RealTerm (a dedicated serial terminal program from sourceforge) which is receiving the full string, including the 2 x initial # chars.

    It seems very reliable in that it only ever receives the second char onwards.

    Any suggestions on why this is? (The baud rate is very low to be a synch problem) and how I can change this so it receives the FULL string?

    Alas I am more an embedded assembler programmer, C++ is not my particular forte but Im learning. I used to use C-Builder 15-20 years ago and have forgotten lots but i think this is a QSerialport specific issue.

    iain
    Last edited by moman; 8th October 2015 at 22:28.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSerialport read/readline/readall missing first char.

    The bytes sent by the sender do not necessarily arrive together in a single invocation of the readdata(). When they arrive in several pieces the last piece will win in your code. Put a qDebug() in readdata() to dump the byte array to see how the bytes are actually received.

    You need to buffer received bytes until you can see a full line in the buffer then process it.

  3. The following user says thank you to ChrisW67 for this useful post:

    moman (8th October 2015)

  4. #3
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSerialport read/readline/readall missing first char.

    Hello,

    Superb, thanks for the info, I quickly changed the display to ui->lineEdit->insert(data) instead of ui->lineEdit->setText(data) and I can now see all the chars. Now to buffer, thanks again..

    iain

Similar Threads

  1. how to read one line use the QSerialPort
    By xiongxiongchuan in forum Qt Programming
    Replies: 4
    Last Post: 24th June 2014, 16:47
  2. Use QUdpSocket as a QIODevice (read, readline, write)
    By salcin in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2014, 22:17
  3. QSerialPort read delay
    By M4chin3 in forum Qt Programming
    Replies: 14
    Last Post: 24th January 2014, 19:06
  4. Missng last lines when read file wth readLine()
    By aguayro in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2012, 15:07
  5. QTcpSocket->readAll() doesn't have anything to read!
    By naturalpsychic in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2012, 17:20

Tags for this Thread

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.