I have a MySerialPort class/object accessed from FormGUI. After creating a MySerialPort object, I want to open it, and keep receiving data continuously. The data will be stored and managed in the object's data buffer. FormGUI's memoEdit will show the received codes from the MySerialPort object's buffer.
How can I use "new Thread()" with the [port].ReadExisting method?
using System;
using System.IO.Ports;
using System.Threading;
class MySerialPort
{
public SerialPort CreatePort(string portName, int portSpeed, int portParity, int portDataSize, int portStopBits)
{
// fixed values while testing
var port = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
return port;
}
public void OpenPort(SerialPort port)
{
port.Open();
new Thread(() => port.ReadExisting).Start();
while (true)
{
// Send to buffer
// Maybe some break condition
}
}