1 | function [AM, tout, DataTime, ErrorFlag] = getpvonline(ChannelNames, varargin) |
---|
2 | %GETPVONLINE - Get the online value via OPC control |
---|
3 | % |
---|
4 | % [AM, tout, DataTime, ErrorFlag] = getpvonline(ChannelNames, DataType, N, t) |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. ChannelNames |
---|
8 | % 2. DataType - 'Double', 'String', 'Vector' or 'Waveform', 'Matrix' {Default: 'Double'} |
---|
9 | % 3. N - Number of data points to return {Default: 1} |
---|
10 | % (only used for 'Vector', 'Waveform', and 'Matrix' DataTypes) |
---|
11 | % 4. t - time vector of when to start a read [seconds] |
---|
12 | % |
---|
13 | % OUTPUTS |
---|
14 | % 1. AM - Value |
---|
15 | % 2. tout - Local computer time on finish of data read |
---|
16 | % 3. DataTime = time (in seconds) since 00:00:00, Jan 1, 1970 |
---|
17 | % (seconds + nanoseconds * i) |
---|
18 | % 4. ErrorFlag (Presently not functional. All errors will cause a Matlab error.) |
---|
19 | % |
---|
20 | % Written by Greg Portmann |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | % To do |
---|
25 | % 1. NAvg, AvgPeriod |
---|
26 | |
---|
27 | |
---|
28 | ErrorFlag = 0; |
---|
29 | |
---|
30 | |
---|
31 | % Function start time |
---|
32 | t0 = clock; |
---|
33 | |
---|
34 | |
---|
35 | if nargin < 1 |
---|
36 | error('Must have at least a channel name input'); |
---|
37 | end |
---|
38 | |
---|
39 | |
---|
40 | % Input parsing |
---|
41 | DataType = 'Double'; |
---|
42 | N = 0; |
---|
43 | t = 0; |
---|
44 | NAvg = 1; |
---|
45 | AvgPeriod = 0; |
---|
46 | if length(varargin) >= 1 |
---|
47 | if ischar(varargin{1}) |
---|
48 | DataType = varargin{1}; |
---|
49 | varargin(1) = []; |
---|
50 | if length(varargin) >= 1 |
---|
51 | N = varargin{1}; |
---|
52 | varargin(1) = []; |
---|
53 | end |
---|
54 | end |
---|
55 | if length(varargin) >= 1 |
---|
56 | t = varargin{1}; |
---|
57 | if length(varargin) >= 2 |
---|
58 | NAvg = varargin{2}; |
---|
59 | if length(varargin) >= 3 |
---|
60 | AvgPeriod = varargin{3}; |
---|
61 | if length(varargin) >= 4 |
---|
62 | FreshDataFlag = varargin{4}; |
---|
63 | end |
---|
64 | end |
---|
65 | end |
---|
66 | end |
---|
67 | end |
---|
68 | |
---|
69 | % Scalars |
---|
70 | N = N(1); |
---|
71 | NAvg = NAvg(1); |
---|
72 | AvgPeriod = AvgPeriod(1); |
---|
73 | |
---|
74 | % t must be a row vector |
---|
75 | t = t(:)'; |
---|
76 | |
---|
77 | |
---|
78 | % Vectorized Get |
---|
79 | % There can be multiple channel names due to "ganged" power supplies (or redundent user input) |
---|
80 | [ChannelNames, i, j] = unique(ChannelNames, 'rows'); |
---|
81 | |
---|
82 | |
---|
83 | % Remove ' ' and fill with NaN latter (' ' should always be the first row) |
---|
84 | if isempty(deblank(ChannelNames(1,:))) |
---|
85 | iblank = 1; |
---|
86 | ChannelNames(1,:) = []; |
---|
87 | else |
---|
88 | iblank = []; |
---|
89 | end |
---|
90 | |
---|
91 | |
---|
92 | if size(ChannelNames,1) > 0 |
---|
93 | |
---|
94 | if strcmpi(DataType, 'String') |
---|
95 | |
---|
96 | error('SPS does not return strings at the moment.'); |
---|
97 | |
---|
98 | elseif strcmpi(DataType, 'Vector') |
---|
99 | |
---|
100 | % Get data |
---|
101 | AM = getopc(ChannelNames); |
---|
102 | AM = AM(:); |
---|
103 | if N > 0 |
---|
104 | AM = AM(1:N); |
---|
105 | end |
---|
106 | tout = etime(clock, t0); |
---|
107 | |
---|
108 | elseif strcmpi(DataType, 'Matrix') | strcmpi(DataType, 'Waveform') |
---|
109 | |
---|
110 | % Get data |
---|
111 | for k = 1:size(ChannelNames,1) |
---|
112 | tmp = getopc(deblank(ChannelNames(i,:))); |
---|
113 | AM(k,:) = tmp(:)'; |
---|
114 | end |
---|
115 | tout = etime(clock, t0); |
---|
116 | |
---|
117 | else |
---|
118 | |
---|
119 | % Get data |
---|
120 | ExtraTimeDelay = etime(clock, t0); |
---|
121 | t = t - ExtraTimeDelay; |
---|
122 | for itime = 1:length(t) |
---|
123 | T = t(itime)-etime(clock, t0); |
---|
124 | if T > 0 |
---|
125 | pause(T); |
---|
126 | end |
---|
127 | |
---|
128 | % Get data |
---|
129 | AM(:, itime) = getopc(ChannelNames); |
---|
130 | tout(1,itime) = etime(clock, t0); |
---|
131 | end |
---|
132 | |
---|
133 | end |
---|
134 | |
---|
135 | |
---|
136 | % DataTime |
---|
137 | if nargout >= 3 |
---|
138 | days = datenum(t0(1:3)) - 719529; %datenum('1-Jan-1970'); |
---|
139 | tt = 24*60*60*days + 60*60*t0(4) + 60*t0(5) + (t0(6) + tout - ExtraTimeDelay); |
---|
140 | DataTime = ones(size(AM,1),1) * (fix(tt) + rem(tt,1)*1e9*sqrt(-1)); |
---|
141 | end |
---|
142 | |
---|
143 | else |
---|
144 | |
---|
145 | t1 = clock; |
---|
146 | tout = etime(t1, t0); |
---|
147 | days = datenum(t1(1:3)) - 719529; %datenum('1-Jan-1970'); |
---|
148 | tt = 24*60*60*days + 60*60*t1(4) + 60*t1(5) + t1(6); |
---|
149 | DataTime = fix(tt) + rem(tt,1)*1e9*sqrt(-1); |
---|
150 | |
---|
151 | % Expand multiple channelnames back to multiple devices |
---|
152 | AM = NaN; |
---|
153 | AM = AM(j,:); |
---|
154 | return |
---|
155 | |
---|
156 | end |
---|
157 | |
---|
158 | |
---|
159 | % Expand the blank channel names |
---|
160 | if ~isempty(iblank) |
---|
161 | AM = [NaN*ones(1,size(AM,2)); AM(iblank:end,:)]; |
---|
162 | if nargout >= 3 |
---|
163 | DataTime = [(NaN+NaN*sqrt(-1))*ones(1,size(DataTime,2)); DataTime(iblank:end)]; |
---|
164 | end |
---|
165 | end |
---|
166 | |
---|
167 | |
---|
168 | % Expand multiple channelnames back to multiple devices |
---|
169 | AM = AM(j,:); |
---|
170 | if nargout >= 3 |
---|
171 | DataTime = DataTime(j,:); |
---|
172 | end |
---|
173 | |
---|