/// <summary> /// This functions handles opening and closing of serial connections /// </summary> /// <param name="appState">reference to a number of variables that need to be maintained</param> /// <param name="isUno">If the board is an Uno we need to set specific options</param> /// <param name="comPort">The name of the com port to initialize</param> /// <param name="toolStripStatus">A reference to the toolstrip so we can pass status value back</param> /// <returns>true if successful. false otherwise.</returns> public bool SerialInit(ref AppState appState, bool isUno, string comPort, ref ToolStripStatusLabel toolStripStatus) { if (serialComm.IsOpen) //if serial connection is active { #region SerialDisconnectLogic try { return serialComm.CloseConnection(ref appState); } catch (InvalidCastException e) // if exception occurs { return false; } #endregion } else // if serial connection is not currently open { if (!string.IsNullOrEmpty(comPort)) // make sure COM port is selected once more (better safe then sorry) { #region SerialConnectLogic try { var baudRate = Constants.BAUDRATE * Constants.BAUDRATE_MULTIPLIER; if (isUno) // if We use Arduino Uno (or similar snail) { baudRate = Constants.BAUDRATE * Constants.UNO_BAUDRATE_MULTIPLIER; // Set baud rate to "Uno" speed - because it's slow as F#@$ } var isOpen = serialComm.OpenConnection(comPort, ref appState, baudRate, dedDevice_DataReceived); if (isOpen) // if succeded { initVars(); //initiallize the Falcon variables return true; } else { return false; } } catch (InvalidCastException e) //if try fails. { return false; } #endregion } else // no device is selected { toolStripStatus.Text = "No Device Selected"; toolStripStatus.BackColor = Color.Orange; return false; } } }