Serial Port Data Communicatio

Apr 25 2014 2:03 PM

Hi I want to display updated data from the same serial port to four different forms at same time to plot online graph to these four windows ,I wrote the code for single window as follow

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)




{





string s = serialPort1.ReadExisting();




get1 = s.ToCharArray();




System.IO.StreamWriter file3 = new System.IO.StreamWriter("d:\\" + MTIME1.Text + ".csv", true);




file3.Write(s);


file3.Close();



for (int i = 0; i < s.Length; i++)




{


gets[0] = get1[i];




if (flag != 0)




{



switch (gets[0])




{



case space:




count1 = 0;


count++;



break;


case enter:






for (int l = 0; l < 10; l++)




{


m[l] = (c[0, l]);


}



s1 = new string(m);


for (int l = 0; l < 10; l++)




{


m[l] = (c[1, l]);


}



s2 = new string(m);


for (int l = 0; l < 10; l++)




{


m[l] = (c[2, l]);


}



s3 = new string(m);








for (int l = 0; l < 10; l++)




{


m[l] = (c[3, l]);


}



s4 = new string(m);








for (int h = 0; h < 20; h++)




{



for (int z = 0; z < 20; z++)




{



c[h, z] = Convert.ToChar(0);




}


}



d1 = Convert.ToDecimal(s1);


d2 = Convert.ToDecimal(s2);


d3 = Convert.ToDecimal(s3);


d4 = Convert.ToDecimal(s4);




cntr++;


time.Enqueue(d1);


ch1.Enqueue(d2);


ch2.Enqueue(d3);


ch3.Enqueue(d4);



if (cntr == mestim)




{



t1 = Convert.ToDouble(d2);


t2 = Convert.ToDouble(d3);


t3 = Convert.ToDouble(d4);




}



count = 0;


count1 = 0;




break;


case qm:




flag = 0;





double g1 = Convert.ToDouble(d2);


double f1 = Convert.ToDouble(d3);


double h1 = Convert.ToDouble(d4);






result1 = -(System.Math.Log10(g1 / t1));


result2 = (System.Math.Log10(f1 / t2));


result3 = (System.Math.Log10(h1 / t3));




ntu1 = f1 - t2;


ntu2 = h1 - t3;



matr[inct] = result1;


matr1[inct] = result2;


matr2[inct] = result3;


matr3[inct] = ntu1;


matr4[inct] = ntu2;


inct++;


rpt--;


PLAG = 1;


cntr = 0;



for (int h = 0; h < 20; h++)




{



for (int z = 0; z < 20; z++)




{



c[h, z] = Convert.ToChar(0);




}


}




c1 = c1 + result1;


c2 = c2 + result2;


c3 = c3 + result3;


c4 = c4 + ntu1;


c5 = c5 + ntu2;








break;


default:


if (gets[0] == '1' || gets[0] == '2' || gets[0] == '3' || gets[0] == '4' || gets[0] == '5' || gets[0] == '6' || gets[0] == '7' || gets[0] == '8' || gets[0] == '9' || gets[0] == '0')




{


c[count, count1] = gets[0];


count1++;


}



break;




}



}



else




{



if (gets[0] == 'S')




{



//flag = 1;




flag = 1;



//int length = time.Count;


//for (int q = 0; q < length;q++ )


//{


// time.Dequeue();


// ch1.Dequeue();


// ch2.Dequeue();


// ch3.Dequeue();


//}


//time.Clear();


//time.Clear();




time.Clear();


ch1.Clear();


ch2.Clear();


ch3.Clear();



//time.Dequeue();


//System.IO.StreamWriter file2 = new System.IO.StreamWriter("e:\\time.csv", true);


//file2.Write(time);


//file2.Close();


//System.IO.StreamWriter file3 = new System.IO.StreamWriter("e:\\ch1.csv", true);


//file3.Write(ch1);


//file3.Close();


//System.IO.StreamWriter file4 = new System.IO.StreamWriter("e:\\ch2.csv", true);


//file4.Write(ch2);


//file4.Close();







//MessageBox.Show("hai");




}


}



//button2.PerformClick();


//}


//repeat--;


//txtrpt.Text = repeat.ToString();




}


}



