public void ProcessSerial(object sender, System.EventArgs e) { if (!_SerialPort.IsOpen) { return; } if ((DateTime.Now - _LastTimeAlive).TotalMilliseconds > 2500 || (DateTime.Now - _LastUpdateTime).TotalMilliseconds > 2500) { if (OnEvent != null) { OnEvent(this, Actions.TIMEOUT_OCCURED); } } bool sendData = false; try { if (_Message.Receive() > 0) { _LastTimeAlive = DateTime.Now; if (!_Message.IsCorrupt) { switch ((Actions)_Message.Action) { case Actions.UPDATE_SETTINGS: //VentValveOpen;AirPumpOn;PressureGlobalBar;ControlFromPC;PressureMilliBar if (_Message.DataLength == 6) { _LastUpdateTime = DateTime.Now; byte readval = 0; if (_Message.Read_byte(ref readval)) { VentValveOpen = readval != 0; if (_Message.Read_byte(ref readval)) { AirPumpOn = readval != 0; if (_Message.Read_byte(ref readval)) { if (!ControlFromPC) { //only read global set pressure PressureGlobalBar = readval; } if (OnEvent != null) { OnEvent(this, PressureGlobalBar != readval ? Actions.INCORRECT_PRESSURE : Actions.CORRECT_PRESSURE); } if (_Message.Read_byte(ref readval)) { IsPCControlStatusOK = (ControlFromPC == (readval != 0)); if (OnEvent != null && IsPCControlStatusOK) { OnEvent(this, readval == 1 ? Actions.PC_AQUIRE_CONTROL_SUCCESS : Actions.PC_RELEASE_CONTROL_SUCCESS); } short millibars = 0; if (_Message.Read_Int16(ref millibars)) { PressureMilliBar = (ushort)millibars; if (!AreValuesUpToDate) { AreValuesUpToDate = true; } if (OnEvent != null) { OnEvent(this, Actions.UPDATE_SETTINGS); } } } } } } } break; case Actions.PC_AQUIRE_CONTROL_SUCCESS: case Actions.PC_RELEASE_CONTROL_SUCCESS: if (OnEvent != null) { OnEvent(this, (Actions)_Message.Action); } break; } } } if ((DateTime.Now - _LastDataSendTime).TotalMilliseconds > 100) { _LastDataSendTime = DateTime.Now; if (!IsPCControlStatusOK) { _Message.BeginWrite(); _Message.Action = (byte)(ControlFromPC ? Actions.PC_AQUIRE_CONTROL : Actions.PC_RELEASE_CONTROL); _Message.Send(); sendData = true; } if (ControlFromPC) { _Message.BeginWrite(); _Message.Action = (byte)Actions.UPDATE_GLOBAL_PRESSURE; _Message.Write_byte(PressureGlobalBar); _Message.Send(); sendData = true; } if (!sendData) { _Message.BeginWrite(); _Message.Action = (byte)Actions.PING; _Message.Send(); } } } catch (Exception) { /*State not OK? Connection corrupted? */ if (OnEvent != null) { OnEvent(this, Actions.CONNECTION_CORRUPTED); } } }
/// <summary> /// Automatically check existing devices and see if they are a decompression chamber by trial and error /// </summary> /// <param name="maxTries"></param> /// <returns>true if found false if not</returns> private bool SearchForDecompressionDevice(int maxTries) { if (_SerialPort == null) { return false; } bool found = false; int tries = 0; Thread.Sleep(1000); while (!found && tries < maxTries) { foreach (string port in SerialPort.GetPortNames()) { if (_SerialPort.IsOpen) { try { _SerialPort.Close(); } catch (Exception) { /*We just want to disconnect and continue searching*/ } } try { _SerialPort.PortName = port; _SerialPort.Open(); Message message = new Message(_SerialPort); message.BeginWrite(); message.Action = (byte)Actions.CHECK_FOR_DECOMPRESSION_DEVICE; message.Write_byte((byte)MsgStruct.MS_DataBegin); message.Send(); Stopwatch watch = new Stopwatch(); watch.Start(); while (message.Receive() != 6 && watch.ElapsedMilliseconds < 150) { } if (message.DataLength == 1 && !message.IsCorrupt) { byte output = 0; if (message.Action == (byte)Actions.HERE_IS_A_DECOMPRESSION_DEVICE && message.Read_byte(ref output)) { if (output == (byte)MsgStruct.MS_DataEnd) { //found decompression device return true; } } } } catch (Exception) { continue; } } ++tries; Thread.Sleep(1); } return false; }
/// <summary> /// Automatically check existing devices and see if they are a decompression chamber by trial and error /// </summary> /// <param name="maxTries"></param> /// <returns>true if found false if not</returns> private bool SearchForDecompressionDevice(int maxTries) { if (_SerialPort == null) { return(false); } bool found = false; int tries = 0; Thread.Sleep(1000); while (!found && tries < maxTries) { foreach (string port in SerialPort.GetPortNames()) { if (_SerialPort.IsOpen) { try { _SerialPort.Close(); } catch (Exception) { /*We just want to disconnect and continue searching*/ } } try { _SerialPort.PortName = port; _SerialPort.Open(); Message message = new Message(_SerialPort); message.BeginWrite(); message.Action = (byte)Actions.CHECK_FOR_DECOMPRESSION_DEVICE; message.Write_byte((byte)MsgStruct.MS_DataBegin); message.Send(); Stopwatch watch = new Stopwatch(); watch.Start(); while (message.Receive() != 6 && watch.ElapsedMilliseconds < 150) { } if (message.DataLength == 1 && !message.IsCorrupt) { byte output = 0; if (message.Action == (byte)Actions.HERE_IS_A_DECOMPRESSION_DEVICE && message.Read_byte(ref output)) { if (output == (byte)MsgStruct.MS_DataEnd) { //found decompression device return(true); } } } } catch (Exception) { continue; } } ++tries; Thread.Sleep(1); } return(false); }