/// <summary>
        /// 处理F命令上去的唯一性编码
        /// </summary>
        private static void HandleSoleCoding(int soleCodingCount, byte[] data, ushort startIndex, GetSwitchboardParamCommResponse cdata)
        {
            int          index = 0;
            byte         branch;                                                      //分支号
            byte         address;                                                     //地址号
            RealDataItem item;
            UInt32       soleCoding;                                                  //唯一性编码;
            ushort       SensorDevID     = 0;                                         //设备的类型
            ItemState    SensorTempState = ItemState.EquipmentCommOK;                 //设备状态

            for (; index < soleCodingCount; index++)                                  //有多少个唯一性编码
            {
                SensorTempState     = ItemState.EquipmentCommOK;                      //设备状态
                branch              = data[startIndex++];                             //分支号及地址号
                SensorDevID         = data[startIndex++];                             //设备型号
                soleCoding          = CommandUtil.ConvertByteToInt(data, startIndex); //唯一性编码
                address             = (byte)(branch >> 3);                            //地址号
                branch              = (byte)(branch & 0x07);                          //分支号
                branch             += 1;
                item                = new RealDataItem();
                item.DeviceProperty = ItemDevProperty.SoleCoding;
                item.DeviceTypeCode = SensorDevID; //设备型号
                item.BranchNumber   = branch;      //分支号
                if ((soleCoding & 0xFFFFFF) != 0x00)
                {
                    item.SoleCoding = (((soleCoding >> 19) & 0x1F) + 2018).ToString().PadLeft(4, '0')
                                      + ((soleCoding >> 15) & 0x0F).ToString().PadLeft(2, '0')
                                      + ((soleCoding >> 10) & 0x1F).ToString().PadLeft(2, '0')
                                      + (soleCoding & 0x3FF).ToString().PadLeft(4, '0');
                }
                else
                {
                    item.SoleCoding = "0";
                }
                item.Address  = "0";
                item.Channel  = address.ToString();//地址号
                item.RealData = "0";
                item.State    = SensorTempState;
                cdata.RealDataItems.Add(item);
                startIndex += 4;
            }
        }