private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)




{





string s = serialPort1.ReadExisting();




get1 = s.ToCharArray();




System.IO.StreamWriter file3 = new System.IO.StreamWriter("d:\\" + MTIME1.Text + ".csv", true);




file3.Write(s);


file3.Close();



for (int i = 0; i < s.Length; i++)




{


gets[0] = get1[i];




if (flag != 0)




{



switch (gets[0])




{



case space:




count1 = 0;


count++;



break;


case enter:






for (int l = 0; l < 10; l++)




{


m[l] = (c[0, l]);


}



s1 = new string(m);


for (int l = 0; l < 10; l++)




{


m[l] = (c[1, l]);


}



s2 = new string(m);


for (int l = 0; l < 10; l++)




{


m[l] = (c[2, l]);


}



s3 = new string(m);








for (int l = 0; l < 10; l++)




{


m[l] = (c[3, l]);


}



s4 = new string(m);








for (int h = 0; h < 20; h++)




{



for (int z = 0; z < 20; z++)




{



c[h, z] = Convert.ToChar(0);




}


}



d1 = Convert.ToDecimal(s1);


d2 = Convert.ToDecimal(s2);


d3 = Convert.ToDecimal(s3);


d4 = Convert.ToDecimal(s4);




cntr++;


time.Enqueue(d1);


ch1.Enqueue(d2);


ch2.Enqueue(d3);


ch3.Enqueue(d4);



if (cntr == mestim)




{



t1 = Convert.ToDouble(d2);


t2 = Convert.ToDouble(d3);


t3 = Convert.ToDouble(d4);




}



count = 0;


count1 = 0;




break;


case qm:




flag = 0;





double g1 = Convert.ToDouble(d2);


double f1 = Convert.ToDouble(d3);


double h1 = Convert.ToDouble(d4);






result1 = -(System.Math.Log10(g1 / t1));


result2 = (System.Math.Log10(f1 / t2));


result3 = (System.Math.Log10(h1 / t3));




ntu1 = f1 - t2;


ntu2 = h1 - t3;



matr[inct] = result1;


matr1[inct] = result2;


matr2[inct] = result3;


matr3[inct] = ntu1;


matr4[inct] = ntu2;


inct++;


rpt--;


PLAG = 1;


cntr = 0;



for (int h = 0; h < 20; h++)




{



for (int z = 0; z < 20; z++)




{



c[h, z] = Convert.ToChar(0);




}


}




c1 = c1 + result1;


c2 = c2 + result2;


c3 = c3 + result3;


c4 = c4 + ntu1;


c5 = c5 + ntu2;








break;


default:


if (gets[0] == '1' || gets[0] == '2' || gets[0] == '3' || gets[0] == '4' || gets[0] == '5' || gets[0] == '6' || gets[0] == '7' || gets[0] == '8' || gets[0] == '9' || gets[0] == '0')




{


c[count, count1] = gets[0];


count1++;


}



break;




}



}



else




{



if (gets[0] == 'S')




{



//flag = 1;




flag = 1;



//int length = time.Count;


//for (int q = 0; q < length;q++ )


//{


// time.Dequeue();


// ch1.Dequeue();


// ch2.Dequeue();


// ch3.Dequeue();


//}


//time.Clear();


//time.Clear();




time.Clear();


ch1.Clear();


ch2.Clear();


ch3.Clear();



//time.Dequeue();


//System.IO.StreamWriter file2 = new System.IO.StreamWriter("e:\\time.csv", true);


//file2.Write(time);


//file2.Close();


//System.IO.StreamWriter file3 = new System.IO.StreamWriter("e:\\ch1.csv", true);


//file3.Write(ch1);


//file3.Close();


//System.IO.StreamWriter file4 = new System.IO.StreamWriter("e:\\ch2.csv", true);


//file4.Write(ch2);


//file4.Close();







//MessageBox.Show("hai");




}


}



//button2.PerformClick();


//}


//repeat--;


//txtrpt.Text = repeat.ToString();




}


}


How I can do this? Please Help me