You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 line
13KB

  1. classdef Equipment < handle
  2. %EQUIPMENT Summary of this class goes here
  3. % Detailed explanation goes here
  4. properties (SetAccess=protected)
  5. name
  6. tcp
  7. channel
  8. locked
  9. end
  10. properties (Dependent, SetAccess=private)
  11. error
  12. end
  13. methods
  14. function ecq = Equipment(ipAddress,port,channel)
  15. %EQUIPMENT Construct an instance of this class.
  16. % This functions opens the required TCP connection
  17. % for this device. Channel is GPIB-channel on prologix
  18. % converter if used, otherwise set channel to -1.
  19. % Name is queried from device via '*IDN?' command.
  20. ecq.tcp = Equipment.getTCP(ipAddress,port);
  21. ecq.locked = false;
  22. ecq.channel = channel;
  23. ecq.name = ecq.idn();
  24. if channel >= 0
  25. ecq.setPrologix;
  26. end
  27. end
  28. function id = idn(ecq)
  29. %IDN Queries identificantion from device via '*IDN?'.
  30. id = ecq.query_unsafe('*idn?');
  31. end
  32. function clear(ecq)
  33. %CLEAR Sends clear command to device via '*CLS'.
  34. %This function also clears the TCP inputbuffer;
  35. ecq.write_unsafe('*cls');
  36. flushinput(ecq.tcp);
  37. end
  38. function reset(ecq)
  39. %RESET Sends reset command to device via '*RST'.
  40. ecq.write_unsafe('*rst');
  41. end
  42. function flush(ecq)
  43. flushinput(ecq.tcp);
  44. flushoutput(ecq.tcp);
  45. end
  46. function opc(ecq)
  47. %OPC executes 'operation complete query' to device.
  48. %Function holds untill device returns '1'. Must be
  49. %used to avoid interrupting busy device.
  50. ecq.write_unsafe('*OPC?');
  51. for i = 1:10
  52. ack = ecq.read;
  53. if strcmp(ack(1),'1')
  54. return
  55. end
  56. end
  57. error('Device is not ready');
  58. end
  59. function unlock(ecq)
  60. %UNLOCK Sets the device back to 'local control'
  61. %This function only supports devices via the Prologix.
  62. if ecq.channel >= 0
  63. fprintf(ecq.tcp,'++loc');
  64. ecq.locked = false;
  65. end
  66. end
  67. function lock(ecq)
  68. %UNLOCK Sets the device back to 'remote control'
  69. %This function only supports devices via the Prologix.
  70. if ecq.channel >= 0
  71. fprintf(ecq.tcp,'++llo');
  72. ecq.locked = true;
  73. else
  74. warning('Device does not support locking')
  75. end
  76. end
  77. function beep(ecq)
  78. %BEEP Sends the beep command to the device
  79. ecq.write('SYST:BEEP')
  80. end
  81. function write(ecq,message)
  82. %WRITE Sends a command to the channel.
  83. %The function will first check if the device is ready to
  84. %recieve a command via the OPC function. Then send the command
  85. %and eventually check if the device has thrown an error via
  86. %the ERROR function.
  87. %
  88. %See also QUERY
  89. ecq.opc;
  90. ecq.write_unsafe(message);
  91. ecq.error;
  92. end
  93. function data = bin_read(ecq,datalength,type,typesize)
  94. try
  95. if nargin < 4
  96. typesize = 8;
  97. end
  98. data = zeros(1,datalength);
  99. totalread = 0;
  100. maxlength = floor(ecq.tcp.InputBufferSize/typesize)-16;
  101. while datalength > 0
  102. readlength = min(datalength,maxlength);
  103. lastcount = 0;
  104. [lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type);
  105. if lastcount == 0
  106. error('Data stream from device shorter than expected');
  107. end
  108. data(totalread+1:totalread+lastcount) = lastdata;
  109. totalread = totalread+lastcount;
  110. datalength = datalength - lastcount;
  111. end
  112. flushinput(ecq.tcp)
  113. ecq.clear
  114. catch ME
  115. flushinput(ecq.tcp);
  116. rethrow(ME);
  117. end
  118. end
  119. function data = bin_read_float(ecq,datalength)
  120. data = bin_read(ecq,datalength,'float',4);
  121. end
  122. function output = read(ecq)
  123. %READ Extracts recieved data from the TCP-buffer.
  124. %This function sends the '++read'-command to the Prologix if
  125. %needed. Will always read the TCP-buffer in matlab.
  126. if ecq.channel >= 0
  127. ecq.write_unsafe('++read');
  128. end
  129. output = fscanf(ecq.tcp);
  130. fprintf(['<< ',output]);
  131. end
  132. function output = query(ecq,message)
  133. %QUERY Sends command to the device and read the respond.
  134. %The command is send via the WRITE and the respond is collected
  135. %from the device.
  136. %
  137. %See also WRITE, READ
  138. ecq.opc;
  139. output = ecq.query_unsafe(message);
  140. ecq.error;
  141. end
  142. function errorlist = get.error(ecq)
  143. %ERROR Checks for errors on device. If any, throws a warning.
  144. %The function will pull all errors from the device error stack.
  145. %This is up to a maximum of 20 errors. If errors have occured
  146. %the function will throw a warning with all the error messages.
  147. for i = 1:20
  148. output = ecq.query_unsafe('SYSTem:ERRor?'); %Query error message from device.
  149. % [msgstr, msgid] = lastwarn;
  150. % if strcmp(msgid,'instrument:fscanf:unsuccessfulRead')
  151. % error(['Lost connection with ' ecq.name]);
  152. % end
  153. errorlist(i,1:(length(output))) = output; %Store the error message in the errorlist array.
  154. %GPIB protocol states that the error code '0' means no
  155. %error. The for loop will break if the last recieved error
  156. %code is zero.
  157. if str2double(regexp(output,'\d*','match','once'))==0 %Check if last recieved error code is '0'
  158. % If the 'no error' message is the only error then no
  159. % warning will be trown. However if there is more than
  160. % one error in the list. The function gives a warning
  161. % with all error messages.
  162. if i>1 %check for more than one error message.
  163. warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[]));
  164. end
  165. ecq.clear;
  166. break;
  167. end
  168. end
  169. end
  170. end
  171. methods% (Access = protected)
  172. function write_unsafe(ecq,message)
  173. %WRITE_UNSAFE Sends command to device.
  174. %This function does not check if device is ready or check if an
  175. %error occured after writing. If possible one should always use
  176. %the WRITE function.
  177. %
  178. %See also WRITE, QUERY
  179. if ecq.channel >= 0
  180. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  181. fprintf(['>> ++addr ', num2str(ecq.channel),'\n']);
  182. end
  183. fprintf(ecq.tcp, message);
  184. fprintf(['>> ',message,'\n']);
  185. end
  186. function write_noerror(ecq,message)
  187. %WRITE Sends a command to the channel.
  188. %The function will first check if the device is ready to
  189. %recieve a command via the OPC function. Then send the command.
  190. %
  191. %See also WRITE
  192. ecq.opc;
  193. ecq.write_unsafe(message);
  194. end
  195. function output = query_unsafe(ecq,message)
  196. %QUERY_UNSAFE Sends command to device and reads device output.
  197. %This function does not check if device is ready or check if an
  198. %error occured after writing. If possible one should always use
  199. %the QUERY function.
  200. %
  201. %See also QUERY, WRITE, READ
  202. ecq.write_unsafe(message);
  203. output = read(ecq);
  204. end
  205. end
  206. methods (Access = protected, Hidden)
  207. function setPrologix(ecq)
  208. fprintf(ecq.tcp, '++mode 1'); %set device in controller mode
  209. fprintf(ecq.tcp, '++auto 0'); %disable automatic datapull. this avoids errors on equipment.
  210. end
  211. function delete(ecq)
  212. %DELETE Destructs the current object.
  213. ecq.unlock;
  214. Equipment.getTCP(ecq.tcp.RemoteHost,-ecq.tcp.RemotePort);
  215. end
  216. end
  217. methods (Static)
  218. function tcpobject = getTCP(ipAddress,port)
  219. %GETTCP Returns TCP-object for TCP-connection.
  220. %Function makes a TCP-connection for specific ipAddress and
  221. %port. If TCP-connection already exist no new connection is
  222. %made and only existing handle is returned.
  223. %For existing connections the function will store the amount of
  224. %handles in use. Via the DELETE function the connection will be
  225. %closed if the connection is not in use anymore.
  226. %The connection will be removed if the port is negative number.
  227. persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls.
  228. if isempty(tcpconnection)
  229. %if the tcpconnection is empty (first time function
  230. %call) make a struct in the variable.
  231. tcpconnection = struct;
  232. end
  233. [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress.
  234. if port > 0 %if port number is positive a connection is made.
  235. if ~isfield(tcpconnection, ipname) %check if the handle is already made before.
  236. tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection
  237. tcpconnection.(ipname).nopen = 1; %Set number of connections in use to 1
  238. tcpconnection.(ipname).tcp.InputBufferSize = 2^24; %make really large buffer size 64MB. To acquire complete waveforms.
  239. tcpconnection.(ipname).tcp.Timeout = 5; %set timeout to 5 seconds.
  240. fopen(tcpconnection.(ipname).tcp); %Open the TCP-connection
  241. else %If connection already exist. Increase number of connections in use by 1.
  242. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  243. end
  244. tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle.
  245. elseif port < 0 %If the portnumber is negative the connection is removed.
  246. if ~isfield(tcpconnection, ipname)
  247. return;
  248. elseif tcpconnection.(ipname).nopen > 1
  249. %If more than one object uses this tcp-handle. Decrease
  250. %the number of connections in use by 1.
  251. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1.
  252. else
  253. %If only one handle uses this connection. The
  254. %connection is closed and the field is removed from the
  255. %tcpconnection struct.
  256. flushinput(tcpconnection.(ipname).tcp);
  257. fclose(tcpconnection.(ipname).tcp);
  258. tcpconnection = rmfield(tcpconnection,ipname);
  259. end
  260. else
  261. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  262. end
  263. end
  264. function bool = isnum(input)
  265. %ISNUM Tests if the input is numeric scalar
  266. bool = isnumeric(input)&&isscalar(input);
  267. end
  268. function num = forceNum(input)
  269. %FORCENUM Throws an error if the input is not numeric.
  270. if ~Equipment.isnum(input)
  271. error('Input should be a (single) number.');
  272. end
  273. num = input;
  274. end
  275. function iptest(ipAddress)
  276. %IPTEST Checks if the given IPv4-address is valid.
  277. validip = regexp(ipAddress,'^(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]))$', 'once');
  278. if isempty(validip)
  279. error('Invalid IP-address');
  280. end
  281. end
  282. function [structname,cleanip] = ip2structname(ipAddress,port)
  283. %IP2STRUCTNAME Returns a structname and ip w/out leading zeros.
  284. %structname to store stuff in struct (especially for GETTCP).
  285. %cleanip is a shortened ip without leading zeros.
  286. cleanip = regexprep(ipAddress,'(?:(?<=\.)|^)(?:0+)(?=\d)','');
  287. Equipment.iptest(cleanip);
  288. structname = matlab.lang.makeValidName(['ip',cleanip,'_',num2str(port)]);
  289. end
  290. end
  291. end