-
Notifications
You must be signed in to change notification settings - Fork 0
/
serIO.m
80 lines (75 loc) · 2.37 KB
/
serIO.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function varargout = serIO(cmd, varargin)
% out = serIO('cmd', h, param)
%
% This is a simple wrapper to use serFTDI and IOPort. Its syntax is the same as
% serFTDI which is used if available. If serFTDI is not available or fails to
% open a port, IOPort will be used.
%
% TF = serIO('use_serFTDI', true_or_false)
% - Force to use or not use serFTDI.
%
% See also: serFTDI, IOPort
% 171008 Write it (Xiangrui.Li at gmail.com)
% 180902 fix for 64-bit Octave
persistent use_serFTDI clnObj; %#ok
if isempty(use_serFTDI)
if exist('OCTAVE_VERSION', 'builtin')
try
serFTDI('Accessible');
catch
if ismac
a = 'OctaveMac64';
elseif isunix
a = 'OctaveLnx64';
elseif ~isempty(strfind(computer, 'x86_64')) %#ok 64-bit
a = 'OctaveWin64';
else
a = 'OctaveWin32';
end
pth = fileparts(which('serIO'));
oriName = fullfile(pth, ['serFTDI.mex' a]);
try
mexNam = fullfile(pth, 'serFTDI.mex');
copyfile(oriName, mexNam);
catch % in case permission error
mexNam = fullfile(pwd, 'serFTDI.mex');
copyfile(oriName, mexNam);
end
rehash path;
end
end
use_serFTDI = serFTDI('Accessible');
if ~use_serFTDI
verbo = IOPort('Verbosity', 0);
clnObj = onCleanup(@() IOPort('Verbosity', verbo));
end
end
if strcmp(cmd, 'use_serFTDI')
if nargin>1, use_serFTDI = logical(varargin{1}); end
if nargout, varargout{1} = use_serFTDI; end
return;
end
if use_serFTDI
[varargout{1:nargout}] = serFTDI(cmd, varargin{:});
return;
end
% Fallback to IOPort
if strcmpi(cmd, 'Write')
[~, tpost, ~, tpre] = IOPort(cmd, varargin{:});
varargout = {tpre, tpost};
elseif strcmpi(cmd, 'Read')
if nargin>2, [varargout{1:nargout}] = IOPort(cmd, varargin{1}, 1, varargin{2:end});
else, [varargout{1:nargout}] = IOPort(cmd, varargin{1});
end
else %
if strcmpi(cmd, 'Open')
cmd = 'OpenSerialPort';
dft = ' BaudRate=115200 ReceiveTimeout=0.3';
if nargin<3, varargin{2} = dft;
else, varargin{2} = [varargin{2} dft];
end
elseif strcmpi(cmd, 'Configure')
cmd = 'ConfigureSerialPort';
end
[varargout{1:nargout}] = IOPort(cmd, varargin{:});
end