Tweak to get timeout in serial read/write less than 1 second
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!