Ejemplo n.º 1
0
 //时间校准
 //7A 7A 01 00 02 08 0e 00 31 32 33 34 35 36 37 38 17 11 01 09 58 00 54 FA 7b 7b
 //7a 7a 01 00 02 08 07 00 01 17 11 01 10 09 02 a2 6b 7b 7b
 static public void ReceiveDispose_TimeCalibration(byte[] data, TcpSocketClient client)
 {
     try
     {
         Frame_TimeCalibration dataFrame = new Frame_TimeCalibration();
         //设备编号
         byte[] sn = new byte[8];   //设备编号
         for (int i = 0; i < 8; i++)
         {
             sn[i] = data[i];
         }
         dataFrame.DeviceNo = Encoding.ASCII.GetString(sn);  //获取设备编号ASCII
         //RTC
         string tStr = ConvertData.ToHexString(data, 8, 6);
         try
         {
             dataFrame.RTC = DateTime.ParseExact(tStr, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture);
         }
         catch (Exception)
         {
             dataFrame.RTC = null;
         }
         //拼接应答
         byte[] result = SendJoint_TimeCalibration(dataFrame);
         if (result != null)
         {
             client.SendBuffer(result);
         }
     }
     catch (Exception)
     { }
 }
Ejemplo n.º 2
0
        //时间校准
        static public byte[] SendJoint_TimeCalibration(Frame_TimeCalibration TimeCalibration)
        {
            try
            {
                List <byte> msgTemp = new List <byte>();
                //头
                msgTemp.Add(0x7A);
                msgTemp.Add(0x7A);
                //协议版本号
                msgTemp.Add(0x01);
                msgTemp.Add(0x00);
                msgTemp.Add(0x02);
                //命令字
                msgTemp.Add(0x08);
                //数据长度
                msgTemp.Add(0x07);
                msgTemp.Add(0x00);
                if (TimeCalibration.RTC == null)
                {
                    //时间标识
                    msgTemp.Add(0x01);
                }
                else
                {
                    double compare = Math.Abs((DateTime.Now - (DateTime)TimeCalibration.RTC).TotalMinutes);
                    if (compare > 1)//需要校验
                    {
                        //时间标识
                        msgTemp.Add(0x01);
                    }
                    else//不需要校验
                    {
                        //时间标识
                        msgTemp.Add(0x00);
                    }
                }
                //RTC
                DateTime dt = DateTime.Now;
                msgTemp.Add(byte.Parse(dt.Year.ToString().Substring(2, 2), System.Globalization.NumberStyles.HexNumber));
                msgTemp.Add(byte.Parse(dt.Month.ToString(), System.Globalization.NumberStyles.HexNumber));
                msgTemp.Add(byte.Parse(dt.Day.ToString(), System.Globalization.NumberStyles.HexNumber));
                msgTemp.Add(byte.Parse(dt.Hour.ToString(), System.Globalization.NumberStyles.HexNumber));
                msgTemp.Add(byte.Parse(dt.Minute.ToString(), System.Globalization.NumberStyles.HexNumber));
                msgTemp.Add(byte.Parse(dt.Second.ToString(), System.Globalization.NumberStyles.HexNumber));
                //校验和
                byte[] byteTemp = new byte[msgTemp.Count];
                msgTemp.CopyTo(byteTemp);
                byte[] crc16 = BitConverter.GetBytes(ConvertData.CRC16(byteTemp, 8, byteTemp.Length - 8));
                msgTemp.Add(crc16[0]);
                msgTemp.Add(crc16[1]);
                //结束符
                msgTemp.Add(0x7B);
                msgTemp.Add(0x7B);

                byteTemp = new byte[msgTemp.Count];
                msgTemp.CopyTo(byteTemp);
                return(byteTemp);
            }
            catch (Exception ex)
            {
                ToolAPI.XMLOperation.WriteLogXmlNoTail("GprsResolveDataV100.SendJoint_TimeCalibration:error", ex.Message + "|" + ex.StackTrace.ToString());
                return(null);
            }
        }