0
0
Files
----/test1/test1.cpp

115 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "SimMsg.h"
#include "test1.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstring> // 添加这个头文件用于memset
#include <algorithm> // 用于std::generate
using namespace std;
void test1WriteLog(const std::string &msg)
{
std::cout << msg<<std::endl;
}
string topic_name_cmd = "Command";
string topic_name_tlm = "Telemetry";
SimMsg* test1_part = nullptr;
string servername = "test_Service";
// 声明函数
void command_callback(std::string src, std::string dest, std::string type,
std::string reserve1, std::string reserve2,
std::vector<uint8_t>& data);
void telemetry_callback(std::string src, std::string dest, std::string type,
std::string reserve1, std::string reserve2,
std::vector<uint8_t>& data);
// 为test1生成测试数据从1开始累加最后一位是校验位
void generate_test1_data(uint8_t* buffer, size_t size) {
if (size == 0) return;
// 填充数据从1开始累加
for (size_t i = 0; i < size - 1; i++) {
buffer[i] = (uint8_t)((i + 1) % 256); // 从1开始防止溢出
}
// 计算校验和(除最后一位外的所有字节的和)
uint8_t checksum = 0;
for (size_t i = 0; i < size - 1; i++) {
checksum += buffer[i];
}
// 设置最后一位为校验位
buffer[size - 1] = checksum;
// 打印生成的数据(调试用)
std::cout << "test1生成数据: ";
size_t print_len = size > 20 ? 20 : size;
for (size_t i = 0; i < print_len; ++i) {
std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << (int)buffer[i] << " ";
}
if (size > print_len) {
std::cout << "...";
}
std::cout << std::dec << " (校验位: 0x" << std::hex << (int)checksum << std::dec << ")" << std::endl;
}
//初始化
void test1_init(uint8_t domainid, std::string appname)
{
std::vector<std::string> parameters;
string expression = "src = '"+ servername + "'";
if (nullptr == test1_part)
{
test1_part=new SimMsg(domainid, 3000, appname, test1WriteLog);
test1_part->create_pub(topic_name_cmd);
test1_part->create_pub(topic_name_tlm);
test1_part->create_sub(topic_name_cmd, command_callback, expression, parameters);
test1_part->create_sub(topic_name_tlm, telemetry_callback, expression, parameters);
}
}
// 遥控接收回调函数
void command_callback(std::string src, std::string dest, std::string type,
std::string reserve1, std::string reserve2,
std::vector<uint8_t>& data) {
uint8_t test1_data[256] = {0};
generate_test1_data(test1_data, 256);
uint16_t data_len = 256;
// 发送数据
test1_command_Pub(test1_data, dest, data_len);
std::cout << "test1收到指令已发送数据" << std::endl;
}
// 遥测接收回调函数
void telemetry_callback(std::string src, std::string dest, std::string type,
std::string reserve1, std::string reserve2,
std::vector<uint8_t>& data) {
// 可以保持原样或根据需要修改
}
// AD硬件服务化遥控发布
void test1_command_Pub(uint8_t* data, string dest, uint16_t len)
{
if (test1_part != nullptr) {
test1_part->publish(topic_name_cmd, "test1_Service", dest, "command", data, len);
}
}
// AD硬件服务化遥测发布
void test1_telemetry_Pub(uint8_t* data, string dest, uint16_t len)
{
if (test1_part != nullptr) {
test1_part->publish(topic_name_tlm, "test1_Service", dest, "telemetry", data, len);
}
}