} //DriverGeneric Cttor /// <summary> /// Initialize the drivers.</summary> public void Initialize() { if ((!isConnected) && (thisDriverConf.Enable)) { switch (thisDriverConf.Type) { case DriverConfig.DriverType.XWave: //This driver is initialized in the constructor isInitialized = ObjDriverXWave.isInitialized; break; case DriverConfig.DriverType.S7_TCP: ObjDriverS7.Initialize(); isInitialized = ObjDriverS7.isInitialized; break; case DriverConfig.DriverType.ModbusTCP: ObjDriverModTCP.Initialize(); isInitialized = ObjDriverModTCP.isInitialized; break; default: Status.NewStat(StatType.Warning, "Wrong Driver Type, Check Config."); break; } if (!isInitialized) { Status.NewStat(StatType.Warning, "Driver Initialization Failed."); } } }
/// <summary> /// Initialize the Object. /// <param name="DriversConf">Driver Configuration and DataAreas (Needed for initial set).</param> /// <param name="InitialSet">Initial set flag (will erase actual tables).</param> </summary> public void Initialize(List <DriverComm.DVConfDAConfClass> DriversConf, bool InitialSet) { string myConnectionString; int retVal = 0; //Reset the Status Buffer Status.ResetStat(); //myConnectionString = "server=127.0.0.1;uid=root;" + "pwd=12345;database=test;"; myConnectionString = "SERVER=" + DBConfig.URL + ";" + "PORT=" + DBConfig.Port.ToString() + ";" + "PROTOCOL=" + DBConfig.Protocol.ToString().ToLower() + ";" + "DATABASE=" + DBConfig.DBname + ";" + "UID=" + DBConfig.Username + ";" + "PASSWORD="******";" + "ConnectionTimeout=5; DefaultCommandTimeout=5;Keepalive=3" + ";"; try { conn = new MySqlConnection(myConnectionString); if (Connect()) { Disconnect(); //Database tables initialization. if (InitialSet) { retVal = InitDB(DriversConf); } if (retVal == 0) { isInitialized = true; Status.NewStat(StatT.Good); } else { Status.NewStat(StatT.Bad, "Initialization Failed..."); } } } catch (Exception e) { Status.NewStat(StatT.Bad, e.Message); } } //END Function Initialize
}//END Constructor /// <summary> /// Initialize the object class and prepares for the Server Device connection.</summary> public void Initialize() { //Flag for the configuration validation. bool retVal = true; if (!((MasterDriverConf != null) && (Status != null))) { retVal = false; Status.NewStat(StatType.Warning, "Master Objects are Invalid."); } if (retVal) { if ((!isInitialized) && (!isConnected)) { //Check the TCP port for the XWave device comms. //Check the UDP port for the XWave device comms. if (!((MasterDriverConf.portTCP > 100) && (MasterDriverConf.portUDP > 100))) { retVal = false; Status.NewStat(StatType.Warning, "Wrong Config Params, Check Config."); } //Check the IP Address if (MasterDriverConf.Address.Length < 6) { retVal = false; Status.NewStat(StatType.Warning, "Wrong Config Params, Wrong IP."); } try { //Get the Vars Tree initialized from the XML configuration file. if (retVal) { retVal = XWaveDriver.Init(MasterDriverConf.DefFile, out NumVars.nTVars, out DataInit); } } catch (Exception e) { Status.NewStat(StatType.Bad, e.Message); retVal = false; } //Count the vars of each type from the Var Tree. if (retVal) { retVal = CountVars(); } if (retVal) { Status.NewStat(StatType.Good); } else { Status.NewStat(StatType.Bad, "Initialization Failed."); } isInitialized = retVal; } } }//END Function Initialize
/// <summary> /// Initialize the object class and prepares for the Server Device connection.</summary> public void Initialize() { bool retVal = true; int i, j, SAddress, boolReg; DAConfClass thisArea; if (!((MasterDriverConf != null) && (Status != null))) { retVal = false; Status.NewStat(StatType.Warning, "Master Objects are Invalid."); } if (retVal) if ((!isInitialized) && (MasterDriverConf.Enable)) { try { //Create the driver object ModTCPObj = new ModbusClient(MasterDriverConf.Address, MasterDriverConf.portTCP); ModTCPObj.ConnectionTimeout = MasterDriverConf.Timeout; IntData = new ModbusDataConta[MasterDriverConf.NDataAreas]; j = 0; //Count the enabled Data Areas //Cicle and configure the data areas for (i = 0; i < MasterDriverConf.NDataAreas; i++) { thisArea = MasterDataAreaConf[i]; SAddress = int.Parse(thisArea.StartAddress); if (thisArea.Enable) { IntData[i].nVals = thisArea.Amount; switch (thisArea.dataType) { case DriverConfig.DatType.Bool: if ((thisArea.DBnumber >= 1) && (thisArea.DBnumber <= 2)) { IntData[i].dBool = new bool[thisArea.Amount]; IntData[i].nVals = thisArea.Amount; } else if ((thisArea.DBnumber >= 3) && (thisArea.DBnumber <= 4)) { boolReg = (int)Math.Ceiling(thisArea.Amount / 16.0); IntData[i].dInt = new int[boolReg]; IntData[i].nVals = boolReg; } else { //Invalid Conf IntData[i].dInt = null; IntData[i].nVals = -1; } break; case DriverConfig.DatType.Byte: case DriverConfig.DatType.Word: IntData[i].dInt = new int[thisArea.Amount]; break; case DriverConfig.DatType.DWord: case DriverConfig.DatType.Real: IntData[i].dInt = new int[(thisArea.Amount * 2)]; break; default: retVal = false; Status.NewStat(StatType.Warning, "Wrong DataArea Type, Check Config."); break; } j++; }// Area Enable } // For Data Areas //check there is enabled data areas. if (j == 0) retVal = false; } catch (Exception e) { Status.NewStat(StatType.Bad, e.Message); isInitialized = false; } } //IF Not Initialized and Driver is Enabled if (retVal) Status.NewStat(StatType.Good); else Status.NewStat(StatType.Bad, "Initialization Failed."); isInitialized = retVal; }
/// <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
} //END Class Constructor /// <summary> /// Add a driver to be managed by this Database. /// *This method is not thread safe* /// <param name="DriverConf">Configuration Object for driver config.</param> /// <param name="DAreaConf">Configuration Object for Data Area config.</param> </summary> public int addDriver(DriverComm.DVConfClass DriverConf, DriverComm.DAConfClass[] DAreaConf) { int retVal; DriverComm.DVConfDAConfClass NewDriver; retVal = -1; if ((DriverConf != null) && (DAreaConf != null)) { if (DAreaConf.Length > 0) { NewDriver = new DriverComm.DVConfDAConfClass(DriverConf, DAreaConf); DriversConf.Add(NewDriver); //Adding a new driver while initialized will require new initzialization. if (isInitialized) { Status.NewStat(StatT.Warning, "DB Object Requires to Re-Initialize"); isInitialized = false; retVal = 1; } else { Status.NewStat(StatT.Good, "DV Added: " + NewDriver.DVConf.ID.ToString("00")); retVal = 0; } } else { retVal = -2; Status.NewStat(StatT.Bad, "Internal Data Corruption."); } } return(retVal); } //END Add Driver function.
/// <summary> /// Initialize the Driver variables and prepare for connection.</summary> public void Initialize() { bool retVal = true; int i, j, datSize, SAddress, tAmount; DAConfClass thisArea; if (!((MasterDriverConf != null) && (Status != null))) { retVal = false; Status.NewStat(StatType.Warning, "Master Objects are Invalid."); } if (retVal) { if ((!isInitialized) && (MasterDriverConf.Enable)) { try { // Client creation Client = new S7Client(); //Configure the timeouts //Client.SetParam(S7Consts.p_i32_PingTimeout, ref MasterDriverConf.Timeout); Client.SetParam(S7Consts.p_i32_RecvTimeout, ref MasterDriverConf.Timeout); Client.SetParam(S7Consts.p_i32_SendTimeout, ref MasterDriverConf.Timeout); //Generate the MultiVar Objects Reader = new S7MultiVar(Client); Writer = new S7MultiVar(Client); IntData = new DataExtClass.DataContainer[MasterDriverConf.NDataAreas]; j = 0; //Count the enabled Data Areas //Cicle and configure the data areas for (i = 0; i < MasterDriverConf.NDataAreas; i++) { thisArea = MasterDataAreaConf[i]; SAddress = int.Parse(thisArea.StartAddress); datSize = S7Client.S7WLByte; //Always read bytes if (thisArea.Enable) { switch (thisArea.dataType) { case DriverConfig.DatType.Bool: tAmount = (int)Math.Ceiling((thisArea.Amount / 8.0)); IntData[i].dByte = new byte[tAmount]; if (!thisArea.Write) { //Add Reader variable areas. Reader.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } else { //Add Writer variable areas. Writer.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } break; case DriverConfig.DatType.Byte: IntData[i].dByte = new byte[thisArea.Amount]; if (!thisArea.Write) { //Add Reader variable areas. Reader.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, thisArea.Amount, ref IntData[i].dByte); } else { //Add Writer variable areas. Writer.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, thisArea.Amount, ref IntData[i].dByte); } break; case DriverConfig.DatType.Word: tAmount = (int)(thisArea.Amount * 2.0); IntData[i].dByte = new byte[tAmount]; IntData[i].dWord = new UInt16[thisArea.Amount]; if (!thisArea.Write) { //Add Reader variable areas. Reader.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } else { //Add Writer variable areas. Writer.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } break; case DriverConfig.DatType.DWord: tAmount = (int)(thisArea.Amount * 4.0); IntData[i].dByte = new byte[tAmount]; IntData[i].dDWord = new UInt32[thisArea.Amount]; if (!thisArea.Write) { //Add Reader variable areas. Reader.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } else { //Add Writer variable areas. Writer.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } break; case DriverConfig.DatType.Real: tAmount = (int)(thisArea.Amount * 4.0); IntData[i].dByte = new byte[tAmount]; IntData[i].dReal = new float[thisArea.Amount]; if (!thisArea.Write) { //Add Reader variable areas. Reader.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } else { //Add Writer variable areas. Writer.Add(S7Client.S7AreaDB, datSize, thisArea.DBnumber, SAddress, tAmount, ref IntData[i].dByte); } break; default: retVal = false; Status.NewStat(StatType.Warning, "Wrong DataArea Type, Check Config."); break; } j++; } // Area Enable } // For Data Areas //check there is enabled data areas. if (j == 0) { retVal = false; } } catch (Exception e) { retVal = false; Status.NewStat(StatType.Bad, e.Message); } } // IF not isInitialized } if (retVal) { Status.NewStat(StatType.Good); } else { Status.NewStat(StatType.Bad, "Initialization Failed."); } isInitialized = retVal; } // END Function Initialized
/// <summary> /// Initialize the Object. /// <param name="DriversConf">Driver Configuration and DataAreas (Needed for initial set).</param> /// <param name="InitialSet">Initial set flag (will erase actual tables).</param> </summary> public void Initialize(List <DriverComm.DVConfDAConfClass> DriversConf, bool InitialSet) { int retVal = 0; isInitialized = false; //Reset the Status Buffer Status.ResetStat(); //myConnectionString = "server=127.0.0.1;uid=root;" + "pwd=12345;database=test;"; myConnectionString = "SERVER=" + DBConfig.URL + ";" + "PORT=" + DBConfig.Port.ToString() + ";" + "PROTOCOL=" + DBConfig.Protocol.ToString().ToLower() + ";" + "DATABASE=" + DBConfig.DBname + ";" + "UID=" + DBConfig.Username + ";" + "PASSWORD="******";" + "ConnectionTimeout=5; DefaultCommandTimeout=5; Keepalive=3;" + "Pooling=true; MinimumPoolSize=2; MaximumPoolsize=20; ConnectionReset=false;" + "ConnectionLifeTime=1800; CacheServerProperties=true;"; try { MySqlConnection conn = new MySqlConnection(myConnectionString); conn.Open(); //Test the connection if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); //Disconnect //Database tables initialization. if (InitialSet) { retVal = InitDB(DriversConf); } if (retVal == 0) { isInitialized = true; Status.NewStat(StatT.Good); } else { Status.NewStat(StatT.Bad, "Initialization Failed..."); } } else { Status.NewStat(StatT.Bad, "Database Connection Failed..."); } } catch (Exception e) { Status.NewStat(StatT.Bad, e.Message); } } //END Function Initialize