How to use error handling with tcpclient in R2021b?
6 次查看(过去 30 天)
显示 更早的评论
I am using tcpclient to communicate with a remote server, and when data is received I want to process that data using a callback. However, if any error occurs in the callback, that error is not reported to the command line. Here's a minimum working example:
echotcpip('on',7841); %Just a random port
obj = tcpclient('localhost',7841);
obj.configureTerminator('CR/LF');
obj.configureCallback('terminator',@(~,~) error('An error!'));
obj.writeline('Hello');
When data is received, I expect an error to be thrown, but instead nothing happens. If I replace the callback with something that doesn't throw an error:
obj.configureCallback('terminator',@(~,~) disp('No errors here'));
obj.writeline('Hello');
the line "No errors here" is printed on the command line, as expected.
How do I get the errors in the callback to show up? The ErrorOccurredFcn appears to only apply to internal errors in the tcpclient class, and not to errors in the callback function.
This is a problem in R2021b, but not in R2022a (and probably in later versions), but if there is a solution that does not involve upgrading to the next release, that would be great. I've had severe performance issues with tcpclient when switching versions, and I'd like to avoid the risk of dealing with that if it isn't necessary.
0 个评论
采纳的回答
Anushka
2025-1-23
编辑:Anushka
2025-1-23
Hi,
To address the issue of errors not being reported from the ‘callback’ function in MATLAB R2021b, you can implement a workaround using a MATLAB ‘timer’. This approach mimics the behaviour of the callback by periodically checking for incoming data and processing it.
Here’s how you can achieve this:
% Start the echo server (if needed, otherwise comment out)
% echotcpip('on', 7841); % Just a random port
% Create a tcpclient object
obj = tcpclient("www.rfc-editor.org", 80);
% Configure the terminator
configureTerminator(obj, "LF", "CR/LF");
% Create a timer to check for data periodically
dataTimer = timer( ...
'ExecutionMode', 'fixedSpacing', ...
'Period', 0.1, ... % Check every 0.1 seconds
'TimerFcn', @(~, ~) processData(obj) ...
);
% Start the timer
start(dataTimer);
% Send data to trigger processing
writeline(obj, "GET /rfc/rfc793.txt HTTP/1.1");
writeline(obj, "Host: www.rfc-editor.org");
writeline(obj, "Connection: close");
writeline(obj, ""); % Blank line to indicate end of headers
% Define the data processing function
function processData(client)
try
% Check if data is available
while client.NumBytesAvailable > 0
% Read and process the data
data = readline(client);
fprintf("Received data: %s\n", data);
% Simulate an error for demonstration purposes
fprintf('A simulated error occurred while processing data!\n');
end
catch ME
% Handle or rethrow the error as needed
disp(['Error: ', ME.message]);
end
end
Here’s the link to the documentations you can refer to for more information:
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!