Serial port read in Thread error

  • Thread starter Thread starter Giorgi.Jambazishvili
  • Start date Start date
G

Giorgi.Jambazishvili

Guest
Hello

I try to read some information from my Arduino Uno device. I have class and peace of code, which runs inside a thread.

in port reader class I have read method with code:

SerialPort.Open(); //Port is confugered right, and it works without thread well
while (SerialPort.IsOpen)
{
stream.Add (SerialPort.ReadLine()); // stream is -> List<string> stream = new List<string>();
}

and I have button which closes the public serial port

{
portClass.SerialPort.Close();
foreach (var item in portClass.stream)
listBox1.items.add (item.toString());
}

and I have thread in which read operation is running

{
Thread t1 = new Thread (portClass.read);
t1.Start();
}

everything works well, when I press start button, it starts running and reading the input values, then when I press the end button Ive got my values in my listBox1 and this exception, which I dont know why.

b98ebb4568b7abf2241bb4270a6137a9._.png

and it points to the peace of code above in read method:

stream.Add (SerialPort.ReadLine());

yes, thats right that I requested to stop reading, since Im closing a port, and why this exception occurs at all, and is there any possibilities to avoid this problem.

also, im interested in whether what is better choice to read serial port and maintain UI responsive. Ive tried Application.DoEvents(), but it doesnt works in WPF and I dont like using things which dont know (I mean app.doEvent()).

Continue reading...
 
Back
Top