/// <summary> /// States the fault. /// </summary> /// <param name="event">The event.</param> private void State_Fault(State_Event @event) { switch (@event) { case State_Event.Enter: delay = 0; this.SetPlcData(string.Empty); if (this.tcpClient != null) { this.tcpClient.Connected -= this.TcpClient_Connected; this.tcpClient.Disconnected -= this.TcpClient_Disconnected; this.tcpClient.DataAvailable -= this.TcpClient_DataAvailable; } this.tcpClient?.Disconnect(); this.tcpClient?.Dispose(); this.tcpClient = null; this.UpdateStatus(GuiState.FAULT, Invariant($"{this.Name} is in fault.")); break; case State_Event.Poll: delay++; if (delay > 10) { this.StateTrans(PlcState.State_GetPort, this.State_GetPort); } break; default: break; } }
/// <summary> /// States the online. /// </summary> /// <param name="event">The event.</param> private void State_Online(State_Event @event) { switch (@event) { case State_Event.Enter: this.UpdateStatus(GuiState.ONLINE, "Connected to PLC"); this.plcInputEventData.MarkDirty(); this.plcOutputEventData.MarkDirty(); this.plcSystemEventData.MarkDirty(); this.SetSubscriptions(); break; case State_Event.NewData: this.ProcessInputData(); break; case State_Event.Poll: this.missedHeartbeats++; if (this.missedHeartbeats > 10) { this.StateTrans(PlcState.State_Fault, this.State_Fault); } break; case State_Event.Disconnect: this.StateTrans(PlcState.State_Fault, this.State_Fault); break; default: break; } }
/// <summary> /// States the wait for port information. /// </summary> /// <param name="event">The event.</param> private void State_WaitForPortInformation(State_Event @event) { switch (@event) { case State_Event.Enter: delay = 0; this.UpdateStatus(GuiState.FAULT, "Communications port for the plc is not configured. Please setup the Plc"); break; case State_Event.Poll: delay++; if (delay > 5) { this.StateTrans(PlcState.State_GetPort, this.State_GetPort); } break; default: break; } }
/// <summary> /// States the connect. /// </summary> /// <param name="event">The event.</param> private void State_Connect(State_Event @event) { switch (@event) { case State_Event.Enter: delay = 0; this.UpdateStatus(GuiState.CONNECTING, "Attempting to connect to the plc."); this.tcpClient = new ClientTCP("PlcTcpClient", this.deviceLogger, this.ipAddress, this.port, this.plcTxFifo); this.tcpClient.Connected += this.TcpClient_Connected; this.tcpClient.Disconnected += this.TcpClient_Disconnected; this.tcpClient.DataAvailable += this.TcpClient_DataAvailable; this.tcpClient.Connect(); break; case State_Event.Poll: delay++; if (delay > 5) { this.StateTrans(PlcState.State_Fault, this.State_Fault); } break; case State_Event.Connected: this.StateTrans(PlcState.State_Online, this.State_Online); break; case State_Event.Disconnect: this.StateTrans(PlcState.State_Fault, this.State_Fault); break; default: break; } }
/// <summary> /// States the get port. /// </summary> /// <param name="event">The event.</param> private void State_GetPort(State_Event @event) { switch (@event) { case State_Event.Enter: this.UpdateStatus(GuiState.CONNECTING, Properties.Resources.GET_PLC_PORT_INFO); break; case State_Event.Poll: if (this.ipAddress != this.defaultIPAddress) { this.StateTrans(PlcState.State_Connect, this.State_Connect); } else { this.StateTrans(PlcState.State_WaitForPortInformation, this.State_WaitForPortInformation); } break; default: break; } }