How to continuously read from serial COM port

  • Thread starter Thread starter YigalB
  • Start date Start date
Y

YigalB

Guest
I am reading information from Arduino into a C# program that supposed to parse the data, analyze it and execute tasks on the host (PC). At this stage I need only to read from the serial port. I can get the available ports, set the baud rate and read data. The problem is that I can only read it into a form's text box. This was good for debug, but now I need to parse the data. so I need it as a list/array of strings (lines).

My current code is:

while (true) {
oneLine = myPort.ReadLine();
this.Invoke(new EventHandler(display_data_Event));

// TBD: add analysis of data from port
// TBD: execute according to data from Arduino
}
The handler is:

private void display_data_Event(object sender, EventArgs e) {
string curr_time = DateTime.Now.ToString("h:mm:ss tt");
port_in_TextBox.AppendText(curr_time + " " + oneLine + "\n");
}


How can I use the "one line" string only when the event occurs, instead of the "while true"? I tried calling functions from the handler - I guess it failed because it is another thread. So perhaps the problem is how to share the string from one thread to another.

Continue reading...
 

Similar threads

Back
Top