Socket desync when using pause function

2 vues (au cours des 30 derniers jours)
Daniel Horvath
Daniel Horvath le 20 Avr 2022
Réponse apportée : Ravi le 21 Déc 2023
Hello everyone,
I'm working on a project which involves the use of realtime data flow between a C++ program and a Matlab script. The C++ program gets the data from a haptic device and sends it through a socket connection to the Matlab program to be used elsewhere. The Matlab script uses a while loop to continuously read the incoming data from the C++ program and to further process that data. The thing is that I need this communication to be as fast as possible and that means that I can't really use any pauses in my while loop that I have in my Matlab script, but since I wish to make a GUI with app designer (a start button and a stop button), I think that I need to use a pause function so i can effectively interrupt the while loop using the stop button. The time value I have set for the pause is 0.0001 so that it has the least possible delay and at first this seems to work fine but after a minute or less the data flow becomes desynced, meaning that it's no longer being sent in real time and it becomes delayed by a lot. I believe that due to the pause the socket client, after some time, desyncs and reads old data which had been sent many seconds before, instead of reading the current values sent, but I do not know the full extent of the problem.
How can this delay be resolved? Should I be using some other loop in order to read the incoming data? Is there any other way to interrup a while loop in a app designer GUI without using pause() or drawnow()?
Thanks.

Réponses (1)

Ravi
Ravi le 21 Déc 2023
Hi Daniel Horvath,
I understand that you are experiencing delays when “pause” function is used within the “while” loop.
In MATLAB, using a while loop for real-time applications can be challenging, as the pause function can introduce unwanted delays, as you've experienced. Instead of using a while loop with pause, you might consider using a timer-based approach, which is better suited for real-time applications and GUIs.
Here's a basic example of how you might structure your MATLAB code using a timer:
classdef RealTimeApp < matlab.apps.AppBase
properties (Access = private)
Timer;
SocketClient;
DataBuffer;
IsRunning = false;
end
methods (Access = private)
function startButtonPushed(app, event)
app.IsRunning = true;
app.Timer = timer('ExecutionMode', 'fixedRate', 'Period', 0.001, ...
'TimerFcn', @(~,~)app.updateData());
start(app.Timer);
end
function stopButtonPushed(app, event)
app.IsRunning = false;
stop(app.Timer);
end
function updateData(app)
if app.IsRunning
% Perform your data processing and visualization here
% e.g., read data from the socket and update UI
newData = app.SocketClient.readData();
app.DataBuffer = [app.DataBuffer, newData];
% Update your GUI elements
app.updateUI();
end
end
function updateUI(app)
% Update GUI elements with the latest data
% For example, update a plot or text box
end
end
methods (Access = public)
function app = RealTimeApp
% Initialize your GUI components and set up the socket client
app.SocketClient = SocketClient(); % Replace with your actual socket client class
app.DataBuffer = [];
end
end
methods (Access = protected)
function createComponents(app)
% Create and configure GUI components
% For example, create buttons, plots, etc.
end
end
end
This example uses MATLAB's timer object to repeatedly call the updateData method at a specified rate. This approach avoids the need for a while loop and eliminates the use of pause. The timer-based approach allows for better synchronization and responsiveness in GUI-based real-time applications.
To know more about the “timer” function, please refer to the following link.
I hope my explanation resolves the issue you are facing.
Thanks,
Ravi Chandra

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by