Bom dia galera, estou desenvolvendo um programa em que deve ser plotado vários gráficos de linha(x,y) na tela, o eixo y corresponde a porta serial onde está conectada um Arduino UNO. Como exemplo, eu preciso de um gráfico onde eu tenha 6000 pontos desenhados em 20 segundos(cerca de 3.333 ms cada novo ponto). O problema é que para desenhar os 6000 pontos o programa está demorando cerca de 35 segundos. Segue chamada do controle graphControl1 que desenha o gráfico.
private void timerDRAW_Tick(object sender, EventArgs e)
{
//timerDRAW com intervalo de 3 ms
SerialCOM.DataReceived += new SerialDataReceivedEventHandler(SerialCOM_DataReceived);
}
private void SerialCOM_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (SerialCOM.IsOpen)
{
SerialPort sData = (SerialPort)sender;
{
string RxString = sData.ReadLine();
bool result = int.TryParse(RxString, out int data);
if (result)
{
var atualdata = 5.0f * data / 1023.0f;
graphControl1.AddValue(atualdata, atualdata, atualdata, atualdata);
}
}
}
}
Já tentei várias modificações no código. Recebi dicas que o método DataReceived pode ser otimizado para melhorar o desempenho do gráfico, mas não estou conseguindo implementar outra solução.
Não sei se somente essas informações são suficientes para resolver meu problema pois sou iniciante. O controle graphControl é um pouco extenso por isso não estarei anexando, mas se alguém tiver disponibilidade em ajudar eu posso enviar o código. Valeu galera !!
EDIT: Estou upando o código do controle graphControl para análise
public class GraphControl : Control
{
#region DECLARAÇAO DE VARIAVEIS GLOBAIS
int contador = 0;
private int _minValue = 0;
private int _maxValue = 10;
private int _dataCount = 6000;
private List<float> _values1 = new List<float>();
private List<float> _values2 = new List<float>();
private List<float> _values3 = new List<float>();
private List<float> _values4 = new List<float>();
#endregion
public int MinValue
{
get => _minValue;
set
{
_minValue = value;
Invalidate();
}
}
public int MaxValue
{
get => _maxValue;
set
{
_maxValue = value;
Invalidate();
}
}
public int DataCount
{
get => _dataCount;
set
{
_dataCount = value;
Invalidate();
}
}
public GraphControl()
{
Size = new System.Drawing.Size(498, 264);
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
}
public void AddValue(float value1, float value2, float value3, float value4)
{
_values1.Add(value1);
_values2.Add(value2);
_values3.Add(value3);
_values4.Add(value4);
contador++;
if (contador == 4)
{
Invalidate();
contador = 0;
}
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
var wid = (float)Width / (float)_dataCount;
var total = 5;
//-----------------------------------------1--------------------------------------------------------------------
var lastPoint1 = new PointF(0F, (float)(2 * Height / 5) * 0.5F);
var lastPoint2 = new PointF(0F, (float)(Height / 2));
var lastPoint3 = new PointF(0F, (float)(7 * Height / 10));
var lastPoint4 = new PointF(0F, (float)(9 * Height / 10));
PointF newPoint1 = new PointF();
PointF newPoint2 = new PointF();
PointF newPoint3 = new PointF();
PointF newPoint4 = new PointF();
var idInit = _values1.Count - _dataCount;
if (idInit < 0)
idInit = 0;
for (int i = idInit, a = 0; i < _values1.Count; i++, a++)
{
var value = (float)(_values1[i]);
var porcent = value / total;
if (porcent > 1) porcent = 1;
var hg = porcent * (2 * Height / 5);
newPoint1.X = (a + 1) * wid;
if (porcent >= 1) newPoint1.Y = 2 * Height / 5 - hg;
else newPoint1.Y = 2 * Height / 5 - hg - 1;
using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint1, newPoint1);
lastPoint1 = newPoint1;
//------------------------------------------------------------------------------------
value = (float)(_values2[i]);
porcent = value / total;
hg = porcent * (Height / 5);
newPoint2.X = (a + 1) * wid;
newPoint2.Y = 3 * Height / 5 - hg;
using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint2, newPoint2);
lastPoint2 = newPoint2;
//------------------------------------------------------------------------------------
value = (float)(_values3[i]);
porcent = value / total;
hg = porcent * (Height / 5);
newPoint3.X = (a + 1) * wid;
newPoint3.Y = 4 * Height / 5 - hg;
using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint3, newPoint3);
lastPoint3 = newPoint3;
//------------------------------------------------------------------------------------
value = (float)(_values4[i]);
porcent = value / total;
hg = porcent * (Height / 5);
newPoint4.X = (a + 1) * wid;
newPoint4.Y = Height - hg -1;
using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint4, newPoint4);
lastPoint4 = newPoint4;
}
base.OnPaint(e);
}
}
}
Na hora de enviar os valores para o graphControl estou usando os 4 valores iguais, mas é somente uma situação de teste. Se alguém puder me ajudar agradeço.