I am trying to plot real time data. A circuit sends data rapidly and I know that the first byte is '$', next 16 bytes as sound and last byte is pulse sensor data.I am using a method that stores data to an array and it continuously adds data inside array starting from the last index.
byte[] Read_Data1 = new byte[100000];
byte[] Read_Data2 = new byte[100000];
byte[] Read_Data3;
private void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!myPort.IsOpen)
return;
while (myPort.BytesToRead > 0)
{
int bytes = myPort.BytesToRead;
byte[] buffer = new byte[bytes];
myPort.Read(buffer, 0, bytes);
bytetobyte(Read_Data1, buffer, buffer.Length, count);
count += buffer.Length;
}
}
public void bytetobyte(byte[] Storage, byte[] databyte, int datacount, int count)
{
//count comes from count += buffer.Lenght
int abc;
for (abc = 0; abc < datacount; abc++)
{
Storage[abc+count] = databyte[abc];
}
}
Then I start to process the data using a method that works with timer's tick. I should remove first byte '$' and need to save 16 bytes to a list and 1 byte to another list. Here is the method:
byte[] Read_Data3;
LinkedList<byte> data;
public void DrawingAudioData(byte[] data) //This method works inside timer.
{
Read_Data2 = Read_Data1;
int lastCount = count;
int division = count / 18;
int remaning = (count - 18 * division);
Read_Data3 = new byte[count - remaning];
for (int i = 0; i < count - remaning ; i++)
{
Read_Data3[i] = Read_Data2[i];
}
count = 0;
IPointListEdit listAuido = curveAudio.Points as IPointListEdit;
IPointListEdit listPulse = curvePulse.Points as IPointListEdit;
XDate time = new XDate(DateTime.Now);
if (Read_Data3 == null)
return;
data= new LinkedList<byte>(Read_Data3);
if(data.First == null)
return;
if (data.Count >= 18 & data.ElementAt(0) == Convert.ToByte('$'))
{
data.Remove(veri.ElementAt(0));
for (int x = 0; x < 16; x++)
{
listAuido.Add(time, data.ElementAt(0));
data.Remove(data.ElementAt(0));
}
listPulse.Add(time, data.ElementAt(0));
data.Remove(data.ElementAt(0));
}
lastCount = 0;
}
I think that when timer ticks I should have a new byte because serialport sends data rapidly and data are changed inside of Read_Data1. After that I equalize count to zero. Because data still flows and I don't want to my array goes out of rang. Then I start to process the new Read_Data2. I must process each 18 bytes as I said before so I trim the Read_Data2 and removed the reamaning last few data and I equalize Read_Data3[i]=Read_Data2[i];
Then using a LinkedList
that includes Read_Data3, I tried to write a loop that removes the first data after fill it into a list. After processing LinkedList, lastCount is equaled zero because I should set a new size according to new data set. Looks the logic is true but I have a problem. Sometimes the pulse sensor data is listing into the audio data. I mean the data should be inside the listPulse but it goes into the listAudio.
First of all I want to know approach is a correct approach. I mean using a timer is a good idea for rapid data? Please share your opinions. If you have a better way please tell me or give a reference article or samples' link. Thank you.