1LB0truetrueLOCAL:[本地寄存器]:LB05942dc1f-588b-4485-88a4-8a07f240b2f8a868feeb-a779-4a71-b020-f5a889a07f33254truetruetruetruetrue4ConstantLB0LW0trueWordLOCAL:[本地寄存器]:LW05942dc1f-588b-4485-88a4-8a07f240b2f8578aa61d-ea7b-4a18-8ad0-c769ce4803d965791truetruetruetrue1true4Constant2WordLB0truetrueLOCAL:[本地寄存器]:LB05942dc1f-588b-4485-88a4-8a07f240b2f89673b9b2-fbce-40e2-9af7-9de7e041384c254truetruetruetruetrue4ConstantLW010LW0trueWordLOCAL:[本地寄存器]:LW05942dc1f-588b-4485-88a4-8a07f240b2f8c80b002a-cc47-4c8c-a3c0-5a910945ecca65791truetruetruetrue1true4Constant3WordLB0truetrueLOCAL:[本地寄存器]:LB05942dc1f-588b-4485-88a4-8a07f240b2f8472f1305-2b08-4198-8338-43e84a63d249254truetruetruetruetrue4ConstantRW05RW0trueWordLOCAL:[本地寄存器]:RW05942dc1f-588b-4485-88a4-8a07f240b2f8ab40f3a9-d838-419f-a8cb-b10b9297708c65789truetruetruetrue1true4Constant4WordSRB0truetrueLOCAL:[本地寄存器]:SRB05942dc1f-588b-4485-88a4-8a07f240b2f887454f50-b9a4-4693-ad8f-c4e78ad7f98c257truetruetruetruetrue4ConstantSRW0SRW0trueWordLOCAL:[本地寄存器]:SRW05942dc1f-588b-4485-88a4-8a07f240b2f8f3f05a1f-273a-4992-b0fa-ba3220120c8565792truetruetruetrue1true4Constant获取房间信息Test2Test2.ctrue#include <macro.h> #include <string.h> #include <stdio.h> // 定义常量 #define MAX_ROOMS 24 #define BUFFER_SIZE 4096 #define NAME_BUFFER_LEN 100 #define VALUE_BUFFER_LEN 20 // 字段最大长度 #define FIELD_ATM_LEN 7 #define FIELD_CH2O_LEN 4 #define FIELD_LUX_LEN 10 #define FIELD_TVOC_LEN 3 #define FIELD_CO2_LEN 3 #define FIELD_PM_LEN 3 void ParseJsonField( const unsigned char* json, // JSON 响应字符串 const char* key, // 要查找的键名,如 "atm" unsigned char* value, // 输出缓冲区 int max_len // 输出缓冲区最大长度 ) { // 构造查找字符串: "\"key\":" unsigned char pattern[32] = {0}; snprintf(pattern, sizeof(pattern), "\"%s\":", key); unsigned char *pos = strstr(json, pattern); if (pos == NULL) { strcpy(value, "0"); return; } // 跳过 "\"key\":" pos += strlen(pattern); // 跳过空白字符(空格、制表符等) while (*pos == ' ' || *pos == '\t' || *pos == '\n' || *pos == '\r') { pos++; } int k = 0; // 判断是否为字符串值(以引号开头) if (*pos == '"') { pos++; // 跳过开头的引号 while (pos[k] != '"' && k < max_len - 1) { value[k] = pos[k]; k++; } value[k] = '\0'; } // 否则为数字或布尔值等(直接取有效字符) else { while (k < max_len - 1) { char c = pos[k]; if (c == ',' || c == '}' || c == ' ' || c == '\t' || c == '\n' || c == '\r') { break; // 遇到分隔符结束 } value[k] = c; k++; } value[k] = '\0'; } // 如果没提取到任何内容,设为 "0" if (k == 0) { strcpy(value, "0"); } } int MacroMain() { // 数据缓冲区 unsigned char httpRequest[BUFFER_SIZE] = {0}; unsigned char recvBuffer[BUFFER_SIZE] = {0}; // 存储解析结果 unsigned char atm[FIELD_ATM_LEN] = {0}; unsigned char ch2o[FIELD_CH2O_LEN] = {0}; unsigned char lux[FIELD_LUX_LEN] = {0}; unsigned char tvoc[FIELD_TVOC_LEN] = {0}; unsigned char co2[FIELD_CO2_LEN] = {0}; unsigned char pm[FIELD_PM_LEN] = {0}; // 房间ID(字符串) char tId[20] = {0}; // 网络信息 unsigned char host[50] = {0}; unsigned short port = 0; int socketHandle = 0; int sentBytes = 0, recvBytes = 0; // 初始化服务器地址和端口 if (GetDWord(@RW0@, 100) == 0) { const unsigned char YS1[] = "124.221.147.225"; SetMem(YS1, @RW0@, 100, strlen(YS1)); SetWord(@RW0@, 120, 58888); } // 获取服务器信息 GetMem(host, @RW0@, 100, 40); port = GetWord(@RW0@, 120); // 获取房间ID(字符串) GetMem(tId, @LW0@, 77777, 19); // 最多复制19字节 // 构建 HTTP 请求(使用动态 host 和 port) snprintf(httpRequest, sizeof(httpRequest), "GET /coldchain/hmi/getRoomInfoById?tId=%s HTTP/1.1\r\n" "Host: %s:%d\r\n" "Token: SZrN75oijJZLCgXg3K98zW4fbXT2soio\r\n" "Connection: Keep-Alive\r\n" "Accept: application/json, text/plain, */*\r\n" "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0\r\n" "Accept-Encoding: gzip, deflate, br, zstd\r\n" "\r\n", tId, host, port); // 建立连接 socketHandle = OpenSocket(host, port, 1); SetWord(@LW0@, 0, socketHandle); if (socketHandle >= 1000) { Debug(0, "Socket opened: %d", socketHandle); sentBytes = SendSocket(socketHandle, httpRequest, strlen(httpRequest)); SetWord(@LW0@, 1, sentBytes); Debug(0, "Sent %d bytes", sentBytes); Delay(500); recvBytes = RecvSocket(socketHandle, recvBuffer, sizeof(recvBuffer) - 1, 2000); SetWord(@LW0@, 20, recvBytes); recvBuffer[recvBytes] = '\0'; // 确保字符串结束 Debug(0, "HTTP Response: %s", recvBuffer); // ================== 使用函数解析各个字段 ================== ParseJsonField(recvBuffer, "atm", atm, FIELD_ATM_LEN); ParseJsonField(recvBuffer, "ch2o", ch2o, FIELD_CH2O_LEN); ParseJsonField(recvBuffer, "lux", lux, FIELD_LUX_LEN); ParseJsonField(recvBuffer, "tvoc", tvoc, FIELD_TVOC_LEN); ParseJsonField(recvBuffer, "co2", co2, FIELD_CO2_LEN); ParseJsonField(recvBuffer, "pm", pm, FIELD_PM_LEN); // ================== 调试输出所有结果 ================== Debug(0, "Parsed Data:"); Debug(0, " atm : %s", atm); Debug(0, " ch2o : %s", ch2o); Debug(0, " lux : %s", lux); Debug(0, " tvoc : %s", tvoc); Debug(0, " co2 : %s", co2); Debug(0, " pm : %s", pm); // ================== 可选:写入寄存器供 HMI 使用 ================== // 示例:将字符串写入内存区(根据您的系统调整) SetMem(atm, @LW0@, 101111, FIELD_ATM_LEN-1); SetMem(ch2o, @LW0@, 102222, FIELD_CH2O_LEN-1); SetMem(lux, @LW0@, 103333, FIELD_LUX_LEN-1); SetMem(tvoc, @LW0@, 104444, FIELD_TVOC_LEN-1); SetMem(co2, @LW0@, 105555, FIELD_CO2_LEN-1); SetMem(pm, @LW0@, 106666, FIELD_PM_LEN-1); // 关闭连接 CloseSocket(socketHandle); } else { Debug(0, "Failed to open socket to %s:%d", host, port); } return 0; }78211b4f-31bd-4a4b-bc32-d651db7dc4181