private void ConnectButton_Click(object sender, EventArgs e) { PortSettings(); try { //sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); //open serial port sp.Open(); //set read time out to 500 ms // sp.ReadTimeout = 100; } catch (System.Exception ex) { MessageBox.Show(ex.Message); } if (sp.IsOpen) { MessageBox.Show("serial port has open"); connectSp = true; ConnectButton.Hide(); DisconnectButton.Show(); RefreshButton.Hide(); } else { MessageBox.Show("faild to open serial port"); connectSp = false; ConnectButton.Show(); DisconnectButton.Hide(); RefreshButton.Show(); } }
private void DisconnectButton_Click(object sender, EventArgs e) { try { connectSp = false; sp.Close(); ConnectButton.Show(); DisconnectButton.Hide(); RefreshButton.Show(); } catch (System.Exception ex) { connectSp = true; MessageBox.Show("faild to close serial port"); ConnectButton.Hide(); DisconnectButton.Show(); RefreshButton.Hide(); } }