private void ExecuteS7ConnectionTest(int identity) { //get tested connection S7ConnectionModel connection = GetS7ConnectionModel(identity); //create connection testing class object S7ConnectionChecker checker = new S7ConnectionChecker(); //check connection (bool success, string error) = checker.CheckConnection(connection.IPaddress, (short)connection.Rack, (short)connection.Slot); //in case of failure if (!success) { //write event to DB SystemEventCreator eventCreator = new SystemEventCreator(_realmProvider); eventCreator.SaveNewEvent(SystemEventTypeEnum.ConnectionTest, $"Connection test to S7 CPU with IP: {connection.IPaddress}, rack: {connection.Rack}, slot: {connection.Slot}; failed."); MessageBox.Show($"Connection test failed. {error}.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else //success { SuccessConnectionDataReaded(checker); //write event to DB SystemEventCreator eventCreator = new SystemEventCreator(_realmProvider); eventCreator.SaveNewEvent(SystemEventTypeEnum.ConnectionTest, $"Connection test to S7 CPU with IP: {connection.IPaddress}, rack: {connection.Rack}, slot: {connection.Slot}; successfull."); } }
private void CheckingIfConnectionIsOK() { //find incorrect connections foreach (var item in _activeS7Connections) { bool connectionOK; try { S7ConnectionChecker checker = new S7ConnectionChecker(); (bool ok, string error) = checker.CheckConnection($"{item.FirstOctet}.{item.SecondOctet}.{item.ThirdOctet}.{item.FourthOctet}", (short)item.Rack, (short)item.Slot); if (!ok) { SystemEventCreator creator = new SystemEventCreator(_realmProvider); creator.SaveNewEvent(SystemEventTypeEnum.S7PLCconnectionFailure, $"Failed to connect to PLC with IP address: " + $"{item.FirstOctet}.{item.SecondOctet}.{item.ThirdOctet}.{item.FourthOctet}, Rack: {item.Rack}, Slot: {item.Slot}; during runtime."); } connectionOK = ok; } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error($"Error while trying to check communication status. Error: {ex.Message}."); connectionOK = false; } _plcConnectionOK.Add(connectionOK); } }
private void SuccessConnectionDataReaded(S7ConnectionChecker checker) { string cpuInfo = checker.GetCPUInfo(); string cpuDateAndTime = checker.GetCPUDateAndTime(); string message = $"Connection test successfull.\n\nCPU info:\n{cpuInfo}\n\nCPU date and time:\n{cpuDateAndTime}."; MessageBox.Show(message, "Information", MessageBoxButton.OK, MessageBoxImage.Information); }