public Task <string[]> GetPorts() { return(mDipatcher.DoFunc(() => { List <string> unusedPorts = new List <string>(); string[] ports = SerialPort.GetPortNames(); for (int i = 0; i < ports.Length; i++) { bool unused = true; foreach (BotMain bot in BotSet.AllBots) { IBotConnection con = bot.GetConnection(); if ((con is ComConnection) && (((ComConnection)con).GetPortName() == ports[i])) { unused = false; break; } } if (unused) { unusedPorts.Add(ports[i]); } } return unusedPorts.ToArray(); })); }
private void MConnection_StateChanged(IBotConnection sender, BotConnectionState state) { if (!mRunning) { return; } if ((state == BotConnectionState.Stopped) || (state == BotConnectionState.Failed)) { ProtocolError((mConnection.LastError != null) ? mConnection.LastError : "Connection failed."); } else if (state == BotConnectionState.Started) { mConnection.SendMessage(Encoding.ASCII.GetBytes("INIT")); mState = ProtocolState.InitSent; UpdateTimeout(); } }
public void SetConnection(IBotConnection connection) { if (mRunning) { throw new InvalidOperationException(); } if (mConnection != null) { mConnection.StateChanged -= MConnection_StateChanged; mConnection.Message -= MConnection_Message; } mConnection = connection; if (mConnection != null) { mConnection.StateChanged += MConnection_StateChanged; mConnection.Message += MConnection_Message; } }
private void MConnection_Message(IBotConnection sender, byte[] message) { switch (mState) { case ProtocolState.InitSent: if (Encoding.ASCII.GetString(message) == "REDY") { mState = ProtocolState.Connected; UpdateTimeout(); SetAvailable(true); } else { ProtocolError("Invalid response from bot."); } break; case ProtocolState.Connected: string text = Encoding.ASCII.GetString(message); int time; if (int.TryParse(text, out time)) { if (mTimerMain != null) { mTimerMain.SetTime(time); } } else { ProtocolError("Invalid message from bot."); } break; default: ProtocolError("Unexpected response from bot."); break; } }