Tweak to get timeout in serial read/write less than 1 second

6 vues (au cours des 30 derniers jours)
Marko Gulin
Marko Gulin le 22 Déc 2019
Modifié(e) : Marko Gulin le 22 Déc 2019
Hi all,
It seems that Timeuot property in serial object, although specified as double, is rounded upwards full seconds.
Is there a tweak to have it less than 1 second? I really don't understand why they decided to implement the Timeout property like that in the first place.
I have an idea to use a timer in single shot execution mode. Although it works, I'm not sure if it is recommended to have it like that.
Here is my proposal, please let me know what you think:
% obj.flag is logical flag
% obj.ser is serial port object
function readTimeout(obj)
%READTIMEOUT
% Timeout has occurred
obj.flag = true;
end
function msg = read(obj, noOfBytes, timeout)
%READ
% Create timeout timer
tmr = timer( ...
'TimerFcn', @(objj,event) obj.readTimeout(), ...
'ExecutionMode', 'singleShot', ...
'StartDelay', timeout ...
);
% Start timeout timer
obj.flag = false;
start(tmr);
% Wait for bytes
while (obj.ser.BytesAvailable < noOfBytes)
if obj.flag == true
error('serial:timeout', 'Timeout exception.');
end
end
% Read available bytes
msg = fread(obj.ser, noOfBytes, 'uint8');
end

Réponses (0)

Catégories

En savoir plus sur MATLAB 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