Ejemplo n.º 1
0
        /// <summary>
        /// 读取四组寄存器值其中前三个是连续的寄存器值
        /// </summary>
        /// <param name="startAddress1">三组连续寄存器的首地址,每组两个寄存器,值类型为float</param>
        /// <param name="startAddress2">一组不连续寄存器的首地址,读两个寄存器,值类型为float</param>
        /// <returns>读到的四组寄存器值</returns>
        public byte[] read_floatRegisters(ushort startAddress1, ushort startAddress2)
        {
            byte[] bholdregs1 = new byte[12];
            sg.ReadHoldingRegisters(startAddress1, 6, bholdregs1);
            byte[] bholdregs2 = new byte[4];
            sg.ReadHoldingRegisters(startAddress2, 2, bholdregs2);

            CTConver conver = new CTConver();

            byte[] bholdregs = new byte[16];
            bholdregs = conver.addByte(bholdregs1, bholdregs2);
            return(bholdregs);
        }
Ejemplo n.º 2
0
        void Data2mssql()
        {
            if (error)
            {
                Console.WriteLine("Something error happened");
                return;
            }
            dataTime = System.DateTime.Now;
            //Console.WriteLine("Starttime:{0}",dataTime);
            try
            {
                //费率1的正向有功电能,单位Wh,格式Double
                //meter1
                TcpMeter1.ReadHoldingRegisters(801, 4, bholdregs1);
                //meter2
                TcpMeter2.ReadHoldingRegisters(801, 4, bholdregs2);
                //meter3
                TcpMeter3.ReadHoldingRegisters(801, 4, bholdregs3);

                //总有功功率,单位W,格式Float
                //meter1  active power
                TcpMeter1.ReadHoldingRegisters(65, 2, bholdregs4);
                //meter2
                TcpMeter2.ReadHoldingRegisters(65, 2, bholdregs5);
                //meter3
                TcpMeter3.ReadHoldingRegisters(65, 2, bholdregs6);

                //总无功功率,单位VAR,格式Float
                //meter1  reactive power
                TcpMeter1.ReadHoldingRegisters(67, 2, bholdregs7);
                //meter2
                TcpMeter2.ReadHoldingRegisters(67, 2, bholdregs8);
                //meter3
                TcpMeter3.ReadHoldingRegisters(67, 2, bholdregs9);

                //各表的电压值
                //meter1  U
                TcpMeter1.ReadHoldingRegisters(1, 2, bholdregs10);
                //meter2  U
                TcpMeter2.ReadHoldingRegisters(1, 2, bholdregs11);
                //meter3  U
                TcpMeter3.ReadHoldingRegisters(1, 2, bholdregs12);

                //各表的电流值
                //meter1  I
                TcpMeter1.ReadHoldingRegisters(13, 2, bholdregs13);
                //meter2  I
                TcpMeter2.ReadHoldingRegisters(13, 2, bholdregs14);
                //meter3  I
                TcpMeter3.ReadHoldingRegisters(13, 2, bholdregs15);
                //按照上述函数的功能,应该能够采集到电表中的电压、电流、有功功率、无功功率和实际消耗的电能
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.ToString());
                errorlog.Error(e);
            }
            //Console.WriteLine("endtime:  {0}", System.DateTime.Now);
            if (System.DateTime.Now.Subtract(dataTime).Seconds > 1)
            {
                Console.WriteLine("too long time between meter reading,drop this data");
                return;
            }
            //协议数据流转换为实际物理数据
            Array.Reverse(bholdregs1);//为何要进行数组中数据的反转?
            Energy1 = BitConverter.ToDouble(bholdregs1, 0);
            Array.Reverse(bholdregs2);
            Energy2 = BitConverter.ToDouble(bholdregs2, 0);
            Array.Reverse(bholdregs3);
            Energy3 = BitConverter.ToDouble(bholdregs3, 0);
            //Deta1Energy = Energy1 - Energy2 - Energy3;

            Array.Reverse(bholdregs4, 0, 4);
            Power1 = BitConverter.ToSingle(bholdregs4, 0);
            Array.Reverse(bholdregs5, 0, 4);
            Power2 = BitConverter.ToSingle(bholdregs5, 0);
            Array.Reverse(bholdregs6, 0, 4);
            Power3 = BitConverter.ToSingle(bholdregs6, 0);

            Array.Reverse(bholdregs7, 0, 4);
            RPower1 = BitConverter.ToSingle(bholdregs7, 0);
            Array.Reverse(bholdregs8, 0, 4);
            RPower2 = BitConverter.ToSingle(bholdregs8, 0);
            Array.Reverse(bholdregs9, 0, 4);
            RPower3 = BitConverter.ToSingle(bholdregs9, 0);

            Array.Reverse(bholdregs10, 0, 4);
            U1 = BitConverter.ToSingle(bholdregs10, 0);
            Array.Reverse(bholdregs11, 0, 4);
            U2 = BitConverter.ToSingle(bholdregs11, 0);
            Array.Reverse(bholdregs12, 0, 4);
            U3 = BitConverter.ToSingle(bholdregs12, 0);

            Array.Reverse(bholdregs13, 0, 4);
            I1 = BitConverter.ToSingle(bholdregs13, 0);
            Array.Reverse(bholdregs14, 0, 4);
            I2 = BitConverter.ToSingle(bholdregs14, 0);
            Array.Reverse(bholdregs15, 0, 4);
            I3 = BitConverter.ToSingle(bholdregs15, 0);

            switch (SampleCount)
            {
            case 0:    //第一次采样
                SampleCount++;
                //Deta1EnergyPrevious = Deta1Energy;
                Energy1Previous = Energy1;
                Energy2Previous = Energy2;
                Energy3Previous = Energy3;
                break;

            default:    //第二次采样,可计算出两次采样间隔间的能量消耗在三表间的偏差Deta2Energy,以此为偏差基准Deta2EnergyBase
                SampleCount++;
                //Deta2Energy = Math.Abs(Deta1Energy - Deta1EnergyPrevious - MeterBaseCost);
                //Deta1EnergyPrevious = Deta1Energy;
                //Deta2EnergyPrevious = Deta2Energy;
                //Deta2EnergyBase = Deta2Energy;//基准偏差

                CostEnergy1 = Energy1 - Energy1Previous;
                CostEnergy2 = Energy2 - Energy2Previous;
                CostEnergy3 = Energy3 - Energy3Previous;

                Energy1Previous = Energy1;
                Energy2Previous = Energy2;
                Energy3Previous = Energy3;

                //Ratio = (CostEnergy1 - CostEnergy2 - CostEnergy3 - MeterBaseCost) / CostEnergy1;
                Ratio = Power1 - Power2 - Power3;

                //Console.WriteLine("Deta2Energy:{0}Wh", Deta2Energy);
                //Console.WriteLine("CostEnergy1-2-3:{0}Wh", CostEnergy1 - CostEnergy2 - CostEnergy3);
                break;
                //default://经过两次采样后的采样
                //Deta2Energy = Math.Abs(Deta1Energy - Deta1EnergyPrevious - MeterBaseCost);
                //Deta1EnergyPrevious = Deta1Energy;
                //Deta2EnergyPrevious = Deta2Energy;
                //Deta3Energy = Deta2Energy - Deta2EnergyBase;

                //CostEnergy1 = Energy1 - Energy1Previous;
                //CostEnergy2 = Energy2 - Energy2Previous;
                //CostEnergy3 = Energy3 - Energy3Previous;

                //Energy1Previous = Energy1;
                //Energy2Previous = Energy2;
                //Energy3Previous = Energy3;

                //Ratio = (CostEnergy1 - CostEnergy2 - CostEnergy3 - MeterBaseCost) / CostEnergy1;
                //Ratio = Power1 - Power2 - Power3;
                //Ratio=Deta2Energy/CostEnergy1;
                //Console.WriteLine("Deta2Energy:{0}Wh", Deta2Energy);
                //Console.WriteLine("CostEnergy1-2-3:{0}Wh", CostEnergy1 - CostEnergy2 - CostEnergy3);
                //Console.WriteLine("Deta3Energy:{0}Wh", Deta3Energy);
                //break;
            }
            LogInfo();
            DebugInfo();
            if (SampleCount >= 2)
            {
                //确定报警标志
                if (Math.Abs(Ratio) > CompareRatio)
                {
                    warnflag = 1;
                }
                else
                {
                    warnflag = 0;
                }
                try
                {
                    Console.WriteLine("write to mssql");
                    //写入数据库
                    string InsertString = "INSERT INTO phydect_table(EnergySum,Energy1,Energy2,PowerSum,Power1,Power2,RPowerSum,RPower1,RPower2,USum,U1,U2,ISum,I1,I2,CostEnergy,Ratio,PhyWarn,Time) VALUES(" +
                                          Energy1 + "," +
                                          Energy2 + "," +
                                          Energy3 + "," +
                                          Power1 + "," +
                                          Power2 + "," +
                                          Power3 + "," +
                                          RPower1 + "," +
                                          RPower2 + "," +
                                          RPower3 + "," +
                                          U1 + "," +
                                          U2 + "," +
                                          U3 + "," +
                                          I1 + "," +
                                          I2 + "," +
                                          I3 + "," +
                                          CostEnergy1 + "," +
                                          Ratio + "," +
                                          warnflag + "," +
                                          "\'" + dataTime + "\'" + ")";
                    snortSqlCommand.CommandText = InsertString;

                    //用SqlCommand对象,插入一条新的记录
                    snortSqlConnection.Open();
                    snortSqlCommand.ExecuteNonQuery();
                    snortSqlConnection.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.ToString());
                    errorlog.Error(e);
                }
            }
        }