/// <summary> /// This the the Heavy lifter this is the serial interrup function /// Function recives incoming data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void dedDevice_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; byte[] blankByte = new byte[1]; if (!appState.IsClosing)// if we are not in a closing state to your thing { #region DataProcessingLogic int buffersize = sp.BytesToRead; SpinWait sw = new SpinWait(); //Debug.Print("-----"); //Debug.Print("buff: " + buffersize.ToString()); if (buffersize == 0) { return; } char[] s = new char[buffersize]; for (short i = 0; i < s.Length; i++) { s[i] = (char)sp.ReadByte(); } byte[] ResponseByte = new byte[1]; bool PowerOn; if (appState.BMS432) { PowerOn = BMSreader.IsFalconRunning; } else { PowerOn = DeduinoHelpers.CheckLight(HsiBits.Flying, BMSdata); // test isFlying bit - if Falcon is in 3D world - this is true } char mode; // define "mode" variable ushort LineNum; #region mode_logic if (s.Length == 1) { if (char.IsNumber(s[0])) { try { //Debug.Print("Number only"); mode = appState.SerialBuffer; LineNum = ushort.Parse(s[0].ToString()); } catch { return; } } else { try { //Debug.Print("letter only"); mode = s[0]; // mode is the first character from the serial string appState.SerialBuffer = mode; LineNum = 255; FalconUpdate(); } catch { return; } } } else { try { mode = s[0]; // mode is the first character from the serial string if (char.IsNumber(s[1])) { LineNum = ushort.Parse(s[1].ToString()); //Debug.Print("two bytes - with number"); } else { //Debug.Print("two bytes - no number"); LineNum = 255; } } catch { return; } } //Debug.Print("buff: " + new string (s)); //Debug.Print("Mode: " + mode); //Debug.Print("Line: " + LineNum); if (LineNum == 0) { FalconUpdate(); } #endregion switch (mode) // Get the party started. { #region DoWork case 'R': // Recive RDY call from arduino //Debug.Print("buff: " + buffersize.ToString()); serialComm.DiscardInBuffer(); sendLine("G", 1); // if caught sent back GO command serialComm.DiscardOutBuffer(); //MessageBox.Show("R"); break; // exit the interrupt case 'U': // Recive UPDATE commant from Arduino - This is not used, but retained for bawards compatibility sendLine("k", 2); // if caught sent back GO command break; // exit the interrupt case 'D': //Revice DED request from Arduino - requers reciving D and Number of line #region DED_legacy if (PowerOn && LineNum >= 0 && LineNum < 5) { if (BMSdata.DEDLines != null) { sendLine(NormalizeLine(BMSdata.DEDLines[LineNum], BMSdata.Invert[LineNum]).ToString().PadRight(25, ' '), 25); } else { sendLine(" ".PadRight(25, ' '), 25); } } break; #endregion case 'd': //Revice DED request from Arduino - requers reciving D and Number of line #region DED appState.SerialBuffer = 'd'; if (LineNum == 255) { break; } if (PowerOn) { if (BMSdata.DEDLines != null) { sendBytes(NormalizeLine(BMSdata.DEDLines[LineNum], BMSdata.Invert[LineNum]), 24); } else { sendLine(" ".PadRight(24, ' '), 24); } } else { if (LineNum == 2) { sendLine("FALCON NOT READY...".PadRight(24, ' '), 24); } else { sendLine(" ".PadRight(24, ' '), 24); } } break; #endregion case 'P': #region PFL_Legacy if (PowerOn && LineNum >= 0 && LineNum < 5) { if (BMSdata.PFLLines != null) { sendLine(NormalizeLine(BMSdata.PFLLines[LineNum], BMSdata.PFLInvert[LineNum]).ToString().PadRight(25, ' '), 25); } else { sendLine(" ".PadRight(25, ' '), 25); } } break; #endregion case 'p': #region PFL appState.SerialBuffer = 'p'; if (LineNum == 255) { break; } if (PowerOn) { if (BMSdata.PFLLines != null) { sendBytes(NormalizeLine(BMSdata.PFLLines[LineNum], BMSdata.PFLInvert[LineNum]), 24); } else { sendLine(" ".PadRight(24, ' '), 24); } } else { if (LineNum == 2) { sendLine("FALCON NOT READY...".PadRight(24, ' '), 24); } else { sendLine(" ".PadRight(24, ' '), 24); } } break; #endregion case 'M': #region CMDS appState.SerialBuffer = 'M'; if (LineNum == 255) { break; } if (PowerOn) { sendLine(cmdsMakeLine((short)LineNum).PadRight(24, ' '), 24); } else { sendLine(" ".PadRight(24, ' '), 24); } break; #endregion case 'F': #region FuelFlow if (PowerOn) { if (BMSdata.fuelFlow2 != null) { sendLine(FuelFlowConvert((BMSdata.fuelFlow + BMSdata.fuelFlow2)).PadLeft(5, '0'), 5); } else { sendLine(FuelFlowConvert(BMSdata.fuelFlow).PadLeft(5, '0'), 5); } } else { sendLine("0".PadRight(5, '0'), 5); } break; #endregion case 'A': #region Indexers blankByte[0] = (byte)0; if (PowerOn) { sendBytes(MakeAoaLight(), 1); } else { sendBytes(blankByte, 1); } break; #endregion case 'C': #region CautionPanel blankByte[0] = (byte)0; if (PowerOn) { sendBytes(MakeCautionPanel(appState.CautionPanelVer), 5); // types are "old" and "new", default is "new" } else { sendBytes(BitConverter.GetBytes(uint.MinValue), 4); sendBytes(blankByte, 1); } break; #endregion case 'G': #region glareshield blankByte[0] = (byte)0; if (PowerOn) { sendBytes(MakeGlareShield(), 2); } else { // send two blank bytes sendBytes(BitConverter.GetBytes(uint.MinValue), 2); } break; #endregion case 'T': #region TWP sendBytes(blankByte, 1); break; #endregion case 'E': #region Engine if (PowerOn) { // senging out engine data by guage - top to bottom sendBytes(BitConverter.GetBytes(BMSdata.oilPressure), 1); sendBytes(BitConverter.GetBytes(BMSdata.nozzlePos), 1); sendBytes(BitConverter.GetBytes(BMSdata.rpm), 1); sendBytes(BitConverter.GetBytes(BMSdata.ftit), 2); } else { sendBytes(BitConverter.GetBytes(uint.MinValue), 5); } break; #endregion case 'S': #region Speedbreaks if (PowerOn) { sendBytes(MakeSpeedbreaks(), 1); } else { sendBytes(BitConverter.GetBytes('1'), 1); // send inop } break; #endregion } #endregion #endregion //dedDevice.DiscardInBuffer(); //dedDevice.DiscardOutBuffer(); } }