/// <summary> /// 解析协议信息,解析出来的数据都会赋值给每个变量。用户可以通过访问后续的变量来得到相关数据 /// </summary> /// <returns>成功返回true</returns> public bool ParseProtocol() { if (m_pData == null || m_pData.Count <= 3) return false; int buf_id = 0; byte val = Convert.ToByte(m_pData[buf_id++]); if (val != 0x7e) return false; val = Convert.ToByte(m_pData[m_pData.Count-1]); if (val != 0x7e) return false; //转移还原 ArrayList msg = new ArrayList(); msg = m_pData.GetRange(buf_id, m_pData.Count - 2 - buf_id); revser_transfer(msg); //验证checksum byte chksum = Checksum(msg); if(chksum != Convert.ToByte(m_pData[m_pData.Count -2])) return false; //parse header if (msg.Count < 12) return false; if (m_header == null) m_header = new HGMessageHeader(); ArrayList header = new ArrayList(); header = msg.GetRange(0, 12);//不管是否需要打包,以防万一都发12个数据过去 if (!m_header.parse(header)) return false; //parse body int body_start = m_header.isPackget ? 12 : 8; ArrayList body = new ArrayList(); body = msg.GetRange(body_start, Convert.ToInt32(m_header.MsgBodySize)); if(body.Count < 2) return false; byte body_type = Convert.ToByte(body[1]); switch (body_type) { case 1: m_body = new HGMessageBodyStartPosition(); break; case 2: m_body = new HGMessageBodyEndPosition(); break; case 3: m_body = new HGMessageBodyNavigationState(); break; case 4: m_body = new HGMessageBodyNavigationStateIdent(); break; case 11: m_body = new HGMessageBodyCarInfo(); break; case 12: m_body = new HGMessageBodyPosition(); break; case 13: m_body = new HGMessageBodyTracePlayback(); break; } if (!m_body.parse(body)) return false; return true; }
public static HGMessageHeader creat_header(UInt16 id, bool ispacket, UInt16 encrypt) { HGMessageHeader head = new HGMessageHeader(); head.ID = id; head.SequenceNumber = SequenceNumber; head.isPackget = ispacket; head.EncryptType = encrypt; return head; }