Esempio n. 1
0
        /// <summary>
        /// 把MsgType数据转化为byte[]类型数据
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            int size = 7;

            if (data != null)
            {
                size = 7 + data.Length;
            }
            byte[] sendBytes = new byte[size];
            sendBytes[0] = header;
            byte[] len_bytes = DevConvert.ConvertBytes(len);
            sendBytes[1] = len_bytes[0];
            sendBytes[2] = len_bytes[1];
            sendBytes[3] = addr;
            sendBytes[4] = cmd;
            if (data != null)
            {
                Array.Copy(data, 0, sendBytes, 5, data.Length);
            }
            //Console.WriteLine(DevConvert.ToHexString2Format(data, ""));
            //BCC校验
            byte[] bytes = new byte[size - 3];
            Array.Copy(sendBytes, 1, bytes, 0, size - 3);
            byte _bcc = BccParityUtil.BccCheckUtil(bytes);

            sendBytes[size - 2] = _bcc;
            sendBytes[size - 1] = 0xBB;

            return(sendBytes);
        }
Esempio n. 2
0
        public byte ToCheckSum()
        {
            byte[] bytes    = ToBytes();
            byte[] my_bytes = DevConvert.ReadBytes(bytes, 1, bytes.Length - 3);
            byte   _bcc     = BccParityUtil.BccCheckUtil(my_bytes);

            return(_bcc);
        }
Esempio n. 3
0
        /// <summary>
        /// 解析数据帧协议
        /// </summary>
        /// <param name="buffer">需要解析的数据帧</param>
        private void ResolveProtocol(byte[] buffer)
        {
            if (OnDataReceivedHandler != null)
            {
                OnDataReceivedHandler(tag, buffer);
            }
            //Console.WriteLine(DevConvert.ToHexString2Format(buffer, ""));
            //检验数据合法性
            byte[] my_bytes = DevConvert.ReadBytes(buffer, 1, buffer.Length - 3);
            byte   _bcc     = BccParityUtil.BccCheckUtil(buffer);

            byte[] data_bytes = DevConvert.ReadBytes(buffer, 5, buffer.Length - 7);
            byte   imsg_type  = buffer[4];

            switch (imsg_type)
            {
            case (byte)EnumFeedbackMid.SetControlAddr:
                //设置控制板地址
                Console.WriteLine("设置控制板地址");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.SetLockStatus:
                //设置电磁锁状态
                Console.WriteLine("设置电磁锁状态");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.GetLockStatus:
                //获取电磁锁状态
                Console.WriteLine("获取电磁锁状态");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.SetLedStatus:
                //设置LED灯状态
                Console.WriteLine("设置LED灯状态");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.GetControlEdition:
                //获取控制板设备版本
                Console.WriteLine("获取控制板设备版本");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.AddrError:
                //地址错误匹配反馈
                Console.WriteLine("设备地址错误匹配反馈");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            case (byte)EnumFeedbackMid.FrameCheckError:
                //数据帧校验匹配错误反馈
                Console.WriteLine("数据帧校验匹配错误反馈");
                if (OnEquipmentFeedbackHandler != null)
                {
                    OnEquipmentFeedbackHandler(imsg_type, data_bytes);
                }
                break;

            default:
                break;
            }
        }