private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { // Grab the COM name from the list via regex var comchosen = e.ClickedItem.Text; var match = Regex.Match(comchosen, @"[a-zA-Z\ ]+\((COM[0-9]+)\)", RegexOptions.IgnoreCase); if (comchosen == "Show stats" || comchosen == "Exit") { return; } // Uh oh! if (!match.Success) { MessageBox.Show( PopupNotActualSerial, PopupFailedToOpen, MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } // Temporary variable for the checked item. bool shouldCheck = !((ToolStripMenuItem)e.ClickedItem).Checked; // Disable the rest of the checkmarks on the items. foreach (ToolStripItem t in trayMenu.Items) { ((ToolStripMenuItem)t).Checked = false; } // Change the selected item's checkmark ((ToolStripMenuItem)e.ClickedItem).Checked = shouldCheck; _sendmessage = shouldCheck; // Time to connect with serial! try { lock (serial) { if (serial.IsOpen) { serial.Close(); } serial.PortName = match.Groups[1].Value; stage = SerialStage.Handshake; stagepart = 0; if (shouldCheck) { serial.Open(); } } } catch (System.UnauthorizedAccessException) { MessageBox.Show( PopupSerialInUse, PopupFailedToOpen, MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } catch (System.IO.IOException) { MessageBox.Show( PopupSerialFailed, PopupFailedToOpen, MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } }
private void SerialComms() { while (_continueComms) { // If serial is started, don't try to use it. // TODO: lock the serial object when doing comms so it doesnt get interrupted mid-write. if (!serial.IsOpen) { Thread.Sleep(1000); // just so CPU usage doesn't spike. continue; } lock (serial) { if (stage == SerialStage.Handshake) { // First, send a message to the device if (stagepart == 0) { serial.Write("010"); stagepart = 1; } // Next, wait for a response. If one isn't recieved in 5 seconds, restart. else if (stagepart == 1) { var check = Task.Run(() => SerialResponse("101")); if (check.Wait(TimeSpan.FromSeconds(5))) { stage = SerialStage.ComputerParts; stagepart = 0; } else { stage = SerialStage.Handshake; stagepart = 0; } } } else if (stage == SerialStage.ComputerParts) { if (stagepart == 0) { lock (_compMutex) { serial.Write($"{cpuName}|{gpuName}|{ramTotal}GB|"); } stagepart = 1; } else if (stagepart == 1) { var check = Task.Run(() => SerialResponse("222")); if (check.Wait(TimeSpan.FromSeconds(5))) { stage = SerialStage.ContinuousStats; stagepart = 0; } else { stage = SerialStage.Handshake; stagepart = 0; } } } else if (stage == SerialStage.ContinuousStats) { if (stagepart == 0) { lock (_compMutex) { serial.Write($"{cpuFreq}|{cpuTemp}|{cpuLoad}|{ramUsed}|{gpuTemp}|" + $"{gpuCoreClock}|{gpuCoreLoad}|{gpuVramClock}|{gpuVramLoad}|"); } stagepart = 1; } else if (stagepart == 1) { // wait for response "333" for 5 seconds, if nothing then restart var check = Task.Run(() => SerialResponse("333")); if (check.Wait(TimeSpan.FromSeconds(5))) { stage = SerialStage.ContinuousStats; stagepart = 0; } else { stage = SerialStage.Handshake; stagepart = 0; } } } } // Sleep for a moment, we don't need to spam the serial pipe. Thread.Sleep(50); } }