Ejemplo n.º 1
0
        /// <summary> CRC效验
        /// CRC效验:校验的范围为不包含帧头、帧尾和校验值的全部数据内容
        /// </summary>
        /// <param name="byt">不包含帧头、帧尾的全部数据内容</param>
        /// <returns>效验是否正确</returns>
        private bool CheckSum(byte[] byt)
        {
            bool bol = false;

            byte[] checkdatagram = new byte[byt.Length-2];//效验内容
            Array.Copy(byt, 0, checkdatagram, 0, byt.Length - 2);
            
            byte[] sourceCode=new byte[2];//效验值
            Array.Copy(byt, byt.Length - 2, sourceCode, 0, 2);
            
            CRCTool crctool = new CRCTool();
            if (crctool.CalcCRCITT(checkdatagram) == (((sourceCode[0] &0xff)<<8) + sourceCode[1]&0xff))
                bol = true;

            return bol;
        }
Ejemplo n.º 2
0
        /// <summary> 组织下发包
        /// 组织下发包
        /// </summary>
        /// <param name="cha">U为上行,D为下行</param>
        /// <param name="strflage">协议号</param>
        /// <param name="Sequence">指令循环码</param>
        /// <param name="gpscode">终端ID</param>
        /// <param name="len">协议长度</param>
        /// <param name="bytContent">协议内容</param>
        /// <returns></returns>
        private byte[] SetDatagram(char cha,string strflage,byte Sequence, byte[] gpscode, int len, params byte[] bytContent)
        {
            /*
             1	帧头	7EH
            1	标志码	47H
            1	指令循环码	
            2	厂商代码	终端生产厂家代码
            4	终端ID	特指MDT识别号
            4	中心ID	营运管理中心识别号
            4	密码	

             */
            byte byte1 = 0x7e;//帧头
            byte byte2 = 0x47;//标志码
            byte byte3 = Sequence;//指令循环码
            byte[] byte4 = new byte[2] { 0x00, 0x00 };//厂商代码
            byte[] byte5 = gpscode;//终端ID
            byte[] byte6 = new byte[4] { 0x00, 0x00, 0x00, 0x00 };//中心ID
            byte[] byte7 = new byte[4] { 0x00, 0x00, 0x00, 0x00 };//密码
            //协议长度
            byte[] byte8 = new byte[2] { (byte)((len >> 8) & 0xff), (byte)((len >> 0) & 0xff) };
            //协议号
            byte[] byte9 = new byte[2] { Convert.ToByte(cha), byte.Parse(strflage, System.Globalization.NumberStyles.HexNumber) };
            //协议内容(需要加密、编码、时效性)加密算法不起作用,故不作加密
            byte[] byte10 = bytContent;
            //校验
            byte[] byte11 = new byte[2];
            //帧尾
            byte byte12 = 0x7f;

            List<byte> byts = new List<byte>();

            byte[] bythead=null;
            if (bytContent.Length == 0)//无内容
                bythead = new byte[] { byte1, byte2, byte3, byte4[0], byte4[1], 
                                        byte5[0], byte5[1], byte5[2], byte5[3],
                                        byte6[0],byte6[1],byte6[2],byte6[3],
                                        byte7[0],byte7[1],byte7[2],byte7[3],
                                        byte8[0],byte8[1],
                                        byte9[0],byte9[1],
                                        byte11[0],byte11[1],byte12 };
            else//有内容
            {                
                bythead = new byte[] { byte1, byte2, byte3, byte4[0], byte4[1], 
                                        byte5[0], byte5[1], byte5[2], byte5[3],
                                        byte6[0],byte6[1],byte6[2],byte6[3],
                                        byte7[0],byte7[1],byte7[2],byte7[3],
                                        byte8[0],byte8[1],
                                        byte9[0],byte9[1],
                                        byte11[0],byte11[1],byte12 };
                List<byte> lis1 = new List<byte>();
                for (int i = 0; i < 21;i++ )
                    lis1.Add(bythead[i]);
                for (int i = 0; i < bytContent.Length; i++)
                    lis1.Add(bytContent[i]);
                for (int i = 21; i < bythead.Length; i++)
                    lis1.Add(bythead[i]);
                bythead = lis1.ToArray();
            }
            //效验
            byte[] byt2 = new byte[bythead.Length - 2];
            Array.Copy(bythead, 1, byt2, 0, bythead.Length - 4);
            CRCTool crctool = new CRCTool();
            int icrc=(int)crctool.CalcCRCITT(byt2);
            byt2[byt2.Length - 2] = (byte)((icrc >> 8) & 0xff);
            byt2[byt2.Length - 1] = (byte)((icrc >> 0) & 0xff);
            //加密(不起作用,忽略)
            //转义
            byt2 = CommitDecode(byt2);
            //base64加密
            byt2 = System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(byt2));

            //重新组装
            byte[] bytsend = new byte[byt2.Length + 2];
            bytsend[0] = byte1;
            for (int i = 0; i < byt2.Length; i++)
                bytsend[1 + i] = byt2[i];

            bytsend[bytsend.Length - 1] = byte12;
            return bytsend;
        }