private void parseMsgCmdFun(msgCmd msg) { string str = msg.head.ToString("X2") + " " + msg.len.ToString("X2") + " " + msg.sn[0].ToString("X2") + " " + msg.sn[1].ToString("X2") + " "; for (int i = 0; i < msg.len; i++) { str += msg.data[i].ToString("X2") + " "; } str += msg.crc[0].ToString("X2") + " "; str += msg.crc[1].ToString("X2") + " "; str += msg.crc[2].ToString("X2") + " "; str += msg.crc[3].ToString("X2") + " "; str += "18"; addLog("<<<< " + str + Environment.NewLine); if (msg.sn[1] % 2 == 1) { byte[] data = new byte[] { 0xe0, 0, 0, 0, 0, 0, 0, 0, 0x18 }; data[2] = msg.sn[0]; data[3] = msg.sn[1]; crc32.setCrc32Value(data); linkMgr.sendData(data,data.Length); str = ""; for (int i = 0; i < data.Length;i++ ) { str += data[i].ToString("X2") + " "; } addLog(">>>> " + str + Environment.NewLine); } }
public void parseMsgCmdInvoke(msgCmd msg) { this.Dispatcher.BeginInvoke(new Action(() => { parseMsgCmdFun(msg); })); }
public static void setCrc32Value(msgCmd msg) { UInt32 crc32Value = stm32crc32(msg.data, msg.data.Length); msg.crc[0] = (byte)((crc32Value >> 24) & 0xff); msg.crc[1] = (byte)((crc32Value >> 16) & 0xff); msg.crc[2] = (byte)((crc32Value >> 8) & 0xff); msg.crc[3] = (byte)(crc32Value & 0xff); }
public static void parseBufTolst() { int buf_write_tmp = buf_write; //包头检测 若接收到的数据长度小于最小心跳包长度 则不再检测包头 while ((buf_write_tmp + BUFLEN - buf_read) % BUFLEN > 4) { // Console.WriteLine("[parseBufTolst]" + (buf_write_tmp + BUFLEN - buf_read) % BUFLEN); //检测到包头 if (lstBuf[buf_read] == NET_CMD_HEAD) { //取得接收数据长度 byte[] crcData = new byte[200]; buf_read_forward(1); int len_tmp = lstBuf[buf_read]; crcData[0] = lstBuf[buf_read]; if(len_tmp > 100) { continue; } if ((buf_write_tmp + BUFLEN - buf_read) % BUFLEN > len_tmp + 7) { //接收到完整指令 buf_read+3 代表len 位置 head1+sn2+len1+crc4+end1 =9字节 msgCmd pcmdS = new msgCmd(); pcmdS.head = NET_CMD_HEAD; pcmdS.len = lstBuf[buf_read ]; buf_read_forward(1); pcmdS.sn[0] = lstBuf[buf_read ]; crcData[1] = lstBuf[buf_read]; buf_read_forward(1); pcmdS.sn[1] = lstBuf[buf_read ]; crcData[2] = lstBuf[buf_read]; buf_read_forward(1); //指向data // SendCMD((uint8_t)pcmdS); // SendCMD((uint8_t)dat); for (int i = 0; i < (len_tmp); i++) { pcmdS.data[i] = lstBuf[buf_read]; crcData[3 + i] = lstBuf[buf_read]; buf_read_forward(1); } pcmdS.crc[0] = lstBuf[buf_read ]; buf_read_forward(1); pcmdS.crc[1] = lstBuf[buf_read ]; buf_read_forward(1); pcmdS.crc[2] = lstBuf[buf_read ]; buf_read_forward(1); pcmdS.crc[3] = lstBuf[buf_read ]; buf_read_forward(1); // pcmdS.endl = NET_CMD_END; buf_read_forward(1); //Crc323.Reset(); //Crc323.Crc(pcmdS.sn[0]); //Crc323.Crc(pcmdS.sn[1]); //Crc323.Crc(pcmdS.data, 0, pcmdS.len); //UInt32 crc32 = Crc323.Value; ////CRC校验 //psndat = &pcmdS.sn[0]; //CRC_ResetDR(); //for (i = 0; i < len_tmp + 3; i++) //{ // CRC_CalcCRC((uint32_t)*psndat++); //} UInt32 crc_calc = crc32.stm32crc32(crcData, pcmdS.len + 3); UInt32 crc = (UInt32)((pcmdS.crc[0] <<24) | (pcmdS.crc[1] <<16) | (pcmdS.crc[2] << 8) | pcmdS.crc[3]); if (crc_calc == crc) { if(parseMsgCmdHandle != null) parseMsgCmdHandle(pcmdS); if(pcmdS.len > 0) { parseMsg_Data(pcmdS.data); } } //else // Console.WriteLine("[parseBufTolst] crc check error! local crc : {0:x}\tnet crc : {1:x} ", crc32, crc); } else { buf_read_backward(1); } } else { buf_read_forward(1); } buf_write_tmp = buf_write; }//end while }