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

조회 수: 3 (최근 30일)
Marko Gulin
Marko Gulin 2019년 12월 22일
편집: Marko Gulin 2019년 12월 22일
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개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by