private void CutTest_BTN_Click(object sender, EventArgs e) { try { CutTest_BTN.Enabled = false; CutTest_BTN.Update(); MainSerialPort.WriteLine("C"); stopwatch.Reset(); stopwatch.Start(); while (stopwatch.ElapsedMilliseconds < 1000) { } stopwatch.Stop(); Application.DoEvents(); if (!CutTest_BTN.IsDisposed) { CutTest_BTN.Enabled = true; } } catch (Exception) { MessageBox.Show("Error - connection problem"); CutTest_BTN.Enabled = true; } }
private void Connect_BTN_Click(object sender, EventArgs e) { if (Connect_BTN.Text.Equals("Connect")) { try { Connect_BTN.Enabled = false; MainSerialPort.BaudRate = 9600; MainSerialPort.PortName = comboBox1.Text; MainSerialPort.NewLine = "\r\n"; MainSerialPort.DtrEnable = true; stopwatch.Reset(); stopwatch.Start(); MainSerialPort.Open(); while (stopwatch.ElapsedMilliseconds < 1000) { } stopwatch.Stop(); //Thread.Sleep(1000); MessageBox.Show("Connected"); FunctionsEnableConnection(); Connect_BTN.Enabled = true; } catch (Exception) { MessageBox.Show("Error - connection problem!"); Connect_BTN.Enabled = true; } } else if (Connect_BTN.Text.Equals("Disconnect")) { try { Connect_BTN.Enabled = false; MainSerialPort.WriteLine("E"); MainSerialPort.DiscardInBuffer(); MainSerialPort.DiscardOutBuffer(); MainSerialPort.BaseStream.Flush(); stopwatch.Reset(); stopwatch.Start(); while (stopwatch.ElapsedMilliseconds < 1000) { } stopwatch.Stop(); Connect_BTN.Enabled = true; } catch (Exception) { Connect_BTN.Enabled = true; } MainSerialPort.Close(); FunctionsBase(); } }
private void Read_BTN_Click(object sender, EventArgs e) { try { Read_BTN.Enabled = false; MainSerialPort.WriteLine("R"); stopwatch.Reset(); stopwatch.Start(); while (stopwatch.ElapsedMilliseconds < 1000) { } stopwatch.Stop(); Read_BTN.Enabled = true; } catch (Exception) { MessageBox.Show("Error - connection problem"); Read_BTN.Enabled = true; } }