接收不同的帧并逐字节读取它们

时间:2019-03-26 16:14:38

标签: c++ serial-port qt5

我从Xbee serialPort接收视频,我必须接收一些帧,但是当我显示它们时,我只能接收$​​ GPGGA帧,有时会被剪切。

结果,我需要一个QString用于不同的帧。我需要该框架才能在应用程序上显示它们,但是在显示它们之前,我必须通过计算校验和来解码它们...

这是用于Windows 64位的Qt5。

//serialport.cpp
#include "serialport.h"

serialport::serialport()
{

}
bool serialport::serialConnect()
{
com = new QSerialPort();
status = true;

com->setPortName("COM11");
com->affichersetBaudRate(QSerialPort::Baud9600);
com->setDataBits(QSerialPort::Data8);
com->setParity(QSerialPort::NoParity);
com->setStopBits(QSerialPort::OneStop);
com->setFlowControl(QSerialPort::NoFlowControl);

status = com->open(QIODevice::ReadOnly);
qDebug("<debug> etat ouverture port : %d", com->isOpen());

return status;
}
QString serialport::serialRead()
{

QByteArray donnees;

/*while (com->canReadLine())
{
    donnees += com->readLine();
    Sleep(10000);
}*/

while (com->bytesAvailable())
{
    donnees += com->readAll();
    Sleep(10000);
}

QString trameRecue = QString(donnees.data());

return trameRecue;
}

serialport::~serialport()
{

}

//telemetriemoto.cpp (mainwindow)

#include "telemetriemoto.h"
#include "ui_telemetriemoto.h"


telemetriemoto::telemetriemoto(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::telemetriemoto), timer(new QTimer(this))
{
ui->setupUi(this);
connect( timer, &QTimer::timeout, this, &telemetriemoto::TimerSlot);

comPort=new serialport();

}

telemetriemoto::~telemetriemoto()
{
delete ui;
}

void telemetriemoto::on_pushButton_clicked()
{
bool comPort_status;
comPort_status=comPort->serialConnect();
if(comPort_status == 1)
{
    timer->start(1000);
}
else
{
    ui->label->setText("erreur de connexion");
}
}

void telemetriemoto::TimerSlot()
{
QString trame_recu;
trame_recu  = comPort->serialRead();

ui->label->setText(trame_recu);

}


//i need to recive all the frame bytes by bytes

0 个答案:

没有答案