UART to Qt software error - why data always split?

2019-05-30 11:04发布

问题:

I am tryig to display data I receive via UART on my Qt application. I send all my data from my microcontroller at once, but my qt application receives it in multiple parts why?

this is what I get: http://imgur.com/kLXRvU5 in stead of: http://imgur.com/h2yNZjl

So every time I receive data my slot function gets called, hence the "data received". But my data is split in two parts. Why please?

my code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)//, mijnAnimatie(new animatie())
{  

    serialPort->setPortName("COM13");
        serialPort->setBaudRate(QSerialPort::Baud115200);

       // serialPort->setDataBits(QSerialPort::Data8);
       // serialPort->setParity(QSerialPort::NoParity);
       // serialPort->setStopBits(QSerialPort::OneStop);


        if (!serialPort->open(QIODevice::ReadWrite))
        {
         qDebug("some error when opening\n");
        }
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(updateReceivedData()));

}

void MainWindow::updateReceivedData()
{
    qDebug("received data\n");
    QString m_readData;

    ui->receiveLabel->setText(m_readData);


      QByteArray result = serialPort->readAll();
      QString command(result); //to convert byte array to string
      qDebug()<<result;
      ui->receiveLabel->setText(command);
}

回答1:

Streaming connections, like TCP/IP or serial ports, give you zero guaranties about how the data is cut up into pieces. If you want packets, you have to implement them yourself on top of a streaming connection.