CDT报文分次上传的合并
对于将一段报文进行分段上传,需要将它合并,并去除其中同步字之前的报文,
报文分段上传对于多个设备同时连接可能有影响
char* memstr(char* full_data, int full_data_len, char* substr) { if (full_data == NULL || full_data_len <= 0 || substr == NULL) return NULL; if (*substr == '\0') return NULL; int sublen = strlen(substr); int i; char* cur = full_data; int last_possible = full_data_len - sublen + 1; for (i = 0; i < last_possible; i++) { if (*cur == *substr) { if (memcmp(cur, substr, sublen) == 0) { return cur; } } cur++; } return NULL; } int MSGADD(unsigned char* recv_data, signed int recv_size, unsigned char** history_data, int historylen) {//将报文叠加起来 if ((historylen) == 0) { historylen = recv_size; (*history_data) = chk_calloc(historylen, sizeof (ST_UCHAR)); memcpy((*history_data), recv_data, (historylen)); } else { ST_UCHAR *temprecv = chk_calloc(historylen + recv_size, sizeof (ST_UCHAR)); memcpy(temprecv, (*history_data), (historylen)); memcpy(temprecv + historylen, recv_data, recv_size); chk_free((*history_data)); (*history_data) = temprecv; historylen = historylen + recv_size; } return historylen; } ST_INT MSG_superposition(ST_UCHAR *recv_data, ST_INT recv_size){ static ST_UCHAR needle[7] = {0xeb, 0x90, 0xeb, 0x90, 0xeb, 0x90, '\0'}; static ST_UCHAR *history_data = NULL; static int historylen = 0; ST_UCHAR *indexstr; historylen = MSGADD(recv_data, recv_size, &history_data, historylen); chk_free(recv_data); recv_data=NULL; recv_size = 0;if ((indexstr = memstr(history_data, historylen, needle)) == NULL) return -2; else if (indexstr != history_data) {//去除不正确报文 int templen = indexstr - history_data; memcpy(history_data, history_data + templen, historylen - templen); historylen -= templen; } else { if ((indexstr = memstr(indexstr + 6, historylen - (indexstr - history_data + 6), needle)) == NULL) return -2; else { recv_size = indexstr - history_data; recv_data = chk_calloc(recv_size, sizeof (ST_UCHAR)); memcpy(recv_data, history_data, recv_size); memcpy(history_data, history_data + recv_size, historylen - recv_size); historylen -= recv_size; } }
return recv_size;
//printf("revvdata:\n");
//printdata(recv_data, recv_size);
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步