#include #include #include #include #include #include #include #include #include #include #include // #include // #include // #include #pragma comment(lib, "ws2_32.lib") #define PORT 8988 #define EACH_FRAME_SIZE 180 #define DATA_NUM (EACH_FRAME_SIZE / 3) typedef struct parse_packet_to_csv { uint8_t h0; uint8_t h1; uint8_t data[180]; uint8_t e0; uint8_t e1; }; uint32_t chdatacache[DATA_NUM]; void parse_chdatacache(parse_packet_to_csv *packet) { for (size_t i = 0; i < DATA_NUM; i++) { chdatacache[i] = (uint32_t)packet->data[i * 3] << 0 | (uint32_t)packet->data[i * 3 + 1] << 8 | (uint32_t)packet->data[i * 3 + 2] << 16; } } int main(int argc, char *argv[]) { /** * @brief * 打开raw.txt * 读取raw.txt全部内容 * 去掉换行,空格,回车 * 将16进制字符串转换成二进制 * 写入raw.bin */ std::ifstream ifs("Data.txt"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); ifs.close(); std::string str2; for (auto &c : str) { if (c == ' ' || c == '\n' || c == '\r') { continue; } str2.push_back(c); } std::ofstream ofs("Data.bin", std::ios::binary); for (size_t i = 0; i < str2.size(); i += 2) { std::string str3 = str2.substr(i, 2); char c = (char)std::stoi(str3, nullptr, 16); ofs.write(&c, 1); } ofs.close(); /** * @brief * 解析上报文件 */ { std::ifstream ifs("Data.bin", std::ios::binary); std::vector buffer((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); ifs.close(); printf("buffer.size() = %d\n", buffer.size()); std::ofstream ofs("Data.csv"); for (size_t i = 0; i < buffer.size();) { parse_packet_to_csv *packet = (parse_packet_to_csv *)&buffer[i]; parse_chdatacache(packet); if (packet->h0 == 0x4A && packet->h1 == 0xA4 && packet->e0 == 0x4B && packet->e1 == 0xB4) { for (size_t i = 0; i < DATA_NUM; i += 3) { ofs << chdatacache[i] << "," << chdatacache[i + 1] << "," << chdatacache[i + 2] << "\n"; } i += sizeof(parse_packet_to_csv); } else { i++; } } } printf("parse over\n"); while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); } }