This commit is contained in:
2026-06-29 11:06:05 +08:00
parent b2a41d781b
commit 5e1d88f2e2
2 changed files with 51 additions and 32 deletions
+49 -27
View File
@@ -59,35 +59,57 @@ std::string packet_to_escape_format(std::string_view t) {
} }
return result; return result;
} }
void Binary_Format_handle_buffer( // void Binary_Format_handle_buffer(std::string& buffer, std::string_view data, const std::function<void(std::string&)>& callback) {
std::string& buffer, std::string_view data, // size_t i = 0;
const std::function<void(std::string&)>& callback) { // while (i < data.size()) {
size_t i = 0; // if (data[i] == 0x1A) {
while (i < data.size()) { // // 如果遇到起始标记 0x1A
if (data[i] == 0x1A) { // if (i + 1 < data.size() && data[i + 1] == 0x1A) {
// 如果遇到起始标记 0x1A // // 如果是转义的 0x1A 0x1A,继续处理
if (i + 1 < data.size() && data[i + 1] == 0x1A) { // buffer.push_back(0x1A); // 添加一个正常的 0x1A 字节
// 如果是转义的 0x1A 0x1A,继续处理 // i += 2; // 跳过这两个字节
buffer.push_back(0x1A); // 添加一个正常的 0x1A 字节 // }
i += 2; // 跳过这两个字节 // else {
} // // 检测到起始标记 0x1A,认为是新消息的开始
else { // if (!buffer.empty()) {
// 检测到起始标记 0x1A,认为是新消息的开始 // // 如果当前消息 buffer 中已有数据,调用回调函数处理当前消息
if (!buffer.empty()) { // callback(buffer);
// 如果当前消息 buffer 中已有数据,调用回调函数处理当前消息 // buffer.clear(); // 清空当前消息的缓存
callback(buffer); // }
buffer.clear(); // 清空当前消息的缓存 // buffer.push_back(0x1A);
} // // 跳过当前的 SOP 标记 (0x1A)
buffer.push_back(0x1A); // i++;
// 跳过当前的 SOP 标记 (0x1A) // }
i++; // }
} // else {
// // 如果不是 0x1A,直接将数据添加到 buffer 中
// buffer.push_back(data[i]);
// i++;
// }
// }
// }
void Binary_Format_handle_buffer(std::string& buffer, std::string_view data_view, const std::function<void(std::string&)>& callback) {
constexpr char sop = 0x1A;
buffer.reserve(buffer.size() + data_view.size());
std::size_t i = 0;
while (i < data_view.size()) {
auto pos = data_view.find(sop, i);
if (pos == std::string_view::npos) {
buffer.append(data_view.data() + i, data_view.size() - i);
break;
} }
else { buffer.append(data_view.data() + i, pos - i);
// 如果不是 0x1A,直接将数据添加到 buffer 中 if (pos + 1 < data_view.size() && data_view[pos + 1] == sop) {
buffer.push_back(data[i]); buffer.push_back(sop);
i++; i = pos + 2;
continue;
} }
if (!buffer.empty()) {
callback(buffer);
buffer.clear();
}
buffer.push_back(sop);
i = pos + 1;
} }
} }
// 9 + 2 // 9 + 2
+2 -5
View File
@@ -360,9 +360,7 @@ inline std::uint8_t get_len(std::string_view msg) {
// Before Escaping: 1A 32 27 1A E8 57 F0 1A 6C // Before Escaping: 1A 32 27 1A E8 57 F0 1A 6C
// During Transmission: 1A 32 27 1A 1A E8 57 F0 1A 1A 6C // During Transmission: 1A 32 27 1A 1A E8 57 F0 1A 1A 6C
// After Un-Escaping: 1A 32 27 1A E8 57 F0 1A 6C // After Un-Escaping: 1A 32 27 1A E8 57 F0 1A 6C
void Binary_Format_handle_buffer( void Binary_Format_handle_buffer(std::string& buffer, std::string_view data, const std::function<void(std::string&)>& callback);
std::string& buffer, std::string_view data,
const std::function<void(std::string&)>& callback);
/* Command Message (Host ==> Device) /* Command Message (Host ==> Device)
The command-message has a simple ASCII based structure. Each message starts with The command-message has a simple ASCII based structure. Each message starts with
a # (0x23) and ends with <CR><LF> (0x0D, 0x0A). In-between are one to 16 a # (0x23) and ends with <CR><LF> (0x0D, 0x0A). In-between are one to 16
@@ -373,8 +371,7 @@ first byte is mandatory and holds the command while the remaining bytes are
parameters. Available commands depend on the selected protocol and are discussed parameters. Available commands depend on the selected protocol and are discussed
in the respective protocol section. in the respective protocol section.
*/ */
std::string mode_s_msg_packet_to_server_packet(std::uint32_t id, std::string mode_s_msg_packet_to_server_packet(std::uint32_t id, std::string_view packet);
std::string_view packet);
// 1a34 radarcape 状态帧消息 // 1a34 radarcape 状态帧消息
// https://wiki.jetvision.de/wiki/Radarcape:Version_History#Status_Frame_0x34_contents_were_extended_with_GPS_and_UTC_information // https://wiki.jetvision.de/wiki/Radarcape:Version_History#Status_Frame_0x34_contents_were_extended_with_GPS_and_UTC_information
// 状态帧0x34内容已扩展为 GPS 和 UTC 信息 // 状态帧0x34内容已扩展为 GPS 和 UTC 信息