以Cat62格式雷达数据为例,十六进制雷达数据部分代码:
3e0120bf7da4ffee0085
雷达数据长度使用4个字符(2个字节)标识,在这里是“0120”,转换为十进制数为288。
雷达数据长度父类:
base_length_processor.h
//
// Created by qiaowei on 2024-02-02.
//
#ifndef RADARDATACONTROLLER_BASE_LENGTH_PROCESSOR_H
#define RADARDATACONTROLLER_BASE_LENGTH_PROCESSOR_H
#include <QString>
namespace processor {
class BaseLengthProcessor {
public:
virtual int processor(const QString& track_data,
int begin_position_index,
int& readed_data_end_position) = 0;
};
} // processor
#endif //RADARDATACONTROLLER_BASE_LENGTH_PROCESSOR_H
cat_62_length_processor.h
//
// Created by qiaowei on 2024-02-02.
//
#ifndef RADARDATACONTROLLER_CAT_62_LENGTH_PROCESSOR_H
#define RADARDATACONTROLLER_CAT_62_LENGTH_PROCESSOR_H
#include <QObject>
#include <QString>
#include "../baseprocess/base_length_processor.h"
namespace processor {
class Cat62LengthProcessor : public QObject, public BaseLengthProcessor {
public:
explicit Cat62LengthProcessor(QObject* parent = nullptr);
virtual ~Cat62LengthProcessor() override;
/**
* @author qiao wei
* @brief 获取Cat62格式雷达数据长度。返回值单位为字节(注意字符串中字节与字符索引区别)。
* @param track_data 雷达数据。
* @param begin_position 雷达数据中保存雷达长度的字符串首字符索引。
* @param readed_data_end_position 雷达数据中保存雷达长度的字符串末字符索引。
* @return 返回该雷达数据的字节数量。雷达数据不完整时返回-1。
* @history
*/
virtual int processor(const QString& track_data,
int begin_position,
int& readed_data_end_position) override;
};
} // processor
#endif //RADARDATACONTROLLER_CAT_62_LENGTH_PROCESSOR_H
cat_62_length_processor.cpp
//
// Created by qiaowei on 2024-02-02.
//
#include "cat_62_length_processor.h"
namespace processor {
Cat62LengthProcessor::Cat62LengthProcessor(QObject* parent) : QObject(parent) {}
Cat62LengthProcessor::~Cat62LengthProcessor() {}
int Cat62LengthProcessor::processor(const QString& track_data,
int begin_position,
int& readed_data_end_position) {
// 初始化索引。长度项初始位索引赋值给长度项末尾索引。
readed_data_end_position = begin_position;
// 读取雷达数据的字节长度。字节长度为字符长度的一半。保存雷达数据长度的字符有4个,2个字节。
const int track_data_length{track_data.mid(begin_position, 4).toInt(nullptr, 16)};
// 雷达数据中数据长度项最后一位字符的索引。
readed_data_end_position = begin_position + 4 - 1;
if (track_data_length == track_data.length() / 2) {
return track_data_length;
} else {
// 雷达数据不完整返回-1。
return -1;
}
}
} // processor
main.cpp
#include <string>
#include <QApplication>
#include <QVector>
#include <QtDebug>
#include <QString>
#include "./form/main_window.h"
#include "./cat62process/cat62_fspec_processor.h"
#include "./cat62process/cat_62_length_processor.h"
#include "./cat62process/cat_62_header_processor.h"
using std::string;
using form::MainWindow;
using processor::Cat62FspecProcessor;
using processor::Cat62HeaderProcessor;
using processor::Cat62LengthProcessor;
int main(int argc,
char *argv[]) {
QApplication a(argc, argv);
// MainWindow* main_window{new MainWindow{nullptr}};
// main_window->show();
QString content = "3e0120bf7da4ffee0085988";
int begin_position{0};
int end_position = begin_position;
Cat62HeaderProcessor* header_processor{new Cat62HeaderProcessor{}};
qDebug()<< header_processor->processor(content, begin_position, end_position);
begin_position = end_position + 1;
Cat62LengthProcessor* lenght_processor{new Cat62LengthProcessor{}};
qDebug()<< lenght_processor->processor(content, begin_position, end_position);
begin_position = end_position + 1;
Cat62FspecProcessor* fspec_processor{new Cat62FspecProcessor{}};
QVector<int>* vector = fspec_processor->processor(content, begin_position, end_position);
for (int index = 0; index < vector->capacity(); ++index) {
qDebug()<< index << ": " << vector->at(index) << ", ";
}
int i{20};
return QApplication::exec();
}
运行结果: