using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZNJM2.Win
{
    public class CommandBase
    {
        public byte Addr { get; set; }

        public List<byte> CmdBytes { get; protected set; }
        public string Name { get; set; }
        public Object Result { get; set; }
        public DateTime? BTime { get; set; }
        public DateTime? ETime { get; set; }
        public int delayMillis { get; set; }
        protected Func<byte[], int, byte[]> CRC { get; set; }
        /// <summary>
        /// 执行次数
        /// </summary>
        public long? RunCount { get; set; }
        /// <summary>
        /// 错误次数
        /// 每次成功执行后设置成0
        /// </summary>
        public long? ErrCount { get; set; }
        public Exception Err { get; set; }

        public List<byte> ReturnBytes { get; set; }


        /// <summary>
        ///  如 01 02,
        ///     01-02,
        ///     0102,
        ///   等形式
        /// </summary>
        public string CmdText { get; set; }
        public string CmdHexString
        {
            get
            {
                return BitConverter.ToString(CmdBytes.ToArray());
            }
        }
        public CommandBase()
        {
            CmdBytes=new List<byte>();
            ReturnBytes = new List<byte>();
            Addr = 0x01;
            ErrCount = 0;
            RunCount = 0;
            CRC = CRCUtil.CRC16;

        }

        #region 方法

        /// <summary>
        /// 生成命令的字节数组
        ///  如 01 02
        ///     01-02
        ///     0102
        ///   等形式
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public virtual List<byte> ParserCmd()
        {
            var cmdBytes = ParserCmdWithoutCRC();
            var crc2 = CRC(cmdBytes, cmdBytes.Length);


            CmdBytes = new List<byte>(cmdBytes.Concat(crc2));
            return CmdBytes;

        }

        protected virtual byte[] ParserCmdWithoutCRC()
        {
            string cmdText = CmdText;
            var cmd = cmdText.Replace(" ", "").Replace("-", "").Trim();
            if (string.IsNullOrWhiteSpace(cmd))
            {
                throw new Exception("空字符串!");
            }
            if ((cmd.Length % 2) != 0)
            {
                throw new Exception("命令字符窜长度必须是2的倍数!");
            }
            cmd = BitConverter.ToString(new byte[] { Addr }) + cmd;
            var cmdBytes = new byte[cmd.Length / 2];
            for (int i = 0; i < cmd.Length; i = i + 2)
            {
                cmdBytes[i / 2] = byte.Parse(cmd.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
            }
            return cmdBytes;
        }

        protected virtual bool VerifyResult()
        {

            if (ReturnBytes.Count <= 0)
            {
                Err=new Exception("零长度返回!");
                ErrCount++;
                return false;

            }
            if (ReturnBytes.Count <= 2)
            {
                Err = new Exception("返回字节少于2!");
                ErrCount++;
                return false;
            }

            //CRC Check\
            var len = ReturnBytes.Count;
            var crc = CRC(ReturnBytes.ToArray(), len-2);
            if (crc[0] != ReturnBytes[len - 2] || crc[1] != ReturnBytes[len - 1])
            {
                Err = new Exception("CRC校验错误!");
                ErrCount++;
                return false;
            }

            if (ReturnBytes[0] != Addr)
            {
                Err = new Exception("非期望的设备地址!");
                ErrCount++;
                return false;
            }
            if (ReturnBytes[1] != CmdBytes[1])
            {
                Err = new Exception("非期望的命令!");
                ErrCount++;
                return false;
            }




            return true;
        }

        public virtual bool ParserReturn()
        {
            return VerifyResult();
           
        }
        #endregion
    }
    public class RFIDCmdBase : CommandBase
    {
        public RFIDCmdBase()
        {

            CRC = CRCUtil.CRC16ForRFID;

        }


        /// <summary>
        /// 生成命令的字节数组
        ///  如 01 02
        ///     01-02
        ///     0102
        ///   等形式
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public override List<byte> ParserCmd()
        {
            var cmdBytes = ParserCmdWithoutCRC();
            var crc2 = CRC(cmdBytes, cmdBytes.Length);


            CmdBytes = new List<byte>(cmdBytes.Concat(crc2));
            return CmdBytes;

        }

        protected override byte[] ParserCmdWithoutCRC()
        {
            string cmdText = CmdText;
            var cmd = cmdText.Replace(" ", "").Replace("-", "").Trim();
            if (string.IsNullOrWhiteSpace(cmd))
            {
                throw new Exception("空字符串!");
            }
            if ((cmd.Length % 2) != 0)
            {
                throw new Exception("命令字符窜长度必须是2的倍数!");
            }
            cmd = BitConverter.ToString(new byte[] { Addr }) + cmd;
            var cmdBytes = new byte[cmd.Length / 2];
            for (int i = 0; i < cmd.Length; i = i + 2)
            {
                cmdBytes[i / 2] = byte.Parse(cmd.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
            }


            var lenV = (byte)(cmdBytes.Length + 2); //添加命令长度
            return new byte[] { lenV }.Concat(cmdBytes).ToArray();

          
        }

        protected override bool VerifyResult()
        {

            if (ReturnBytes.Count <= 0)
            {
                Err = new Exception("零长度返回!");
                ErrCount++;
                return false;

            }
            if (ReturnBytes.Count <= 5)
            {
                Err = new Exception("返回字节少于5!");
                ErrCount++;
                return false;
            }

            //CRC Check\
            var len = ReturnBytes.Count;
            var crc = CRC(ReturnBytes.ToArray(), len - 2);
            if (crc[0] != ReturnBytes[len - 2] || crc[1] != ReturnBytes[len - 1])
            {
                Err = new Exception("CRC校验错误!");
                ErrCount++;
                return false;
            }

            if (ReturnBytes[1] != Addr)
            {
                Err = new Exception("非期望的设备地址!");
                ErrCount++;
                return false;
            }
            if (ReturnBytes[2] != CmdBytes[2])
            {
                Err = new Exception("非期望的命令!");
                ErrCount++;
                return false;
            }




            return true;
        }

    }

}
View Code

相关文章:

  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-08-14
  • 2021-08-31
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
猜你喜欢
  • 2021-11-08
  • 2021-08-27
  • 2022-02-24
  • 2021-07-21
  • 2021-06-10
  • 2022-03-03
  • 2021-06-24
相关资源
相似解决方案