成果图:
关键语句:
用来检测错误信息
qDebug()<<my_serialPort_w->error()<<endl;
解决粘包问题:
requestData_w.append(my_serialPort_w->readAll());
int totol_size_w = requestData_w.size();
base_size_w = 14;
if(totol_size_w<base_size_w)
{
qDebug()<<"消息太短"<<endl;
return;
}
else
{
#if 1
//收到数据后解析
// ui->lineEdit_PITCH->setText(requestData_w);
// ui->textEdit_gps->append(QString("时:%1, 分:%2, 经度:%3%4,纬度:%5%6").arg(gpsInfo.utc.hour).arg(gpsInfo.utc.sec).arg(gpsInfo.longitude).arg(gpsInfo.ewhemi).arg(gpsInfo.latitude).arg(gpsInfo.nshemi));
Luopan_msg LPMSG;
Luopan_msg_2 LPMSG_2;
if(HEX_str_flag == 1) //转换为HEX形式
{
requestData_w = requestData_w.toHex();
}
qDebug()<<"进入罗盘分析收到消息准备分析"<<endl;
luopan_anlyse_2(&LPMSG_2,requestData_w);
if(QString("1")==LPMSG_2.Pitch.pms)
{
qDebug()<<"进入罗盘分析的继续分析啊啊啊啊啊啊啊啊"<<endl;
ui->lineEdit_PITCH->setText(QString("-%1.%2").arg(LPMSG_2.Pitch.integers).arg(LPMSG_2.Pitch.decimals));
}
else
{
qDebug()<<"进入罗盘分析的继续分析22222222222"<<endl;
qDebug()<<"进入罗盘分析的继续分析22222222222"<<LPMSG_2.Pitch.integers<<endl;
ui->lineEdit_PITCH->setText(QString("%1.%2").arg(LPMSG_2.Pitch.integers).arg(LPMSG_2.Pitch.decimals));
}
if(QString("1")==LPMSG_2.Roll.pms)
{
ui->lineEdit_ROLL->setText(QString("-%1.%2").arg(LPMSG_2.Roll.integers).arg(LPMSG_2.Roll.decimals));
}
else
{
ui->lineEdit_ROLL->setText(QString("%1.%2").arg(LPMSG_2.Roll.integers).arg(LPMSG_2.Roll.decimals));
}
if(QString("1")==LPMSG_2.Heading.pms)
{
ui->lineEdit_HEADING->setText(QString("-%1.%2").arg(LPMSG_2.Heading.integers).arg(LPMSG_2.Heading.decimals));
}
else
{
ui->lineEdit_HEADING->setText(QString("%1.%2").arg(LPMSG_2.Heading.integers).arg(LPMSG_2.Heading.decimals));
}
//QByteArray LPMSG_BYTE_W;
#endif
// qDebug()<<"收到的每帧包长度为:"<<totol_size_w<<endl;
if(HEX_str_flag == 1) //转换为HEX形式
{
requestData_w = requestData_w.toHex();
}
if(requestData_w.length()!=0)//如果读取到有数据,就显示出来。
{
qDebug()<<"收到的数据为"<<requestData_w<<endl;
//经测试收到的数据长短不一需要组包
ui->textEdit_luopan->moveCursor(QTextCursor::End);
ui->textEdit_luopan->insertPlainText(requestData_w);
}
requestData_w = requestData_w.right(totol_size_w-base_size_w);
totol_size_w = requestData_w.size();
// showGpsInfo(meaMsg);
// ui->textEdit_read->append(requestData);
}
#endif
ui->lineEdit_PITCH->setText(QString("-%1.%2").arg(LPMSG_2.Pitch.integers).arg(LPMSG_2.Pitch.decimals));
私有函数:用来类内部访问
通过重写定时器事件进行循环:
分三步:
- virtual void timerEvent(QTimerEvent *event);
- 设置 this->id1 = this->startTimer(1000);
- if(this->id1==event->timerId())
{
qDebug()<<“asdfasdfasdfad”<<endl;
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
virtual void timerEvent(QTimerEvent *event);
private:
Ui::Widget *ui;
int id1;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->id1 = this->startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(this->id1==event->timerId())
{
qDebug()<<"asdfasdfasdfad"<<endl;
}
}
qstring的函数使用
extern void luopan_anlyse_2(Luopan_msg_2* msg,QByteArray buf)
{
//将qbytearry转为qstring方便通过mid提取内容
QString temp = QString(buf);
qDebug()<<"进入罗盘分析2"<<endl;
if(temp.mid(6,2)==QString("84"))
{
qDebug()<<"进入罗盘分析888888888888888888888888888"<<endl;
qDebug()<<"dddddddddddd"<<temp<<endl;
qDebug()<<"rrrrrrrrrrrrrrrrrr"<<temp.mid(8,6).left(1)<<endl;
msg->Pitch.pms = temp.mid(8,6).left(1);
msg->Pitch.integers = temp.mid(8,6).mid(1,3);
msg->Pitch.decimals = temp.mid(8,6).mid(4,5);
msg->Roll.pms = temp.mid(14,6).left(1);
msg->Roll.integers = temp.mid(14,6).mid(1,3);
msg->Roll.decimals = temp.mid(14,6).mid(4,5);
msg->Heading.pms = temp.mid(20,6).left(1);
msg->Heading.integers = temp.mid(20,6).mid(1,3);
msg->Heading.decimals = temp.mid(20,6).mid(4,5);
}
}