Ejemplo n.º 1
0
 /// <summary>
 /// 创建一个下行数据包
 /// </summary>
 /// <param name="protocolPackageId">协议包Id</param>
 /// <param name="body">消息体</param>
 /// <param name="serialNumber"></param>
 /// <returns></returns>
 public static byte[] CreateBytesPackage(string protocolPackageId, Int16 serialNumber,
     Action<BytesWriter> body = null)
 {
     List<byte> list = new List<byte>();
     using (BytesWriter bw = new BytesWriter())
     {
         if (body != null)
         {
             body(bw);
         }
         //消息头 长度 Id(协议号)
         list.Add((byte) (bw.Data.Length + 5));
         list.AddRange(ByteHelper.ConvertHexStringToBytes(protocolPackageId));
         //消息体
         list.AddRange(bw.Data);
         //序号
         list.AddRange(ByteHelper.ConvertInt16ToBytes(serialNumber));
         //效验码
         list.AddRange(GetCheckCode(list.ToArray()));
         //效验数据
         var data = list.ToArray();
         //重新组建数据包
         //清空集合
         list.Clear();
         //标识位开始
         list.AddRange(new byte[] {0x78, 0x78});
         //校验后数据
         list.AddRange(data);
         //标志位结束
         list.AddRange(new byte[] {0x0d, 0x0a});
         return list.ToArray();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建一个下行数据包
        /// </summary>
        /// <param name="protocolPackageId">协议包Id</param>
        /// <param name="body">消息体</param>
        /// <returns></returns>
        public static byte[] CreateBytesPackage(string protocolPackageId, string deviceGpsCodeNoPrefix,Int16 serialNumber, Action<BytesWriter> body = null, bool isMulti = false)
        {
            List<byte> list = new List<byte>();
            using (BytesWriter bw = new BytesWriter())
            {
                if (body != null)
                {
                    body(bw);
                }

                Int16 attr = 0;
                if (isMulti)
                {
                    attr = (Int16)(bw.Data.Length - 4);
                    attr = (Int16)(attr | (1 << 13));
                }
                else
                {
                    attr = (Int16)bw.Data.Length;
                }

                //消息头 Id 属性 终端编码 流水号
                list.AddRange(ByteHelper.ConvertHexStringToBytes(protocolPackageId));
                list.AddRange(ByteHelper.ConvertInt16ToBytes(attr));
                list.AddRange(ByteHelper.ConvertNumStringToBytesByBcd(deviceGpsCodeNoPrefix));
                list.AddRange(ByteHelper.ConvertInt16ToBytes(serialNumber));
                //消息体
                list.AddRange(bw.Data);
                //效验码
                list.Add(GetCheckCode(list.ToArray()));
                //转义数据
                var data = Escape(list.ToArray());

                //重新组建数据包
                //清空集合
                list.Clear();
                //标识位开始
                list.Add(0x7e);
                //转义后数据
                list.AddRange(data);
                //标志位结束
                list.Add(0x7e);
                return list.ToArray();
            }
        }