-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR524.m
82 lines (68 loc) · 2.78 KB
/
PR524.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
81
classdef PR524 < serial
%PR524 Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function meter = PR524()
port = FindPortOf('PR Instrument');
meter@serial(port);
% Communication Settings:
% For some reason, the meter wants to be sent just one
% character at a time, so don't let MATLAB send CR/LF to the
% device. Reading is fine though, since MATLAB will buffer the
% CR/LF. The device doesn't seem to use any sort of standard
% flow control.
set(meter, 'Terminator', {'CR/LF', ''});
set(meter, 'FlowControl', 'none');
fopen(meter);
send(meter, 'PHOTO'); % Set the meter to Remote Mode
send(meter, 'C'); % Clear any errors
pause(0.1);
if get(meter, 'BytesAvailable') > 0
% error was cleared
% ignore any "Instrument error cleared" message
fscanf(meter);
end
end
function delete(meter)
if meter.isvalid && strcmpi(get(meter, 'status'), 'open')
send(meter, 'Q'); % Turn off Remote Mode
fclose(meter);
end
delete@serial(meter)
end
function send(meter, cmd)
% Must send one character at a time :(
% Make sure the command ends with CR/LF
if length(cmd) < 2 || ~strcmpi(cmd(end-1:end), sprintf('\r\n'))
cmd = sprintf('%s\r\n', strtrim(cmd));
end
for idx = 1:length(cmd)
fprintf(meter, cmd(idx));
end
end
function varargout = sendAndRead(meter, cmd, format, pauseTime)
% Output is same as Instrument Control Toolbox's fscanf
if nargin < 3 || isempty(format)
format = '%s'; % discards the CR/LF terminator
end
if nargin < 4 || isempty(pauseTime)
pauseTime = 0.1;
% TODO maybe: if strcmpi(cmd(1), 'M'); pauseTime = 4.0; end;
% or a "wait for data" flag that does the fscanf without checking for data?
end
varargout = cell(1, min(nargout, 1));
meter.send(cmd);
pause(pauseTime);
if get(meter, 'BytesAvailable') > 0
[varargout{:}] = fscanf(meter, format);
else
if nargout >= 1; varargout{1} = ''; end;
if nargout >= 2; varargout{2} = 0; end;
if nargout >= 3; varargout{3} = 'No response.'; end;
% TODO throw warning iff narargout < 3?
end
end
end
end