I'm receiving a stream of data captured from some ECG-equipment via Bluetooth. I save the data in an array but I'd also like to show the data in "real time".
What is the best approach? Now I use a bitmap in witch i draw lines like
Code:
bmpGraphics.DrawLine(blackpen, oldX, lastPlottedValue, oldX++, newValue)
but that's too slow. I call
Code:
pictureBox_graph.Refresh()
for every 10:th value plotted.
I have to plot at at least 400Hz. Any suggestions? Any nice (free) component that can help me? Any nice methods to use?
Example code:
Code:
private void port_DataReceived()
{
while(port.InBufferCount > 1)
{
// Read two bytes from port and save them as a short in the array
dataStorage[recievedValues++] = BitConverter.ToInt16(port.Input, 0);
// Update the GUI every 10th reception
if(recievedValues % 20 == 0)
{
try
{
this.Invoke(new EventHandler(updateGUI));
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
private void updateGUI(object o, EventArgs e)
{
// Update the recieved values label.
label2.Text = recievedValues.ToString();
// Draw the graph
drawGraph();
}
private void hScrollBar1_ValueChanged_1(object sender, System.EventArgs e)
{
this.graph_panel.Left = -this.hScrollBar1.Value;
}
private void drawGraph()
{
// Do this as many times as there's unplotted values.
for(int i = 0; i < recievedValues - plottedValues; i++)
{
// Calculate new y-value to plot
int y2 = (int)((dataStorage[plottedValues + 1] - short.MinValue) / yscale);
// Draw a line from last plotted point to the one calculated above
bmpGraphics.DrawLine(blackpen, plottedValues, lastPlottedValue, plottedValues + 1, y2);
// Increase plotted values counter
plottedValues++;
// Save the plotted value to use as starting point for next line
lastPlottedValue = y2;
// Set the scroll bar into right position
hScrollBar1.Maximum = plottedValues - hScrollBar1.Width; // Should be width of something else, but it didn't work =(
hScrollBar1.Value = hScrollBar1.Maximum;
}
pictureBox_graph.Refresh();
}
}