I have a program which relies on data being sent through a serial port. I would like to monitor when data is sent through with SerialPort.ReadLine()
. My problem is that I use check boxes which freeze when checked because the SerialPort.ReadLine()
method blocks until it receives some data. I have also tried using ReadTimeout()
but it did not work; perhaps because I did not use it correctly.
I've posted my code for this event below. What I would like this method to do is to monitor my serial port when the checkbox is checked, and then stop monitoring the serial port when I un-check the box. My current situation with my code is that it freezes upon checking the box. All help is appreciated.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Text = "Listening...";
using (SerialPort arduino = new SerialPort())
{
arduino.BaudRate = 9600;
arduino.PortName = comboBox1.Text;
arduino.Open();
string pipe = arduino.ReadLine(); //loop here
if (pipe == "S")
{
//System.Diagnostics.Process.Start("shutdown", "/f /r /t 0");
System.Windows.Forms.Application.Exit();
}
else
{
checkBox1.Text = "Start";
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire