• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

On C# - How to capture the Serial Port received data to Global Variable?

Hi Guys.
I've using serialPort and written a small method to read the SerialPort. The SerialPort being read!  I need to separate the string the get Particles Counters. The devices send the string in this format: ?01,00000,00000,00000,n/a,n/a I try to capture the string to separate in the commas, but unfortunately, so capture the end of the string or "n/a". How to capture the entire string to global variable and treat this variable in another method?
Thanks in advance
My method:
private void serialPort1_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
	if(serialPort1.DataBits==8)
    {
    RxString=serialPort1.ReadExisting();
    this.Invoke(new EventHandler(DisplayText));
    }
}

private void DisplayText(object sender,EventArgs e)
{
	textBox1.AppendText(RxString);
}

Open in new window

0
ocaccy
Asked:
ocaccy
1 Solution
 
Minh Võ CôngCommented:
In serialPort1_DataReceived function you can implement like:

void loop(){
  if(Serial.available() > 0){
    int i = 0;
    unsigned char data = 0;
     while(Serial.available() > 0){
       data = Serial.read();
       if(i < buffersize){
        buffer[i] = data; //put all data into buffer until if fills
        i++;
       }
     }
     for(int j = 0; j < i; j++){
       Serial.print(buffer[j]); //print buffer characters
     }
  }
  //delay(10);
}

Open in new window

0
 
ocaccyAuthor Commented:
Thank you, this is the Best Solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now