Tweak to get timeout in serial read/write less than 1 second
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!