Пример #1
0
        /// <summary>
        /// 获取 DL/T 645-2007 控制码的文字说明。
        /// </summary>
        /// <param name="controlCode"></param>
        /// <returns></returns>
        public static string GetDescription(this DLT645_2007ControlCodeOptions controlCode)
        {
            switch (controlCode)
            {
            case DLT645_2007ControlCodeOptions.ProofreadTime:
                return("广播校时");

            case DLT645_2007ControlCodeOptions.ReadData:
                return("读数据");

            case DLT645_2007ControlCodeOptions.ReadNextData:
                return("读后续数据");

            case DLT645_2007ControlCodeOptions.ReadAddress:
                return("读通信地址");

            case DLT645_2007ControlCodeOptions.WriteData:
                return("写数据");

            case DLT645_2007ControlCodeOptions.WriteAddress:
                return("写通信地址");

            case DLT645_2007ControlCodeOptions.Freeze:
                return("冻结命令");

            case DLT645_2007ControlCodeOptions.UpdateBaudrates:
                return("更改通信速率");

            case DLT645_2007ControlCodeOptions.UpdatePassword:
                return("修改密码");

            case DLT645_2007ControlCodeOptions.DemandReset:
                return("最大需量清零");

            case DLT645_2007ControlCodeOptions.IndicatedReset:
                return("电量清零");

            case DLT645_2007ControlCodeOptions.EventReset:
                return("事件清零");

            default:
                throw new NotImplementedException("尚未实现该枚举。");
            }
        }
Пример #2
0
        /// <summary>
        /// 向电表发送数据。所发送的数据为规约文档所描述帧结构中的“数据域”部分,且无需加0x33。
        /// </summary>
        /// <param name="controlCode"></param>
        /// <param name="sendData">需要发送的数据。该数据为规约文档所描述帧结构中的“数据域”部分,且无需加0x33。</param>
        /// <param name="delay"></param>
        /// <returns>返回的数据。该数据为规约文档所描述帧结构中的“数据域”部分,且已经过减0x33处理。</returns>
        public byte[] SendData(DLT645_2007ControlCodeOptions controlCode, byte[] sendData, int delay)
        {
            int intIndex = 0;

            byte[] bytData = new byte[sendData.Length + 12 + 4];

            //前导字节
            bytData[intIndex++] = 0xFE;
            bytData[intIndex++] = 0xFE;
            bytData[intIndex++] = 0xFE;
            bytData[intIndex++] = 0xFE;

            int  intCSStartIndex = intIndex;
            byte bytCS           = 0;

            //帧起始符
            bytData[intIndex++] = 0x68;

            //地址域
            bool bolIsBroadcastAddress = (this.Address == 0);

            switch (controlCode)
            {
            case DLT645_2007ControlCodeOptions.ProofreadTime:
                bolIsBroadcastAddress = true;
                break;

            case DLT645_2007ControlCodeOptions.ReadAddress:
            case DLT645_2007ControlCodeOptions.WriteAddress:
                bolIsBroadcastAddress = false;
                for (int iAddr = 0; iAddr < 6; iAddr++)
                {
                    bytData[intIndex++] = 0xAA;
                }
                break;

            default:
                if (bolIsBroadcastAddress == false)
                {
                    long intAddrMod = 1;
                    for (int iAddr = 0; iAddr < 6; iAddr++)
                    {
                        if ((base.Address / intAddrMod) > 0)
                        {
                            byte bytAddr = (byte)((base.Address / intAddrMod) % 100);
                            bytData[intIndex++] = Function.ByteToBCD(bytAddr);
                        }
                        else
                        {
                            bytData[intIndex++] = 0x00;
                        }
                        intAddrMod *= 100;
                    }
                }
                break;
            }

            if (bolIsBroadcastAddress)
            {
                bytData[intIndex++] = 0x99;
                bytData[intIndex++] = 0x99;
                bytData[intIndex++] = 0x99;
                bytData[intIndex++] = 0x99;
                bytData[intIndex++] = 0x99;
                bytData[intIndex++] = 0x99;
            }

            //帧起始符
            bytData[intIndex++] = 0x68;

            //控制码
            bytData[intIndex++] = (byte)controlCode;

            //数据长度
            bytData[intIndex++] = (byte)sendData.Length;

            for (int i = 0; i < sendData.Length; i++)
            {
                bytData[intIndex++] = (byte)(sendData[i] + 0x33);
            }

            //校验和
            for (int i = intCSStartIndex; i < intIndex; i++)
            {
                bytCS += bytData[i];
            }
            bytData[intIndex++] = bytCS;

            //结束字符
            bytData[intIndex++] = 0x16;

            base.OnSendingData(this, bytData);

            if (delay >= 0)
            {
                bytData = base.Parent.SendData(this, bytData, delay);

                int intReceiveStartIndex = -1;
                for (int i = 0; i < bytData.Length; i++)
                {
                    if (bytData[i] == (byte)0x68)
                    {
                        intReceiveStartIndex = i; // 查找0x68
                        break;
                    }
                }

                if (intReceiveStartIndex == -1)
                {
                    throw new Exception("返回的数据(" + Function.OutBytes(bytData) + ")无帧起始符0x68");
                }

                int intReceiveDataLength = bytData[intReceiveStartIndex + 9]; // 返回的数据长度

                int intReceiveCheckSum = 0;
                for (int i = intReceiveStartIndex + 0; i <= intReceiveStartIndex + 9 + intReceiveDataLength; i++)
                {
                    intReceiveCheckSum += bytData[i];
                }

                if ((byte)intReceiveCheckSum != bytData[intReceiveStartIndex + 9 + intReceiveDataLength + 1])
                {
                    throw new Exception("返回的数据(" + Function.OutBytes(bytData) + ")校验错误");
                }

                byte bytReceiveControl = bytData[intReceiveStartIndex + 8]; // 控制码

                switch (controlCode)
                {
                case DLT645_2007ControlCodeOptions.ReadData:
                    if (bytReceiveControl == (byte)0xD1)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.WriteData:
                    if (bytReceiveControl == (byte)0xD4)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.UpdateBaudrates:
                    if (bytReceiveControl == (byte)0xD7)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.UpdatePassword:
                    if (bytReceiveControl == (byte)0xD8)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.DemandReset:
                    if (bytReceiveControl == (byte)0xD9)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.IndicatedReset:
                    if (bytReceiveControl == (byte)0xDA)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;

                case DLT645_2007ControlCodeOptions.EventReset:
                    if (bytReceiveControl == (byte)0xDB)
                    {
                        throw new Exception("电表应答异常:" + this.GetErrorContext((byte)(bytData[intReceiveStartIndex + 10] - 0x33)));
                    }
                    break;
                }

                byte[] bytReceiveData = new byte[intReceiveDataLength];

                for (int intDataIndex = 0; intDataIndex < intReceiveDataLength; intDataIndex++)
                {
                    bytReceiveData[intDataIndex] = (byte)(bytData[intReceiveStartIndex + 10 + intDataIndex] - 0x33);
                }

                return(bytReceiveData);
            }
            else
            {
                return(null);
            }
        }