/// <summary> /// This class writes to the PLC for Tesla /// Tags have default values, but they can be overriden by accessing the public variables of each of the tags /// </summary> /// <param name="PLCIPAddress"></param> /// <param name="_ResultsTags"></param> /// <param name="_VehicleInfoTags"></param> public PLCComm(string PLCIPAddress) { ResultsTags = new TPM_Results_Tags(); HeartbeatTime = 500; //ms. This value can also be set through a public variable. mPLC = new Esys.IO.PLC.AB.ControlLogix(); mIPAddress = PLCIPAddress; }
public void Initialize() { try { //Setup the system logger. mLog = new Esys.Logger(Properties.Settings.Default.PathToLogFiles, Esys.Logger.NewFileFrequency.Daily); mLog.Write("****************************************************************************************"); mLog.Write("TPM Service starting."); Linc.Communication.TPM_Results_Tags ResultsTags = new Linc.Communication.TPM_Results_Tags(); ResultsTags.O_SerialNumber = "TPMDataToPC.SN"; ResultsTags.O_StemCode = "TPMDataToPC.StemCode"; ResultsTags.O_Pressure = "TPMDataToPC.PSI"; ResultsTags.O_TIDfromPLC = "TPMDataToPC.TIDfromPLC"; ResultsTags.I_TIDfromPC = "TPMDataToPC.TIDfromPC"; ResultsTags.I_Heartbeat = "TPMDataToPC.HB"; ResultsTags.I_ErrorMessage = "TPMDataToPC.ErrorMessageFromPC"; //Start the PLC comms threads. //VehicleInfo thread handles downloading MES data to PLC. ResultsFromPLC handles uploading read results from the PLC. mLog.Write("Created PLCComm to " + Properties.Settings.Default.PLCIP.ToString()); mPLC = new Linc.Communication.PLCComm(Properties.Settings.Default.PLCIP); mLog.Write("mPLC created"); mLog.Write("mPLC.HeartbeatTime = " + mPLC.HeartbeatTime.ToString()); //Add the tags mPLC.ResultsTags = ResultsTags; //Add event handlers for PLC events. mPLC.Message += new Linc.Communication.MessageEventHandler(mPLC_MessageReceived); mPLC.ReadResults += new Linc.Communication.NewReadResultsEventHandler(mPLC_ReadResultsReceived); //Start the PLC object. mLog.Write("mPLC Starting"); mPLC.Initialize(); mPLC.Start(); mLog.Write("mPLC Started"); //Test & Create the connection to the database. if (DataManager.TestConnection(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBName, Properties.Settings.Default.DBUserName, Properties.Settings.Default.DBPassword) == false) { mPLC.WriteErrorMessagetoPLC("TPM service failed to connect to database. Check MySQL is running on PC."); } else { //Connection is OK so connect. DataManager.Initialize(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBName, Properties.Settings.Default.DBUserName, Properties.Settings.Default.DBPassword); mLog.Write("Connection to database is good. Server = " + Properties.Settings.Default.DBServer + ", DBName=" + Properties.Settings.Default.DBName + ", User="******", DBPassword="******"FATAL ERROR DURING Initialize"); mLog.Write(ex.ToString()); throw new Exception("FATAL ERROR DURING Initialize " + ex.ToString()); //Raise an exception so the service will stop itself } }