Ejemplo n.º 2
0
        public void HandleHistoryRealData(byte[] data, MasProtocol protocol, ushort startIndex, byte deviceCommunicationType, string point)
        {
            string startIndexItems = "";

            try
            {
                uint      time;
                byte      tempcur = 0, mincount, parametercount;
                byte      DecimalDigits = 0, minus;//小数点位数
                ushort    ushortcur = 0;
                float     fltcur;
                DateTime  SaveTime;
                string    Channel;
                ItemState State;
                QueryHistoryRealDataResponse ResponseObject = new QueryHistoryRealDataResponse();
                protocol.ProtocolType               = ProtocolType.QueryHistoryRealDataResponse;
                ResponseObject.DeviceCode           = point;
                ResponseObject.HistoryRealDataItems = new List <Common.Protocols.Devices.DeviceHistoryRealDataItem>();
                DeviceHistoryRealDataItem item;
                ItemDevProperty           deviceProperty = ItemDevProperty.Analog;
                ResponseObject.MinuteDataTotal = 0;

                if (startIndex + 9 < data.Length)
                {
                    ResponseObject.MinuteDataTotal = (ushort)((data[startIndex + 8] << 8) + (data[startIndex + 9]));
                    startIndex += 10;
                    mincount    = data[startIndex];
                    startIndex++;
                    for (int index = 0; index < mincount; index++)
                    {
                        startIndexItems += startIndex + "-";
                        time             = CommandUtil.ConvertByteToInt(data, startIndex);//时间
                        startIndex      += 4;
                        SaveTime         = Cache.TurnTimeFromInt(time);
                        tempcur          = data[startIndex++];//通道号
                        Channel          = ((tempcur & 0x1F) + 1).ToString();
                        tempcur        >>= 5;
                        if (tempcur == 6)
                        {
                            State = ItemState.EquipmentTypeError;
                        }
                        else if (tempcur == 5)
                        {
                            State = ItemState.EquipmentDown;
                        }
                        else if (tempcur == 4)
                        {
                            State = ItemState.EquipmentBiterror;
                        }
                        else
                        {
                            #region ----正常有数据需要处理----
                            State          = ItemState.EquipmentCommOK;
                            parametercount = data[startIndex++];//参数个数
                            for (int i = 0; i < parametercount; i++)
                            {
                                item             = new DeviceHistoryRealDataItem();
                                item.HistoryDate = SaveTime;
                                item.Channel     = Channel;
                                item.State       = State;
                                tempcur          = data[startIndex++];
                                #region ----解析参数属性----

                                if ((tempcur & 0x0F) == 2)
                                {
                                    item.State = ItemState.EquipmentChange;
                                }
                                else if ((tempcur & 0x0F) == 1)
                                {
                                    item.State = ItemState.EquipmentHeadDown;
                                }
                                tempcur >>= 4;
                                if ((tempcur & 0x03) == 1)
                                {
                                    deviceProperty = ItemDevProperty.Control;
                                }
                                else if ((tempcur & 0x03) == 2)
                                {
                                    deviceProperty = ItemDevProperty.Analog;
                                }
                                else if ((tempcur & 0x03) == 3)
                                {
                                    deviceProperty = ItemDevProperty.Derail;
                                }
                                item.DeviceProperty = deviceProperty;

                                #endregion
                                item.Address = (parametercount == 1 ? "0" : (i + 1).ToString());
                                ushortcur    = CommandUtil.ConvertByteToInt16(data, startIndex);//平均值
                                startIndex  += 2;
                                if (deviceProperty == ItemDevProperty.Analog)
                                {
                                    DecimalDigits = (byte)((ushortcur >> 13) & 0x03);
                                    fltcur        = (float)((ushortcur & 0x1FFF) / Math.Pow(10, DecimalDigits));
                                    minus         = (byte)(ushortcur >> 15);
                                    if (minus == 1)
                                    {
                                        fltcur = -fltcur;
                                    }
                                    //item.FiveAvgData = fltcur;
                                }
                                else
                                {
                                    //item.FiveAvgData = ushortcur;
                                }
                                ushortcur   = CommandUtil.ConvertByteToInt16(data, startIndex);//最大值
                                startIndex += 2;
                                if (deviceProperty == ItemDevProperty.Analog)
                                {
                                    DecimalDigits = (byte)((ushortcur >> 13) & 0x03);
                                    fltcur        = (float)((ushortcur & 0x1FFF) / Math.Pow(10, DecimalDigits));
                                    minus         = (byte)(ushortcur >> 15);
                                    if (minus == 1)
                                    {
                                        fltcur = -fltcur;
                                    }
                                    //item.FiveMaxData = fltcur;
                                }
                                else
                                {
                                    //item.FiveMaxData = ushortcur;
                                }
                                ushortcur   = CommandUtil.ConvertByteToInt16(data, startIndex);//最小值
                                startIndex += 2;
                                if (deviceProperty == ItemDevProperty.Analog)
                                {
                                    DecimalDigits = (byte)((ushortcur >> 13) & 0x03);
                                    fltcur        = (float)((ushortcur & 0x1FFF) / Math.Pow(10, DecimalDigits));
                                    minus         = (byte)(ushortcur >> 15);
                                    if (minus == 1)
                                    {
                                        fltcur = -fltcur;
                                    }
                                    //item.FiveMinData = fltcur;
                                }
                                else
                                {
                                    //item.FiveMinData = ushortcur;
                                }
                                if (deviceProperty == ItemDevProperty.Analog)
                                {
                                    //处理最大值时间和最小值时间
                                    ushortcur   = CommandUtil.ConvertByteToInt16(data, startIndex);//最大值时间
                                    startIndex += 2;
                                    //item.FiveMaxDateTime = item.SaveTime.AddSeconds(ushortcur);

                                    ushortcur   = CommandUtil.ConvertByteToInt16(data, startIndex);//
                                    startIndex += 2;
                                    //item.FiveMinDataTime = item.SaveTime.AddSeconds(ushortcur);//最小值时间
                                }
                                ResponseObject.HistoryRealDataItems.Add(item);
                            }
                            #endregion
                        }
                    }
                }
                protocol.Protocol = ResponseObject;
            }
            catch (Exception ex)
            {
                LogHelper.Error("QueryHistoryRealDataResponseCommand HandleHistoryRealData Error:" + ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 智能开停的初始化下发
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        //private int GetZNDerailControlCapacity(byte[] bytes, int index)
        //{
        //    ushort controldefine =0;
        //    ushort znDefine = 0;
        //    controldefine = CommandUtil.ConvertByteToInt16(bytes, 11);//定义的控制口
        //    znDefine = bytes[13];
        //    List<DeviceInfo> items;//测点设备集合
        //    for (int i = 0; i < 8; i++)
        //    {
        //        if ((znDefine & (1 << i)) == (1 << i))//定义的智能开停
        //        {
        //            items = DeviceListItem.FindAll(delegate(DeviceInfo p) { return p.Fzh == def.Fzh && p.Kh == i + 17 && p.DevPropertyID == 2; });
        //            if (items == null || items.Count == 0)
        //            {
        //                bytes[index++] = 0;//0态
        //                bytes[index++] = 0;//1态
        //                bytes[index++] = 0;//2态
        //                continue;
        //            }
        //            bytes[index++] = GetControlByteCapacity(controldefine, (ushort)((items[0].K1 & 0xFF) | (items[0].K1 >> 8)));//0态
        //            bytes[index++] = GetControlByteCapacity(controldefine, (ushort)((items[0].K2 & 0xFF) | (items[0].K2 >> 8)));//1态
        //            bytes[index++] = GetControlByteCapacity(controldefine, (ushort)((items[0].K3 & 0xFF) | (items[0].K3 >> 8)));//2态
        //        }
        //    }
        //    return index;
        //}
        /// <summary>
        /// 得到智能分站模拟量和开关量的下发初始化信息。
        /// </summary>
        /// <param name="bytes">下发的数组</param>
        /// <param name="index">填充的开始位置</param>
        /// <returns></returns>
        private int GetAnalogDerailCapacity(byte[] bytes, int index)
        {
            List <DeviceInfo> items;                                                 //测点设备集合
            //ushort allcontroldefine = CommandUtil.ConvertByteToInt16(bytes, 11);//定义的控制口
            uint analogderaildefine = CommandUtil.ConvertByteToInt(bytes, 9, false); //定义的模开控

            for (int i = 0; i < 32; i++)
            {
                if ((analogderaildefine & (1 << i)) == (1 << i))//此设备有定义
                {
                    items = DeviceListItem.FindAll(delegate(DeviceInfo p) { return(p.Fzh == def.Fzh && p.Kh == i + 1 && ((p.DevPropertyID == 1 || p.DevPropertyID == 2) || (p.DevPropertyID == 3 && p.Dzh != 0))); });
                    if (items == null || items.Count == 0)
                    {//表示前面有这个测点现在没有这个测点了,此时,把对应的定义标记设置为0
                        if (i > 23)
                        {
                            bytes[12] = (byte)(bytes[12] & (~(1 << (i - 24))));
                        }
                        else if (i > 15)
                        {
                            bytes[11] = (byte)(bytes[11] & (~(1 << (i - 16))));
                        }
                        else if (i > 7)//高在前
                        {
                            bytes[10] = (byte)(bytes[10] & (~(1 << (i - 8))));
                        }
                        else
                        {
                            bytes[9] = (byte)(bytes[9] & (~(1 << i)));
                        }
                        continue;
                    }
                    items = items.OrderBy(temp => temp.Dzh).ToList(); //按照通道号排序,默认第一个参数上的型号为此设备的型号,多参时。
                    for (int j = 0; j < items.Count; j++)             //处理多参数传感器
                    {
                        switch (items[j].DevPropertyID)
                        {
                        case 1:                                         //模拟量
                            bytes[index++] = (byte)items[j].Kh;         //地址号
                            bytes[index++] = (byte)items[j].DevModelID; //设备大类
                            if (items.Count == 1)
                            {
                                bytes[index] = 0;                      //标记字节
                            }
                            else
                            {
                                bytes[index] = (byte)(items[j].Dzh);    //后续还有参数
                            }
                            index++;
                            index = GetAnalogCapacity(bytes, index, items[j]);
                            break;

                        case 2:                                         //开关量
                            bytes[index++] = (byte)items[j].Kh;         //地址号
                            bytes[index++] = (byte)items[j].DevModelID; //设备大类
                            if (items.Count == 1)
                            {
                                bytes[index++] = 0;                      //标记字节
                            }
                            else
                            {
                                bytes[index++] = (byte)(j + 1);                                       //后续还有参数
                            }
                            CommandUtil.ConvertInt16ToByte((ushort)items[j].K1, bytes, index, false); //
                            index += 2;
                            CommandUtil.ConvertInt16ToByte((ushort)items[j].K2, bytes, index, false); //
                            index += 2;
                            CommandUtil.ConvertInt16ToByte((ushort)items[j].K3, bytes, index, false); //
                            index += 2;
                            break;

                        case 3:                                         //智能断电器
                            bytes[index++] = (byte)items[j].Kh;         //地址号
                            bytes[index++] = (byte)items[j].DevModelID; //设备大类
                            break;
                        }
                        if (j == 0 && items[0].DevPropertyID == 3)//表示为智能断电器,只有1个字节强制性退出。
                        {
                            break;
                        }
                    }
                }
            }
            return(index);
        }
        public void HandleQueryHistoryControl(byte[] data, MasProtocol protocol, ushort startIndex, byte deviceCommunicationType, string point)
        {
            QueryHistoryControlResponse ResponseObject = new QueryHistoryControlResponse();
            DeviceHistoryControlItem    item;

            protocol.ProtocolType     = ProtocolType.QueryHistoryControlResponse;
            ResponseObject.DeviceCode = point;
            int    totoalalarm = 0, curentalarm = 0;
            uint   time;
            ushort tempshort = 0;
            byte   tempcur;
            float  fltcur;
            byte   DecimalDigits = 0, minus, SensorState;                       //小数点位数

            totoalalarm = CommandUtil.ConvertByteToInt16(data, startIndex + 6); //总报警数
            startIndex += 8;
            curentalarm = data[startIndex++];                                   //当前传输的报警数
            if (totoalalarm >= curentalarm)
            {
                ResponseObject.AlarmTotal = (ushort)(totoalalarm - curentalarm);
            }
            ResponseObject.HistoryControlItems = new List <DeviceHistoryControlItem>();
            for (int i = 0; i < curentalarm; i++)
            {//每个记录10个字节
                if (startIndex + 9 < data.Length)
                {
                    item          = new DeviceHistoryControlItem();
                    time          = CommandUtil.ConvertByteToInt(data, startIndex);//时间
                    startIndex   += 4;
                    item.SaveTime = Cache.TurnTimeFromInt(time);
                    tempcur       = data[startIndex++];
                    item.Channel  = ((tempcur & 0x1F) + 1).ToString();//todo 无法区分多参传感器
                    item.Address  = ((tempcur & 0x7F) >> 5).ToString();
                    if ((tempcur & 0x80) == 0x80)
                    {
                        item.DeviceProperty = ItemDevProperty.Analog;
                    }
                    else
                    {
                        item.DeviceProperty = ItemDevProperty.Derail;
                    }
                    tempcur       = data[startIndex++];
                    DecimalDigits = (byte)(tempcur & 0x03);
                    tempcur     >>= 2;
                    minus         = (byte)(tempcur & 0x01);
                    tempcur     >>= 1;
                    SensorState   = (byte)(tempcur & 0x0F);
                    ItemState SensorTempState = ItemState.EquipmentCommOK;     //设备状态
                    SensorTempState = Cache.GetSensorState(SensorState, 0x26); //6为类型有误
                    item.State      = SensorTempState;
                    tempshort       = CommandUtil.ConvertByteToInt16(data, startIndex);
                    startIndex     += 2;
                    if (item.DeviceProperty == ItemDevProperty.Analog)
                    {
                        fltcur = (float)(tempshort / Math.Pow(10, DecimalDigits));
                        if (minus == 1)
                        {
                            item.RealData = (-fltcur).ToString();
                        }
                        else
                        {
                            item.RealData = fltcur.ToString();
                        }
                    }
                    else
                    {
                        item.RealData = tempshort.ToString();
                    }
                    item.ControlDevice = CommandUtil.ConvertByteToInt16(data, startIndex);
                    startIndex        += 2;
                    ResponseObject.HistoryControlItems.Add(item);
                }
            }
            protocol.Protocol = ResponseObject;
        }