Exemple #1
0
        /// <summary>
        /// 根据指令创建指定数据项对象(代码未完成)
        /// </summary>
        /// <returns></returns>
        public DataItem getDataItem(byte ser)
        {
            byte[] cmdData = strToToHexByte(_cmd.DataCommand);
            //重新编写指令序列号
            cmdData[2] = ser;

            if (_cmd.Identification == "A013" && _cmd.ControlCode == 0x04)
            {
                cmdData[3] = ser;
            }

            DataItem item = null;

            switch ((ControlCode)_cmd.ControlCode)
            {
            case ControlCode.ReadData:
                //读数据指令
                item = new DataItem_ReadData_ASK(cmdData);
                break;

            case ControlCode.WriteData:
                //写数据
                item = getWriteDataAskItem(cmdData);
                break;

            case ControlCode.ReadKeyVersion:
                //读Key版本号
                break;

            case ControlCode.ReadMeterAdress:
                //读地址
                break;

            case ControlCode.WriteMeterAdress:
                //写地址
                break;

            case ControlCode.WriteMeterNum:
                //写表底
                break;

            case ControlCode.CYReadData:
                //读参数
                item = getCYReadDataAskItem(cmdData);
                break;

            case ControlCode.CYWriteData:
                //创源内部写指令(参数设置)
                item = getCYWriteDataAskItem(cmdData);
                break;

            case ControlCode.CTR_6:    //主动上报数据中
                item = new DataItem_C001(cmdData);
                break;

            default:
                break;
            }
            return(item);
        }
Exemple #2
0
        private TaskArge JieXi(byte[] buffer, out string message)
        {
            message = "";
            int index = 0;//查找报文头

            for (int i = 0; i < buffer.Length - 2; i++)
            {
                if (buffer[i] == 0XFE && buffer[i + 1] == 0X68)
                {
                    index = ++i;
                    break;
                }
            }
            //检查仪表类型是否为燃气表,不是燃气表,抛弃
            if (buffer[index + 1] != 0x30)
            {
                message = "表类型错误";
                return(null);
            }
            int  DataLength = buffer[index + 10];
            byte checkSum   = 0;

            //检查校验码
            for (int i = index; i < index + DataLength + 11; i++)
            {
                checkSum += Convert.ToByte(buffer[i]);
            }
            if (checkSum != buffer[index + 11 + DataLength])
            {
                //校验和错误
                message = "校验和错误";
                return(null);
            }
            //取出数据部分(该数据为加密数据)
            byte[] DATA = new byte[DataLength];
            for (int i = 0; i < DataLength; i++)
            {
                DATA[i] = buffer[i + index + 11];
            }

            StringBuilder sb = new StringBuilder();

            for (int i = index + 8; i > index + 1; i--)
            {
                sb.Append(string.Format("{0:X2}", buffer[i]));
            }
            if (this.Mac != sb.ToString())
            {
                message = string.Format("接收到数据不是给表:{0}的应答数据或请求指令。", this.Mac);
                return(null);
            }
            ControlCode ctrCode = (ControlCode)buffer[index + 9];

            // 解密数据
            byte[] desData = null;// Encryption.Decry(DATA, MKey);
            if (ctrCode == ControlCode.ReadKeyVersion)
            {
                //读取密钥为明文传输
                desData = DATA;
            }
            else
            {
                desData = Encryption.Decry(DATA, MKey);
            }


            //创建数据对象
            DataItem item = null;

            switch (ctrCode)
            {
            case ControlCode.ReadData:
                //主站请求的读数据操作
                item = new DataItem_ReadData_ASK(desData);
                break;

            case ControlCode.WriteData:
                //主站请求的写数据操作
                item = getWriteDataAskItem(desData);
                break;

            case ControlCode.ReadKeyVersion:
                break;

            case ControlCode.ReadMeterAdress:
                break;

            case ControlCode.WriteMeterAdress:
                break;

            case ControlCode.WriteMeterNum:
                break;

            case ControlCode.CYReadData:
                break;

            case ControlCode.CYWriteData:
                item = getCYWriteDataAskItem(desData);
                break;

            case ControlCode.CTR_6:
                //主动上报数据

                break;

            case ControlCode.CTR_7:
                break;

            case ControlCode.CTR_8:
                break;

            default:
                break;
            }
            return(new TaskArge(this.Mac, item, (ControlCode)buffer[index + 9], this.MKey));
        }