Пример #1
0
        /// <summary>
        /// byte[] 형식의 frame을 명령 클래스 형식으로 반환
        /// </summary>
        /// <param name="frame">byte[] 형식의 frame</param>
        /// <returns>명령 클래스</returns>
        /// <example>
        /// <see cref="IEASProtocolBase">IEASProtocolBase</see><br/>
        /// <see cref="IEASPrtCmd1">IEASPrtCmd1</see><br/>
        /// <see cref="IEASPrtCmd2">IEASPrtCmd2</see><br/>
        /// <see cref="IEASPrtCmd3">IEASPrtCmd3</see><br/>
        /// <see cref="IEASPrtCmd4">IEASPrtCmd4</see><br/>
        /// <see cref="IEASPrtCmdFF">IEASPrtCmdFF</see>
        /// <code>
        /// using IEASProtocol;
        ///
        /// public void ParseFrame(byte[] frame)
        /// {
        ///     //한 패킷의 frame 을 인자로 ParseFrame 함수 호출
        ///     IEASProtocolBase protocolBase = IEASProtocolManager.ParseFrame(frame);
        ///
        ///     //명령어 코드 확인 후 해당 클래스로 캐스팅하여 사용
        ///     switch (prtBase.CMD)
        ///     {
        ///         case 0x01:
        ///             IEASPrtCmd1 prt1 = protocolBase as IEASPrtCmd1;
        ///             break;
        ///         case 0x02:
        ///             IEASPrtCmd2 prt2 = protocolBase as IEASPrtCmd2;
        ///             break;
        ///         case 0x03:
        ///             IEASPrtCmd3 prt3 = protocolBase as IEASPrtCmd3;
        ///             break;
        ///         case 0x04:
        ///             IEASPrtCmd4 prt4 = protocolBase as IEASPrtCmd4;
        ///             break;
        ///         case 0xFF:
        ///             IEASPrtCmdFF prtFF = protocolBase as IEASPrtCmdFF;
        ///             break;
        ///         default:
        ///             break;
        ///    }
        /// </code>
        /// </example>
        /// <seealso cref="IEASProtocolBase">IEASProtocolBase</seealso>
        /// <seealso cref="IEASPrtCmd1">IEASPrtCmd1</seealso>
        /// <seealso cref="IEASPrtCmd2">IEASPrtCmd2</seealso>
        /// <seealso cref="IEASPrtCmd3">IEASPrtCmd3</seealso>
        /// <seealso cref="IEASPrtCmd4">IEASPrtCmd4</seealso>
        /// <seealso cref="IEASPrtCmdFF">IEASPrtCmdFF</seealso>
        public static IEASProtocolBase ParseFrame(byte[] frame)
        {
            if (frame == null || frame.Length < 10)
            {
                throw new Exception("IEASProtocol.dll Exception - IEASProtocolManager - ParseFrame Fail. 데이터가 없거나 데이터의 길이가 충분하지 않습니다.");
            }
            if (!(frame[0] == 'K' && frame[1] == 'C' && frame[2] == 'A' && frame[3] == 'P'))
            {
                throw new Exception("IEASProtocol.dll Exception - IEASProtocolManager - ParseFrame Fail. 데이터가 올바르지 않습니다.");
            }
            try
            {
                IEASProtocolBase prtBase = null;
                #region 프로토콜의 인스턴스 생성
                byte cmd = frame[6];
                switch (cmd)
                {
                case 0x01:
                case 0x02:
                case 0x03:
                case 0x04:
                case 0xFF:
                    prtBase = CreateProtocol(cmd);
                    break;

                default:
                    throw new Exception("IEASProtocol.dll Exception - IEASProtocolManager - ParseFrame Fail. 지원하지 않는 커맨드 입니다.");
                }
                if (prtBase == null)
                {
                    return(null);
                }
                #endregion
                //데이터 길이
                byte[] byDataLen = new byte[2];
                Array.Clear(byDataLen, 0, 2);
                byDataLen[0] = frame[4];
                byDataLen[1] = frame[5];
                int dataLen = BitConverter.ToUInt16(byDataLen, 0);
                //Sender Type
                prtBase.SenderType = IEASProtocolUtil.ConvertByteToSenderType(frame[7]);
                //Data
                byte[] dataTemp = new byte[dataLen];
                Array.Clear(dataTemp, 0, dataTemp.Length);
                Buffer.BlockCopy(frame, 8, dataTemp, 0, dataLen);
                prtBase.Data = dataTemp;
                prtBase.ParseData();
                //Reserved
                byte[] reserved = new byte[2];
                Array.Clear(reserved, 0, reserved.Length);
                Buffer.BlockCopy(frame, 8 + dataLen, reserved, 0, reserved.Length);
                return(prtBase);
            }
            catch (Exception ex)
            {
                throw new Exception("IEASProtocol.dll Exception - IEASProtocolManager - ParseFrame Fail. " + ex.Message);
            }
        }