repay
This commit is contained in:
@@ -34,6 +34,32 @@
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Raised</enum>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLineEdit" name="lineEdit"/>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="pushButton_2">
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QSlider" name="horizontalSlider">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
||||
@@ -85,7 +85,7 @@ MavLinkNode::~MavLinkNode()
|
||||
{
|
||||
if(Commander->isActive())
|
||||
{
|
||||
Commander->stop();
|
||||
Commander->stop();//可能还没停止线程,后面不能紧跟着删除
|
||||
delete Commander;
|
||||
Commander = nullptr;
|
||||
}
|
||||
@@ -292,6 +292,8 @@ void MavLinkNode::Mavlinkparse(quint32 src,QByteArray datagram)
|
||||
mavlink_message_t msg;
|
||||
mavlink_status_t status;
|
||||
|
||||
//qDebug() << datagram;
|
||||
|
||||
for (QByteArray::const_iterator i = datagram.cbegin(); i != datagram.cend(); ++i)
|
||||
{
|
||||
if(MAVLINK_FRAMING_OK == mavlink_parse_char(src,*i,&msg,&status))
|
||||
|
||||
+69
-10
@@ -16,6 +16,7 @@ DLink::DLink(QObject *parent) : QObject(parent)
|
||||
}
|
||||
|
||||
|
||||
|
||||
connect(this,SIGNAL(recieveMessage(quint32,QByteArray)),
|
||||
this,SLOT(record(quint32,QByteArray)));
|
||||
|
||||
@@ -41,12 +42,15 @@ DLink::DLink(QObject *parent) : QObject(parent)
|
||||
mavlinknode->start();
|
||||
//其他协议节点。。。
|
||||
|
||||
|
||||
|
||||
replay("./Tlog/20200818210716.tlog");
|
||||
|
||||
|
||||
}
|
||||
DLink::~DLink()
|
||||
{
|
||||
stopPort();
|
||||
|
||||
|
||||
//查询所有的接口,然后删除
|
||||
if (mavLogFile)
|
||||
{
|
||||
@@ -63,12 +67,14 @@ DLink::~DLink()
|
||||
int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
||||
{
|
||||
//让这个函数在其他线程运行
|
||||
/*
|
||||
QString num;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
num.append(QString::number(msg[i],16).toUpper());
|
||||
num.append(" ");
|
||||
}
|
||||
qDebug() << num;
|
||||
*/
|
||||
//Q_UNUSED(ch);
|
||||
|
||||
//更加ch选择
|
||||
@@ -294,6 +300,8 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
}
|
||||
else
|
||||
{
|
||||
Clientsock->leaveMulticastGroup(remote_addr);
|
||||
Clientsock->close();
|
||||
isSuccess = false;
|
||||
qWarning() << "sock not open";
|
||||
}
|
||||
@@ -301,6 +309,8 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
else
|
||||
{
|
||||
isSuccess = false;
|
||||
Clientsock->leaveMulticastGroup(remote_addr);
|
||||
Clientsock->close();
|
||||
qWarning("sock is not binded.");
|
||||
delete Clientsock;
|
||||
}
|
||||
@@ -311,9 +321,11 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
|
||||
void DLink::readPendingDatagramsSerialPort(void)
|
||||
{
|
||||
QByteArray datagram = serialPort->readAll();
|
||||
|
||||
emit recieveMessage(SourceType::s_port,datagram);
|
||||
if(serialPort)
|
||||
{
|
||||
QByteArray datagram = serialPort->readAll();
|
||||
emit recieveMessage(SourceType::s_port,datagram);
|
||||
}
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsClient(void)
|
||||
@@ -328,22 +340,69 @@ void DLink::readPendingDatagramsClient(void)
|
||||
}
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsReplay(void)
|
||||
{
|
||||
|
||||
if(play)
|
||||
{
|
||||
QByteArray datagram = play->readAll();
|
||||
emit recieveMessage(SourceType::c_sock,datagram);
|
||||
//qDebug() << "play" << datagram;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if(play)
|
||||
{
|
||||
QByteArray datagram = play->readAll();
|
||||
|
||||
if(serialPort)
|
||||
{
|
||||
serialPort->write(datagram);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
void DLink::record(quint32 src,QByteArray data)
|
||||
{
|
||||
Q_UNUSED(src)
|
||||
|
||||
//加时间撮
|
||||
|
||||
if (mavLogFile)
|
||||
{
|
||||
char buff[sizeof(quint64)];
|
||||
|
||||
quint64 currentTimestamp = ((quint64)QDateTime::currentMSecsSinceEpoch()) * 1000;
|
||||
qToBigEndian(currentTimestamp, buff);
|
||||
|
||||
data.append(buff);
|
||||
|
||||
//使用数据流的方式可能更好
|
||||
mavLogFile->write((const char *)data, sizeof(data));
|
||||
mavLogFile->write((const char *)data,data.size());
|
||||
//qDebug() << data;
|
||||
}
|
||||
}
|
||||
|
||||
void DLink::replay(quint32 src,QByteArray data)
|
||||
void DLink::replay(QString file)
|
||||
{
|
||||
//定时器回放
|
||||
//得到文件的路
|
||||
if(!play)
|
||||
{
|
||||
play = new Replay();
|
||||
|
||||
connect(play,&Replay::readReady,
|
||||
this,&DLink::readPendingDatagramsReplay);
|
||||
}
|
||||
|
||||
play->setLogfile(file);
|
||||
//使用多线程进行读取数据
|
||||
play->start();//开始线程
|
||||
|
||||
/*QThread::sleep(100);
|
||||
play->stop();
|
||||
QThread::sleep(100);
|
||||
play->start();*/
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -8,6 +8,8 @@
|
||||
#include "QDebug"
|
||||
#include "mavlinknode.h"
|
||||
|
||||
#include "replay.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
|
||||
@@ -70,12 +72,15 @@ public slots:
|
||||
|
||||
void readPendingDatagramsClient(void);
|
||||
|
||||
void readPendingDatagramsReplay(void);
|
||||
|
||||
|
||||
int SendMessageTo(quint8 ch, quint8 *msg, quint16 len);
|
||||
|
||||
private slots:
|
||||
|
||||
void record(quint32 src,QByteArray data);
|
||||
void replay(quint32 src,QByteArray data);
|
||||
void replay(QString file);
|
||||
|
||||
|
||||
|
||||
@@ -84,8 +89,10 @@ private slots:
|
||||
protected:
|
||||
|
||||
enum SourceType{
|
||||
|
||||
c_sock = 0,
|
||||
s_port = 1
|
||||
s_port = 1,
|
||||
p_port = 2,
|
||||
};
|
||||
|
||||
|
||||
@@ -97,6 +104,8 @@ protected:
|
||||
|
||||
QFile *mavLogFile = NULL;
|
||||
|
||||
Replay *play = nullptr;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
+4
-2
@@ -45,10 +45,12 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/dlink.h \
|
||||
dlinkglobal.h
|
||||
$$PWD/dlinkglobal.h \
|
||||
$$PWD/replay.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/dlink.cpp
|
||||
$$PWD/dlink.cpp \
|
||||
$$PWD/replay.cpp
|
||||
|
||||
#加入第三方库
|
||||
INCLUDEPATH += $$PWD/../thirdpart/include
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "Replay.h"
|
||||
|
||||
|
||||
Replay::Replay(QObject *parent) : QObject(parent)
|
||||
{
|
||||
//main thread
|
||||
running_flag = false;
|
||||
thread = new QThread();
|
||||
this->moveToThread(thread);
|
||||
connect(thread, SIGNAL(started()), this, SLOT(process()));
|
||||
setRunFrq(20);//100
|
||||
|
||||
logFile.clear();
|
||||
}
|
||||
|
||||
Replay::~Replay()
|
||||
{
|
||||
if(thread->isRunning())
|
||||
{
|
||||
thread->quit();
|
||||
thread->wait();
|
||||
}
|
||||
delete thread;
|
||||
thread = nullptr;
|
||||
}
|
||||
|
||||
//线程相关函数
|
||||
void Replay::setRunFrq(uint32_t frq)
|
||||
{
|
||||
if((frq != 0)||(frq <= 1000))
|
||||
{
|
||||
running_frq = frq;
|
||||
qDebug() << "set thread running frquency:" <<frq <<"Hz";
|
||||
}
|
||||
}
|
||||
|
||||
void Replay::start()
|
||||
{
|
||||
if(!thread)
|
||||
{
|
||||
thread = new QThread();
|
||||
this->moveToThread(thread);
|
||||
connect(thread, SIGNAL(started()), this, SLOT(process()));
|
||||
}
|
||||
|
||||
if(!thread->isRunning())
|
||||
{
|
||||
if(!file)
|
||||
{
|
||||
file = new QFile();
|
||||
file->setFileName(logFile);
|
||||
if(!file->open(QIODevice::ReadOnly))
|
||||
{
|
||||
QMessageBox::warning(nullptr,
|
||||
tr("Error"),
|
||||
tr("%1").arg(file->errorString()));
|
||||
|
||||
delete file;
|
||||
file = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
running_flag = true;
|
||||
thread->start();
|
||||
qDebug() << "thread start" << running_flag;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "thread has started";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Replay::stop()
|
||||
{
|
||||
if(thread->isRunning())
|
||||
{
|
||||
running_flag = false;
|
||||
qDebug() << "thread stop" << running_flag;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "thread is not running";
|
||||
}
|
||||
}
|
||||
|
||||
void Replay::process()//线程函数
|
||||
{
|
||||
uint8_t count = 0;
|
||||
while (running_flag)
|
||||
{
|
||||
count ++;
|
||||
QThread::msleep(1000/running_frq);
|
||||
//读取数据放到缓存里面
|
||||
|
||||
if(file)//已经有文件,那么可以读取
|
||||
{
|
||||
if(position <= file->size())
|
||||
{
|
||||
buff.clear();
|
||||
file->seek(position);
|
||||
buff = file->read(100);
|
||||
position += 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
running_flag = false;//换成另外的标志
|
||||
}
|
||||
|
||||
qDebug() << position << file->size();
|
||||
//读取一帧数据
|
||||
//将百分比增加,或者位置增加
|
||||
}
|
||||
//发送就绪信号
|
||||
emit readReady();
|
||||
}
|
||||
//退出线程
|
||||
thread->quit();
|
||||
thread->wait();
|
||||
}
|
||||
|
||||
//类相关函数
|
||||
|
||||
QByteArray Replay::readAll(void)
|
||||
{
|
||||
//把buff里面的数据返回
|
||||
QByteArray data;
|
||||
|
||||
data.clear();
|
||||
if(!buff.isEmpty())
|
||||
{
|
||||
data.append(buff);
|
||||
buff.clear();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void Replay::setLogfile(QString name)
|
||||
{
|
||||
logFile = name;
|
||||
//重置各种参数
|
||||
percentage = 0;
|
||||
ispause = false;
|
||||
isplaying = false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#ifndef REPLAY_H
|
||||
#define REPLAY_H
|
||||
|
||||
#include <QObject>
|
||||
#include "QThread"
|
||||
#include "QMessageBox"
|
||||
#include "QDebug"
|
||||
#include "QFile"
|
||||
#include "replay.h"
|
||||
#include "QDataStream"
|
||||
|
||||
#ifdef QtDlink
|
||||
#include <dlinkglobal.h>
|
||||
class DLINKSHARED_EXPORT Replay : public QObject {
|
||||
#else
|
||||
class Replay : public QObject
|
||||
{
|
||||
#endif
|
||||
Q_OBJECT
|
||||
|
||||
typedef struct {
|
||||
qint32 max_size;
|
||||
quint8 select;
|
||||
QByteArray buff[2];
|
||||
}_buffdef;
|
||||
|
||||
|
||||
public:
|
||||
explicit Replay(QObject *parent = nullptr);
|
||||
~Replay();
|
||||
|
||||
|
||||
QByteArray readAll(void);
|
||||
void setLogfile(QString name);
|
||||
|
||||
|
||||
|
||||
signals:
|
||||
void showMessage(const QString &message,int TimeOut = 0);
|
||||
|
||||
|
||||
void readReady();
|
||||
|
||||
public slots:
|
||||
//线程对外接口
|
||||
void setRunFrq(uint32_t frq);
|
||||
void start();
|
||||
void stop();
|
||||
bool isActive(void)
|
||||
{
|
||||
return running_flag;
|
||||
}
|
||||
//缓存对外接口
|
||||
|
||||
private slots:
|
||||
//线程私有接口
|
||||
void process();
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
bool running_flag = false;
|
||||
quint32 running_frq = 200;//200Hz
|
||||
|
||||
QThread *thread = nullptr;
|
||||
|
||||
QString logFile;
|
||||
|
||||
QFile *file = nullptr;
|
||||
|
||||
//QDataStream *stream = nullptr;
|
||||
|
||||
|
||||
float percentage = 0;
|
||||
int position = 0;
|
||||
bool ispause = false;//控制暂停
|
||||
bool isplaying = false;//控制开始和结束
|
||||
QByteArray buff;
|
||||
|
||||
};
|
||||
|
||||
#endif // MAVLINKNODE_H
|
||||
Reference in New Issue
Block a user