private void OpenConnection() { string selectedPort = comboBoxPorts.GetItemText(comboBoxPorts.SelectedItem); int baudRate = Convert.ToInt32(comboBoxBaudRate.Text); Class1.InitSerialConnection(selectedPort, baudRate); if (!Class1.port.IsOpen) { listMessages.Items.Add("Tentativo di connessione"); Class1.port.Open(); string response = Class1.port.ReadLine(); if (response == "start\r") { listMessages.Items.Add("Connesso"); delegateConnection = CloseConnection; comboBoxPorts.Enabled = false; comboBoxBaudRate.Enabled = false; buttonSend.Enabled = true; button1.Text = "Disconnect"; button5.Enabled = false; button4.Enabled = false; Class1.port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived); } else { listMessages.Items.Add("Connection error: " + response); Class1.port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived); CloseConnection(); } } }
public void GetAvailablePorts() { ports = SerialPort.GetPortNames(); comboBoxPorts.Items.Clear(); comboBoxPorts.Items.AddRange(ports); delegateConnection = OpenConnection; }
private void CloseConnection() { Class1.port.Close(); if (!Class1.port.IsOpen) { listMessages.Items.Add("Disconnesso"); delegateConnection = OpenConnection; comboBoxPorts.Enabled = true; comboBoxBaudRate.Enabled = true; buttonSend.Enabled = false; button1.Text = "Connect"; button5.Enabled = true; button4.Enabled = true; } }