public static byte[] GeneratePackage(byte[] content, byte mainCode) { int l = content == null ? 0 : content.Length; byte[] s = new byte[l + 7]; s[0] = s[1] = 0x29; s[2] = mainCode; s[3] = Convert.ToByte((l + 2) / 256); s[4] = Convert.ToByte((l + 2) % 256); s[l + 6] = 0x0D; int i = l; while (--i >= 0) { s[i + 5] = content[i]; //content } s[l + 5] = EncodeingHelper.BCCValidateCode(s, 0, l + 5); //校验 return(s); }
public override object FromIns(byte[] bytes) { if (bytes.Length < MinLength - 2) { return(null); } Ins_CommonLocationEntity s = new Ins_CommonLocationEntity(); s.UniqueID = UniqueID(EncodeingHelper.CutOff(bytes, 0, 4)); s.Date = DateTime.Now; try { s.Latitude = LatLongitude(EncodeingHelper.CutOff(bytes, 10, 4)); s.Longitude = LatLongitude(EncodeingHelper.CutOff(bytes, 14, 4)); } catch (Exception) { s.ExceptionsPackage = InstructsHelper.GeneratePackage(new byte[] { 0x10, InsCode }, 0xFF); } //s.InqueryPackage= InstructsHelper.GeneratePackage(null, 0x30); return(s); }
protected string UniqueID(byte[] bytes) { return(EncodeingHelper.CompressBCD(bytes).ToString("00000000")); }
public object[] Deserialize(Stream s, LinkParmeters linkParmeters, ref Dictionary <RelevantType, List <object> > Relevants) { List <object> list = new List <object>(); try { s.Seek(0, SeekOrigin.Begin); long l = s.Length; ushort scount = 0; while (s.Position < l) { if (s.ReadByte() == 0x29) { scount++; } else { scount = 0; } if (scount == 2) { byte ins_code = Convert.ToByte(s.ReadByte()); if (!_ips.ContainsKey(ins_code)) { list.Add(new Ins_BaseEntity { ExceptionsPackage = InstructsHelper.GeneratePackage(new byte[] { 0x0A, ins_code }, 0xFF) }); scount = 0; continue; } //if (ins_code == 0x81) //{ // LogHelper.log("0x81"); //} //if (ins_code == 0x80) //{ // LogHelper.log("0x80"); //} IInstructs c = _ips[ins_code]; byte[] bs = new byte[2]; s.Read(bs, 0, 2); int il = bs[0] * 256 + bs[1]; byte[] sTv = new byte[il + 3]; sTv[0] = 0x29; sTv[1] = 0x29; sTv[2] = ins_code; sTv[3] = bs[0]; sTv[4] = bs[1]; bs = new byte[il - 2]; s.Read(bs, 0, il - 2); Array.Copy(bs, 0, sTv, 5, il - 2); int[] last = new int[] { s.ReadByte(), s.ReadByte() }; #region 校验 if (last[1] != 0x0D) { //包尾 throw exception list.Add(new Ins_BaseEntity { ExceptionsPackage = InstructsHelper.GeneratePackage(new byte[] { 0x05, ins_code }, 0xFF) }); scount = 0; continue; } if (last[0] != EncodeingHelper.BCCValidateCode(sTv, 0, il + 3)) { //校验 list.Add(new Ins_BaseEntity { ExceptionsPackage = InstructsHelper.GeneratePackage(new byte[] { 0x01, ins_code }, 0xFF) }); scount = 0; continue; } #endregion object o = c.FromIns(bs); list.Add(o); if (!Relevants.ContainsKey(c.RelevantType)) { Relevants[c.RelevantType] = new List <object>(); } Relevants[c.RelevantType].Add(o); scount = 0; } } if (list.Count == 0 & l < 7) { list.Add(new Ins_BaseEntity { ExceptionsPackage = InstructsHelper.GeneratePackage(new byte[] { 0x15, 0x00 }, 0xFF) }); } } catch (Exception e) { LogHelper.Error(e.ToString()); } return(list.ToArray()); }