private void Serial_Uchwyt_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string txt = ""; try { txt = Serial_Uchwyt.ReadExisting(); } catch (Exception) { Port_Rozlacz(); MetroMessageBox.Show(this, "Wystąpił błąd połączenia szeregowego.", "Problem", MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (InvokeRequired) { Invoke((MethodInvoker) delegate { Port_OdebranaWiadomosc(txt); }); } else { Port_OdebranaWiadomosc(txt); } }
private void Port_Rozlacz() { Przycisk_Polacz.Enabled = true; Przycisk_Polacz.Text = "Połącz"; Etykieta_StanCzujnika.Text = "Brak połączenia"; Zasobnik_UstawIkone(0); Zegar_Laczenie.Stop(); Serial_Uchwyt.Close(); }
// Procedury private void Port_Polacz(bool cicho = false) { Port_Cicho = cicho; ManagementObjectCollection Urzadzenia = new ManagementObjectSearcher("Select * from Win32_SerialPort").Get(); ManagementObject SRM_Board = null; foreach (ManagementObject Urzadzenie in Urzadzenia) { if ((Urzadzenie["Description"].ToString().Contains("SmartRedMotion") || Urzadzenie["Description"].ToString().Contains("Silicon"))) //TODO { SRM_Board = Urzadzenie; break; } } if (SRM_Board == null) { if (!Port_Cicho) { MetroMessageBox.Show(this, "Nie znaleziono urządzenia SmartRedMotion.\r\n\r\nMożliwości:\r\n- urządzenie jest niepodłączone\r\n- sterowniki są niezainstalowane", "Problem", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return; } Przycisk_Polacz.Enabled = false; Przycisk_Polacz.Text = "Połącz"; Etykieta_StanCzujnika.Text = "Łączenie..."; Zasobnik_UstawIkone(0); try { Serial_Uchwyt.PortName = SRM_Board["DeviceID"].ToString(); Serial_Uchwyt.Open(); Zegar_Laczenie.Start(); } catch (Exception) { Port_Rozlacz(); if (!Port_Cicho) { MetroMessageBox.Show(this, "Wystąpił błąd połączenia z urządzeniem.", "Problem", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }