Skip to content
Snippets Groups Projects
Commit c1d34f1d authored by Tim Stadtmann's avatar Tim Stadtmann
Browse files

Implement time-out mechanism in lower layers

Fixes issue #45.
parent 595d4c75
Branches
Tags
No related merge requests found
...@@ -12,6 +12,11 @@ ...@@ -12,6 +12,11 @@
% - The write function should be given a uint8 datatype as a parameter % - The write function should be given a uint8 datatype as a parameter
classdef BrickIO < handle classdef BrickIO < handle
properties (Abstract)
% time-out period (if 0, no time-out)
timeOut
end
properties (Abstract, Access = 'protected') properties (Abstract, Access = 'protected')
% connection handle % connection handle
handle handle
......
This diff is collapsed.
...@@ -30,6 +30,8 @@ classdef btBrickIO < BrickIO ...@@ -30,6 +30,8 @@ classdef btBrickIO < BrickIO
debug = 0; debug = 0;
% bluetooth serial port % bluetooth serial port
serialPort = '/dev/rfcomm0' serialPort = '/dev/rfcomm0'
% time-out period in seconds (if 0, no time-out)
timeOut = 10;
end end
properties (Access = 'protected') properties (Access = 'protected')
...@@ -221,5 +223,17 @@ classdef btBrickIO < BrickIO ...@@ -221,5 +223,17 @@ classdef btBrickIO < BrickIO
end end
end end
end end
function set.timeOut(brickIO, timeOut)
if ~isnumeric(timeOut) || timeOut < 0
error(ID(), 'timeOut-period of Bluetooth-handle can only be set to positive numerical values.');
end
brickIO.timeOut = timeOut;
if timeOut == 0
brickIO.handle.Timeout = 9999999; % MATLAB.serial seems to have no option to disable timeout
end
end
end end
end end
...@@ -253,8 +253,63 @@ classdef hidapi < handle ...@@ -253,8 +253,63 @@ classdef hidapi < handle
hid.handle = []; hid.handle = [];
end end
% @ MMI
function rmsg = read_timeout(hid, timeOut)
%hidapi.read_timeout Read from hid object with a time-out
%
% rmsg = hid.read_timeout() reads from a hid device and returns the
% read bytes. Will print an error if no data was read during the time-out period.
%
% Arguments::
% timeOut time-out period in milliseconds
%
% Throws::
% CommError Error during communication with device
% InvalidHandle Handle to USB-device not valid
%
if hid.debug > 0
fprintf('hidapi read_timeout\n');
end
% Read buffer of nReadBuffer length
buffer = zeros(1,hid.nReadBuffer);
% Create a uint8 pointer
pbuffer = libpointer('uint8Ptr', uint8(buffer));
% Check if pointer is (unexpectedly) already invalidated
assert(isLibPointerValid(hid.handle)==1, ...
[hid.slib, ':', 'InvalidHandle'], ...
'Failed to read USB-data because pointer to USB-device is invalidated.');
% Read data from HID device
[res,h] = calllib(hid.slib,'hid_read_timeout',hid.handle,pbuffer,uint64(length(buffer)),timeOut);
% Check the response (No assert as there are multiple cases)
if res < 1
% Error occurred
id = [hid.slib, ':', 'CommError'];
% Narrow error down
if res == -1
msg = 'Connection error (probably lost connection to device)';
elseif res == 0
msg = ['Could not read data from device (device is still connected, ',...
'but does not react)'];
else
msg = 'Unexpected connection error';
end
causeException = MException(id, msg);
ME = MException(id, 'Failed to read data via USB.');
addCause(ME, causeException);
throw(ME);
end
% Return the string value
rmsg = pbuffer.Value;
end
function rmsg = read(hid) function rmsg = read(hid)
%hidapi.rmsg Read from hid object %hidapi.read Read from hid object
% %
% rmsg = hid.read() reads from a hid device and returns the % rmsg = hid.read() reads from a hid device and returns the
% read bytes. Will print an error if no data was read. % read bytes. Will print an error if no data was read.
......
...@@ -28,6 +28,8 @@ classdef usbBrickIO < BrickIO ...@@ -28,6 +28,8 @@ classdef usbBrickIO < BrickIO
nReadBuffer = 1024; nReadBuffer = 1024;
% write buffer size % write buffer size
nWriteBuffer = 1024; nWriteBuffer = 1024;
% time-out period in milliseconds (if 0, no time-out)
timeOut = 10000;
end end
properties (Access = 'protected') properties (Access = 'protected')
...@@ -173,7 +175,11 @@ classdef usbBrickIO < BrickIO ...@@ -173,7 +175,11 @@ classdef usbBrickIO < BrickIO
% Read from the usb handle % Read from the usb handle
try try
rmsg = brickIO.handle.read; if brickIO.timeOut ~= 0
rmsg = brickIO.handle.read_timeout(brickIO.timeOut);
else
rmsg = brickIO.handle.read();
end
catch ME catch ME
if ~isempty(strfind(ME.identifier, 'CommError')) if ~isempty(strfind(ME.identifier, 'CommError'))
% Throw a clean CommError to avoid confusion in upper layers % Throw a clean CommError to avoid confusion in upper layers
...@@ -241,5 +247,13 @@ classdef usbBrickIO < BrickIO ...@@ -241,5 +247,13 @@ classdef usbBrickIO < BrickIO
end end
end end
end end
function set.timeOut(brickIO, timeOut)
if ~isnumeric(timeOut) || timeOut < 0
error(ID(), 'timeOut-period of USB-handle can only be set to positive numerical values.');
end
brickIO.timeOut = timeOut*1000;
end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment