Reputation: 17
Im taking as an input a large datasheet of a device , nearly 50.000 data every second .
Im trying to plot them as fast as possible ,the data send via websocket from a python script in my WPF app and the timer is set to 0.1 seconds in both python and WPF app.
The thing is that if i set them every second its working fine but if i set the timer less than 1 second its not working as expected .It have slower render times nearly every 5 seconds or nothing ..
Here are some parts of my code :
Python :
async def send_data(websocket):
global min_data
while True:
if min_data is not None:
data = {"min": min_data}
try:
await websocket.send(json.dumps(data))
except websockets.ConnectionClosed as e:
print(f"Websocket connection closed: {e}")
await asyncio.sleep(0.1)
WPF:
public Plot()
{
InitializeComponent();
InitializeRenderingLoop();
}
//Loop
private void InitializeRenderingLoop()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(0.1);
timer.Tick += PlotView;
}
//Plot
private async void PlotView(object sender, EventArgs e)
{
if (!isPaused)
{
try
{
var data = await GetDataFromSocketAsync("xxxx.xxxx.xxxx.xxxx", xxxx);
if (data.Count == 0)
return;
// Reuse the existing PlotModel
if (SpectralPlot.Model == null)
{
Debug.Write("========= Entered =========");
SpectralPlot.Model = new PlotModel
{
Title = "Data from Socket",
Background = OxyColors.Transparent,
TextColor = OxyColors.White,
PlotAreaBorderColor = OxyColors.White
};
SpectralPlot.Model.Axes.Add(new LinearAxis
{
Position = AxisPosition.Bottom,
Title = "Wavelength",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
StringFormat = "0.###",
});
SpectralPlot.Model.Axes.Add(new LinearAxis
{
Position = AxisPosition.Left,
Title = "Units",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
StringFormat = "0.###",
Maximum = 5
});
SpectralPlot.Model.Series.Add(new LineSeries
{
Title = "Data",
Color = OxyColors.White,
MarkerFill = OxyColors.White,
MarkerType = MarkerType.None,
LineStyle = LineStyle.Solid,
StrokeThickness = 2
});
SpectralPlot.Model.Series.Add(new LineSeries
{
Title = "Red Line",
Color = OxyColors.Red,
MarkerFill = OxyColors.Red,
MarkerType = MarkerType.None,
LineStyle = LineStyle.Solid,
StrokeThickness = 2,
});
}
var series = SpectralPlot.Model.Series[0] as LineSeries;
if (series != null)
{
series.Points.Clear();
foreach (var point in data)
{
series.Points.Add(new DataPoint(point.X, point.Y));
}
}
SpectralPlot.Model.InvalidatePlot(true);
DetectPeaks(data.Select(p => p.X).ToArray(), data.Select(p => p.Y).ToArray());
}
catch (Exception ex)
{
}
}
private async Task<List<PointData>> GetDataFromSocketAsync(string ipAddress, int port)
{
using var clientWebSocket = new ClientWebSocket();
Uri uri;
if (Uri.TryCreate($"ws://{ipAddress}:{port}", UriKind.Absolute, out uri))
{
await clientWebSocket.ConnectAsync(uri, CancellationToken.None);
}
else
{
Debug.WriteLine("Invalid URI");
return new List<PointData>();
}
var receiveBuffer = new ArraySegment<byte>(new byte[1024]);
var fullData = new StringBuilder();
WebSocketReceiveResult result;
do
{
result = await clientWebSocket.ReceiveAsync(receiveBuffer, CancellationToken.None);
fullData.Append(Encoding.UTF8.GetString(receiveBuffer.Array, 0, result.Count));
}
while (!result.EndOfMessage);
var json = fullData.ToString();
Debug.WriteLine($"Received JSON data: {json}");
try
{
var minValues = JsonConvert.DeserializeObject<MinValues>(json);
var yData = minValues.min;
return GetPointData(yData);
}
catch (JsonException ex)
{
Debug.WriteLine($"JsonException: {ex.Message}");
Debug.WriteLine($"JSON data: {json}");
return new List<PointData>();
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex.Message}");
return new List<PointData>();
}
}
Upvotes: 0
Views: 90