Why the Qt(4.8) serial port readind data from device in 2 part?



  • Hi
    this is my code : (I used qextserialport)

    SerialPort::SerialPort(QString port)
    {
        sp=new QextSerialPort;
        sp->setPortName(port);
        /*port->setBaudRate(BAUD9600);
        port->setFlowControl(FLOW_OFF);
        port->setParity(PAR_NONE);
        port->setDataBits(DATA_8);
        port->setStopBits(STOP_2);*/
        sp->setBaudRate(BAUD115200);
        sp->setDataBits(DATA_8);
        sp->setFlowControl(FLOW_OFF);
        sp->setParity(PAR_NONE);
        sp->setStopBits(STOP_1);
        sp->open(QIODevice::ReadWrite);
        connect(sp,SIGNAL(readyRead()),this,SLOT(ReadData()));
    
    }
    
    void SerialPort::ReadData()
    {
        QByteArray ba;
        ba.resize(512);
        ba=sp->readAll();
        emit DataRecieve(ba);
    }
    

    this code run on Ubuntu at first and show all the bytes which receive from device, but when I tested it in linux embedded device this code read data from serial device in 2 part. () there is no \n in my bytes.
    How can I solve this problem?



  • how much size of data u are reading



  • some times 48 bit:

    "aa552401040000000000000000000000"
    "0000000000002801"

    some time512 bit



  • void SerialPort::ReadData()
    {
    if (serial->bytesAvailable()>0||serial->waitForReadyRead(10000))
    {
    QByteArray ba;
    ba=serial->readAll();
    }
    }
    try this



  • No. there is no diffrent :

    4
    "aa5524010400000000000000"
    12
    "000000000000000000002801"
    

    for this code:

    void SerialPort::ReadData()
    {
        QByteArray ba;
        ba.resize(512);
        if(sp->bytesAvailable()>0 || sp->waitForReadyRead(20000))
        {
        qDebug()<<sp->bytesAvailable();
        ba=sp->readAll();
        qDebug()<<ba.toHex();
        emit DataRecieve(ba);
        }
    }
    

  • Qt Champions 2016

    Hi
    There is never a guarantee that bytes sent on a
    serial line is received as one stream/read.

    Therefore it is often needed to include data markers in the input to
    know when all has been read or use fixed size blocks.

    So when readyRead signal is sent. you read what there is with readAll() and append to a buffer.

    if enough data is read OR data marker seen, then you emit DataRecieve().



  • @mrjj thanks for help. I think maybe I have mistake in my code. so often the serial port can not read all of the data? . My serial device always send check sum at end of data, so I should check the data that receive and if the checksum is not true again listen run port.readall and attach the second data to the first and if it is true then emit the data.
    Is it true?


  • Qt Champions 2016

    @MhM93

    hi
    Its normal that not all data can be read in one go. Often seen when moving from
    PC to small(er) board.

    The checksum sounds right way to know when all data been read or to catch transmission errors.
    And yes, the logic if like that. On readyRead, read what there is. Append to the running buffer ( a member variable). When checksum is ok, emit signal. This logic will work fine as long there is
    no chance that another data is sent while we wait for the rest of the first one.



  • thanksss a lotttt :)


Log in to reply
 

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