UART至Qt软件错误-为什么数据总是分裂? [英] UART to Qt software error - why data always split?
问题描述
我很想在我的Qt应用程序上显示通过UART接收到的数据. 我一次从微控制器发送了所有数据,但是我的qt应用程序却分成多个部分接收它,为什么?
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?
这就是我得到的: http://imgur.com/kLXRvU5 代替: http://imgur.com/h2yNZjl
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?
我的代码:
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);
}
推荐答案
流式连接(例如TCP/IP或串行端口)为您提供零保证如何将数据切成碎片.如果需要数据包,则必须在流连接之上自己实现它们.
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.
这篇关于UART至Qt软件错误-为什么数据总是分裂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!