Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Read Serial Data In QByteArray



  • every Time Serial Triggered Receive This Message 000011110000111100001111& but i receive it Like that in debug alt text
    when i Do qDebug() << inBuffer ; out side of ReadyRead(); i got only Last byte "&"
    so how to got all Message in QByte array like that "000011110000111100001111&"
    My code :

    void Widget::updateReceivedData()
    {
        
       inBuffer = arduino->readAll();
       qDebug() << inBuffer ;
    
        //inBuffer = arduino->readLine();
    
        //QByteArray datalog(arduino->readAll());
    
       //QByteArray data ;
      // data.append(arduino->readAll());
     
      //while (arduino->waitForReadyRead(10))
      //inBuffer += arduino->readAll();
    
        }
    
    

  • Lifetime Qt Champion

    @Marco-Flad your serial data arrives in chunks of one or several bytes, but seldom the whole answer in one block.

    So you have to append the new data to your existing buffer until all data is received.

    Regards


  • Lifetime Qt Champion

    Hi,

    Are you receiving null chars in your message ?



  • use readyRead() SIGNAL at the time of serial->open(QIODevice::ReadWrite)
    connect(serial, SIGNAL(readyRead()),this,SLOT(readSerialData()),Qt::UniqueConnection);
    where serial is an object QSerialPort *serial;
    readSerialData() is user defined slot function make it in header file like
    private slots:
    void readSerialData();
    and finally in readSerialData() function
    static QString readResponseReply = "";
    readResponseReply.append(serial->readAll());
    qDebug()<<"readResponseReply";
    try it in your source code


  • Lifetime Qt Champion

    @vikas-dhumal Sorry but converting some binary data into a QString is completely wrong here. What @SGaist asked is correct - when there is a null byte in there, qDebug() will not print what you expect. Therefore I would use inBuffer.toHex() to output the data for debugging.


  • Lifetime Qt Champion

    @Marco-Flad your serial data arrives in chunks of one or several bytes, but seldom the whole answer in one block.

    So you have to append the new data to your existing buffer until all data is received.

    Regards



  • ok so instead of QString read it in QByteArray


Log in to reply