Is this serial port receive method correct (TextEdit+SerialPort) ?



  • Dear Sirs,

    I add a TextEdit to receive data from serial port, the receive slot is as below:

    QByteArray data = serial->readAll();
    ui->textEdit->insertPlainText(data.toHex());
    ui->textEdit->insertPlainText("\n");
    

    I send out "55AA33887799" <--In hex 6 bytes
    I expect to receive the same data then cursor will move to next new line.

    However, in my TextEdit, the received data looks like:

    55AA33887799      //row 1
    55AA338877        //row 2
    99                //row 3
    55AA3388          //row 4
    7799              //row 5
    55AA33887799      //row 6
    

    I did not miss any data, but "new line" happen at wrong position.
    Take above row2 and row3 as an example, I receive row2 and row3 at the same time,
    but it should not separate to two row, all 6 bytes should at row2.

    Could it possible I miss some configuration on TextEdit ?
    Or something wrong on serial receive method ?

    Any suggestions would be appreciate.



  • @Hiloshi
    that is normal behaviour. The ReadyRead signal is emited when new data is available at the serial port. But it checks this only 1 ever eventloop. So you might get a readyRead Signal but you do not yet have all the Data at your port.

    Depending on the protocol you use, one of your first bytes should be an identifier for the expected length of the telegram.

    if available data size < length identifier -> wait for next readyread Signal.



  • Dear @J.Hilk ,

    Understood. I will check number of bytes then determine if I should display it on the TextEdit or not.

    Thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.