Exemple #1
0
        /// <summary>
        /// Cloning Method
        /// Returns a new copy of himself.</summary>
        public DataExtClass clone()
        {
            DataExtClass NewMe = new DataExtClass();

            //Clone the Data Struct Arrays
            if (this.Data.dBoolean != null)
            {
                NewMe.Data.dBoolean = (bool[])this.Data.dBoolean.Clone();
            }

            if (this.Data.dByte != null)
            {
                NewMe.Data.dByte = (byte[])this.Data.dByte.Clone();
            }

            if (this.Data.dWord != null)
            {
                NewMe.Data.dWord = (UInt16[])this.Data.dWord.Clone();
            }

            if (this.Data.dDWord != null)
            {
                NewMe.Data.dDWord = (UInt32[])this.Data.dDWord.Clone();
            }

            if (this.Data.dsDWord != null)
            {
                NewMe.Data.dsDWord = (Int32[])this.Data.dsDWord.Clone();
            }

            if (this.Data.dReal != null)
            {
                NewMe.Data.dReal = (float[])this.Data.dReal.Clone();
            }

            if (this.Data.dString != null)
            {
                NewMe.Data.dString = (string[])this.Data.dString.Clone();
            }


            NewMe.NowTimeTicks = this.NowTimeTicks;
            NewMe.FirstInit    = this.FirstInit;

            //Clone the VarNames just in case.
            if (this.VarNames != null)
            {
                NewMe.VarNames = (string[])this.VarNames.Clone();
            }

            //The configuration won't be clone,
            //As its intented to be Initialized at the beggining,
            //and only read in the rest of the program.
            NewMe.AreaConf = this.AreaConf;

            return(NewMe);
        }
        /// <summary>
        /// Class Constructor.
        /// <param name="DNum">Driver number, ID of the driver in the configuration.</param>
        /// <param name="DriverConfigObj">Object with the Driver configuration</param></summary>
        public DriverGeneric(int DNum, DriverConfig DriverConfigObj)
        {
            int DACount, i, DVindex;

            thisDriverConf = new DVConfClass();

            //Reading and Writing Flags Init
            iamReading = false;
            iamWriting = false;

            thisDriverConf.Enable = false;
            isInitialized         = false;

            Status = new Stat.StatReport(DNum, FileLog: true);
            Status.ResetStat();

            //Check Driver Number is not out of bounds
            if ((DriverConfigObj.DriversConf.Length >= DNum) && (DNum > 0))
            {
                //Driver index start from 0, while ID start from 1. ID=0 is reserved to the System.
                DVindex = DNum - 1;

                //General Driver Configuration parameters
                thisDriverConf.ConnConfig(DriverConfigObj.DriversConf[DVindex].ID, DriverConfigObj.DriversConf[DVindex].Enable,
                                          DriverConfigObj.DriversConf[DVindex].Type, DriverConfigObj.DriversConf[DVindex].CycleTime,
                                          DriverConfigObj.DriversConf[DVindex].Timeout);

                if (thisDriverConf.Enable)
                {
                    //Driver Specific configuration parameters
                    switch (thisDriverConf.Type)
                    {
                    case DriverConfig.DriverType.XWave:
                        thisDriverConf.ConfXwave(DriverConfigObj.DriversConf[DVindex].Address,
                                                 DriverConfigObj.DriversConf[DVindex].PortTCP, DriverConfigObj.DriversConf[DVindex].PortUDP,
                                                 DriverConfigObj.DriversConf[DVindex].DefFilePath);
                        break;

                    case DriverConfig.DriverType.S7_TCP:
                        thisDriverConf.ConfS7(DriverConfigObj.DriversConf[DVindex].Address,
                                              DriverConfigObj.DriversConf[DVindex].Rack, DriverConfigObj.DriversConf[DVindex].Slot);
                        break;

                    case DriverConfig.DriverType.ModbusTCP:
                        thisDriverConf.ConfModbusTCP(DriverConfigObj.DriversConf[DVindex].Address,
                                                     DriverConfigObj.DriversConf[DVindex].PortTCP);
                        break;

                    case DriverConfig.DriverType.ModbusRTU:
                        thisDriverConf.ConfModbusRTU(DriverConfigObj.DriversConf[DVindex].PortRTU,
                                                     DriverConfigObj.DriversConf[DVindex].RTUid, DriverConfigObj.DriversConf[DVindex].RTUBaud,
                                                     DriverConfigObj.DriversConf[DVindex].RTUParity, DriverConfigObj.DriversConf[DVindex].RTUStop);
                        break;

                    default:
                        // Disable the driver as it was not configured properly.
                        thisDriverConf.Enable = false;
                        Status.NewStat(StatType.Warning, "Wrong Driver Config Params: Driver Type.");
                        break;
                    }

                    if (thisDriverConf.Type != DriverConfig.DriverType.XWave)
                    {
                        //Run thru all the DataAreas and copy the ones that has the link to the Driver ID.
                        DACount = 0;
                        foreach (DataAreaConf DataAreaElement in DriverConfigObj.DataAreasConf)
                        {
                            if (DataAreaElement.ID_Driver == thisDriverConf.ID)
                            {
                                if (DataAreaElement.Enable)
                                {
                                    DACount++;
                                }
                            }
                        }

                        if (DACount > 0)
                        {
                            thisAreaConf = new DAConfClass[DACount];
                            ExtData      = new DataExtClass[DACount];
                            thisDriverConf.NDataAreas = DACount;
                            i = 0;
                            foreach (DataAreaConf DataAreaElement in DriverConfigObj.DataAreasConf)
                            {
                                if ((DataAreaElement.ID_Driver == thisDriverConf.ID) && (DataAreaElement.Enable))
                                {
                                    thisAreaConf[i] = new DAConfClass(DataAreaElement.ID, DataAreaElement.ID_Driver,
                                                                      DataAreaElement.Enable, DataAreaElement.Write, DataAreaElement.ToHist,
                                                                      DataAreaElement.DataType, DataAreaElement.DB_Number, DataAreaElement.StartAddr,
                                                                      DataAreaElement.AmountVar);

                                    ExtData[i] = new DataExtClass();


                                    //Asign the configuration section to the data area.
                                    ExtData[i].AreaConf = thisAreaConf[i];

                                    //VarNames
                                    ExtData[i].VarNames  = new string[DataAreaElement.AmountVar];
                                    ExtData[i].FirstInit = false;

                                    //Create the Data container.
                                    if (DataAreaElement.DataType != DriverConfig.DatType.Undefined)
                                    {
                                        //Reading and Writing Flags Setup
                                        if (!thisAreaConf[i].Write)
                                        {
                                            iamReading = true;
                                        }
                                        if (thisAreaConf[i].Write)
                                        {
                                            iamWriting = true;
                                        }

                                        switch (DataAreaElement.DataType)
                                        {
                                        case DriverConfig.DatType.Bool:
                                            ExtData[i].Data.dBoolean = new bool[DataAreaElement.AmountVar];
                                            break;

                                        case DriverConfig.DatType.Byte:
                                            ExtData[i].Data.dByte = new byte[DataAreaElement.AmountVar];
                                            break;

                                        case DriverConfig.DatType.Word:
                                            ExtData[i].Data.dWord = new UInt16[DataAreaElement.AmountVar];
                                            break;

                                        case DriverConfig.DatType.DWord:
                                            ExtData[i].Data.dDWord = new UInt32[DataAreaElement.AmountVar];
                                            break;

                                        case DriverConfig.DatType.sDWord:
                                            ExtData[i].Data.dsDWord = new Int32[DataAreaElement.AmountVar];
                                            break;

                                        case DriverConfig.DatType.Real:
                                            ExtData[i].Data.dReal = new float[DataAreaElement.AmountVar];
                                            break;

                                        default:
                                            //Disable this Driver, as it has a configuration problem.
                                            Status.NewStat(StatType.Warning, "Wrong Driver Config Params: Data Area Type.");
                                            thisDriverConf.Enable = false;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        Status.NewStat(StatType.Warning, "Wrong Driver Config Params: Data Area Type.");
                                    }
                                    i++;
                                } //DataArea_Driver_ID==Driver_ID .and. DataArea is Enabled
                            }     //For each DataArea
                        }         //END Data Area Count >0
                        else
                        {
                            Status.NewStat(StatType.Warning, "Wrong Driver Config Params: No Data Areas Configured.");
                        }//ELSE Data Area Count >0
                    }
                    else if (thisDriverConf.Type == DriverConfig.DriverType.XWave)
                    {
                        //Special case for the XWave Driver.
                        DACount = 4;
                        thisDriverConf.NDataAreas = DACount;
                        thisAreaConf = new DAConfClass[DACount];
                        ExtData      = new DataExtClass[DACount];

                        //Reading and Writing Flags (The Xwave only reads data in this edition)
                        iamReading = true;
                        iamWriting = false;


                        //Initialize the driver to get the amount of variables for each type.
                        ObjDriverXWave = new XWave.DriverXWave(thisDriverConf, Status);

                        //The XWave Driver requires initialization to know the amount of data to be addressed.
                        ObjDriverXWave.Initialize();

                        //Configure each area acordingly.
                        if (ObjDriverXWave.isInitialized)
                        {
                            //Bool Areas.
                            ExtData[0] = new DataExtClass();
                            if (ObjDriverXWave.NumVars.nBool > 0)
                            {
                                thisAreaConf[0] = new DAConfClass(1, thisDriverConf.ID, true, false, true,
                                                                  DriverConfig.DatType.Bool, 0, "0", ObjDriverXWave.NumVars.nBool);
                                ExtData[0].Data.dBoolean = new bool[ObjDriverXWave.NumVars.nBool];
                                ExtData[0].VarNames      = new string[ObjDriverXWave.NumVars.nBool];
                                ExtData[0].FirstInit     = false;
                            }
                            else
                            {
                                thisAreaConf[0] = new DAConfClass(1, thisDriverConf.ID, false, false, false,
                                                                  DriverConfig.DatType.Bool, 0, "0", ObjDriverXWave.NumVars.nBool);
                            }
                            ExtData[0].AreaConf = thisAreaConf[0];

                            //Unsigned Double Word Areas.
                            ExtData[1] = new DataExtClass();
                            if (ObjDriverXWave.NumVars.nDWord > 0)
                            {
                                thisAreaConf[1] = new DAConfClass(2, thisDriverConf.ID, true, false, true,
                                                                  DriverConfig.DatType.DWord, 0, "0", ObjDriverXWave.NumVars.nDWord);
                                ExtData[1].Data.dDWord = new UInt32[ObjDriverXWave.NumVars.nDWord];
                                ExtData[1].VarNames    = new string[ObjDriverXWave.NumVars.nDWord];
                                ExtData[1].FirstInit   = false;
                            }
                            else
                            {
                                thisAreaConf[1] = new DAConfClass(2, thisDriverConf.ID, false, false, false,
                                                                  DriverConfig.DatType.DWord, 0, "0", ObjDriverXWave.NumVars.nDWord);
                            }
                            ExtData[1].AreaConf = thisAreaConf[1];

                            //Signed Double Word Areas.
                            ExtData[2] = new DataExtClass();
                            if (ObjDriverXWave.NumVars.nsDWord > 0)
                            {
                                thisAreaConf[2] = new DAConfClass(3, thisDriverConf.ID, true, false, true,
                                                                  DriverConfig.DatType.sDWord, 0, "0", ObjDriverXWave.NumVars.nsDWord);
                                ExtData[2].Data.dsDWord = new Int32[ObjDriverXWave.NumVars.nsDWord];
                                ExtData[2].VarNames     = new string[ObjDriverXWave.NumVars.nsDWord];
                                ExtData[2].FirstInit    = false;
                            }
                            else
                            {
                                thisAreaConf[2] = new DAConfClass(3, thisDriverConf.ID, false, false, false,
                                                                  DriverConfig.DatType.sDWord, 0, "0", ObjDriverXWave.NumVars.nsDWord);
                            }
                            ExtData[2].AreaConf = thisAreaConf[2];

                            //Float point Areas.
                            ExtData[3] = new DataExtClass();
                            if (ObjDriverXWave.NumVars.nReal > 0)
                            {
                                thisAreaConf[3] = new DAConfClass(4, thisDriverConf.ID, true, false, true,
                                                                  DriverConfig.DatType.Real, 0, "0", ObjDriverXWave.NumVars.nReal);
                                ExtData[3].Data.dReal = new float[ObjDriverXWave.NumVars.nReal];
                                ExtData[3].VarNames   = new string[ObjDriverXWave.NumVars.nReal];
                                ExtData[3].FirstInit  = false;
                            }
                            else
                            {
                                thisAreaConf[3] = new DAConfClass(4, thisDriverConf.ID, false, false, false,
                                                                  DriverConfig.DatType.Real, 0, "0", ObjDriverXWave.NumVars.nReal);
                            }
                            ExtData[3].AreaConf = thisAreaConf[3];
                        }
                        else
                        {
                            Status.NewStat(StatType.Warning, "Wrong Driver Config Params: Driver Init Failed.");
                        } // if XWave Driver isInitialized
                    }     //IF Driver Type, XWave Driver has a different treatment.

                    // Built the Drivers.
                    switch (thisDriverConf.Type)
                    {
                    case DriverConfig.DriverType.XWave:
                        //This driver is built and initialized above.
                        break;

                    case DriverConfig.DriverType.S7_TCP:
                        ObjDriverS7 = new Siemens7.DriverS7(thisDriverConf, thisAreaConf, Status);
                        break;

                    case DriverConfig.DriverType.ModbusTCP:
                        ObjDriverModTCP = new ModbusTCP.DriverModbusTCP(thisDriverConf, thisAreaConf, Status);
                        break;

                    default:
                        Status.NewStat(StatType.Warning, "Wrong Driver Type, Check Config.");
                        break;
                    }
                } //IF driver is enabled
            }
            else
            {
                Status.NewStat(StatType.Warning, "Wrong Driver Config Params: Wrong Driver ID.");
            } //IF Driver Number is out of bounds.
        }     //DriverGeneric Cttor