Exemple #1
0
        //根据返回的信息判断消息发送是否成功
        public static bool CheckCommuPack(byte[] RxPack, int Length)
        {
            byte[] buf = new byte[128];
            if (RxPack[0] != (byte)83 || RxPack[Length - 1] != (byte)64 || (RxPack[2] != (byte)0 || (int)RxPack[3] != Length))
            {
                return(false);
            }
            Buffer.BlockCopy((Array)RxPack, 1, (Array)buf, 0, Length - 1);
            int num = (int)Crc16.Crc16Calculate((ushort)0, buf, (ushort)Convert.ToByte(Length - 4));

            return((int)Convert.ToByte(num % 256) == (int)buf[Length - 4] && (int)Convert.ToByte(num / 256) == (int)buf[Length - 3]);
        }
Exemple #2
0
        //生成通讯包
        public static int CreateCommuPack(
            byte[] Dst,
            byte[] Src,
            byte ObjId,
            byte PackNo,
            byte Length)
        {
            byte[] buf = new byte[32];
            buf[0] = PackNo;
            buf[1] = ObjId;
            buf[2] = Convert.ToByte(Length + 7);
            for (int i = 0; i < Length; i++)
            {
                buf[3 + i] = Src[i];
            }
            int crc = Crc16.Crc16Calculate(0, buf, Convert.ToByte(Length + 3));

            buf.CopyTo(Dst, 1);
            Dst[0]              = Convert.ToByte('S');
            Dst[4 + Length]     = Convert.ToByte(crc % 256);
            Dst[4 + Length + 1] = Convert.ToByte(crc / 256);
            Dst[4 + Length + 2] = Convert.ToByte('@');
            return(4 + Length + 3);
        }