-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpumpsyr.m
150 lines (132 loc) · 5.45 KB
/
pumpsyr.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
classdef pumpsyr < handle
% Class for controlling NewEra syringe pumps with RS-232 connectivity.
%
% Usage is:
% pump_handle = pumpsyr('COM7')
% where COM7 is the com port that the pump is connected to. Then send
% commands to the pump as:
% pump_handle.interface(cmd_str)
% where cmd_str the command string, as explained in the user manual at http://syringepump.com/download/NE-1000%20Syringe%20Pump%20User%20Manual.pdf
%
%
% Based on arduino.m master/slave interface for Matlab by ...
% Giampiero Campa.
%
% Based on:
% Author: Lloyd Ung
% created: 2012-04-15
% last modified: 2012-10-11
%
% Modified significantly by Benjamin P Isaacoff (BPI)
% last modification Dec 2017
%% Class Constants
properties (Constant = true)
% Serial link parameters
newEraSerialConfig = struct('BaudRate',19200,'DataBits',8,...
'FlowControl','none','Parity','none','StopBits',1,...
'Terminator','CR');
% Constants
serialWaitTime = 0.5; % units of seconds
end
%% Class properties
% Private class properties
% properties (SetAccess = private, GetAccess = private)
properties
% Serial handle
serialHandle = [];
end
%% Class Methods
methods
% Class creator - initializes serial link.
function obj = pumpsyr(port)
% Check input string
if ~ischar(port)
error(['The input argument must be a string, e.g. '...
'''COM8'' ']);
end
% check if we are already connected
if isa(obj.serialHandle,'serial') && ...
isvalid(obj.serialHandle) && ...
strcmpi(get(obj.serialHandle,'Status'),'open')
disp('Desired port is not available.');
return;
end
% check whether serial port is currently used by MATLAB
if ~isempty(instrfind({'Port'},{port}))
disp(['The port ' port ' is already used by MATLAB']);
error(['Port ' port ' already used by MATLAB']);
end
% Choose the pump type
serialConfig = obj.newEraSerialConfig;
% define serial object and configure according to pumps.
obj.serialHandle = serial(port,'BaudRate',...
serialConfig.BaudRate,'DataBits',serialConfig.DataBits,...
'FlowControl',serialConfig.FlowControl,...
'Parity',serialConfig.Parity,...
'StopBits',serialConfig.StopBits,...
'Terminator',serialConfig.Terminator);
% Connect port
try
fopen(obj.serialHandle);
catch ME
disp(ME.message)
delete(obj);
% obj = [];
error(['Could not open port: ' port]);
end
pause(obj.serialWaitTime);
% Verifying that the pump is connected. It will error out here
% if the COM port is incorrect. Try five times before giving up
% reset any previously written programs on the pump
goodconct=false;
cntr=1;
while ~goodconct || cntr>5
msg=obj.sendcmd('*RESET');
if strcmp(msg,'00S')
goodconct=true;
else
cntr=cntr+1;
pause(obj.serialWaitTime);
end
end
if goodconct
disp('Pump successfully connected!');
else
warning('Problem with pump connection')
end
end
% delete pump object, closing serial
function delete(obj)
% Terminate the serial link.
% if it is a serial, valid and open then close it
if isa(obj.serialHandle,'serial') && ...
isvalid(obj.serialHandle) && ...
strcmpi(get(obj.serialHandle,'Status'),'open')
fclose(obj.serialHandle);
end
% if it's an object delete it
if isobject(obj.serialHandle)
delete(obj.serialHandle);
end
end
% Method for communicating with the pump.
function readString = sendcmd(obj,writeString,varargin)
% Add the address, if necessary.
if nargin > 2 && isnumeric(varargin{1}) && ...
isequal(size(varargin{1}),ones(1,2))
% Set the number between 0 and 99 to a string with two
% digits.
numString = num2str(mod(round(varargin{1}),100),'%02d');
% Concatenate address with write string.
writeString = [numString writeString];
end
% Print the command.
fprintf(obj.serialHandle,writeString);
% Wait for bytes to appear at the port.
pause(obj.serialWaitTime);
% Read the bytes at the port.
readString = fscanf(obj.serialHandle,'%s',...
get(obj.serialHandle,'BytesAvailable'));
end
end
end