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.

312 lines
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. if nargin < 4
  95. typesize = 8;
  96. end
  97. data = zeros(1,datalength);
  98. totalread = 0;
  99. maxlength = floor(ecq.tcp.InputBufferSize/typesize);
  100. while datalength > 0
  101. readlength = min(datalength,maxlength);
  102. [lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type);
  103. if lastcount == 0
  104. error('Data stream from device shorter than expected');
  105. end
  106. data(totalread+1:totalread+lastcount) = lastdata;
  107. totalread = totalread+lastcount;
  108. datalength = datalength - lastcount;
  109. end
  110. flushinput(ecq.tcp)
  111. ecq.clear
  112. end
  113. function data = bin_read_float(ecq,datalength)
  114. data = bin_read(ecq,datalength,'float',4);
  115. end
  116. function output = read(ecq)
  117. %READ Extracts recieved data from the TCP-buffer.
  118. %This function sends the '++read'-command to the Prologix if
  119. %needed. Will always read the TCP-buffer in matlab.
  120. if ecq.channel >= 0
  121. ecq.write_unsafe('++read');
  122. end
  123. output = fscanf(ecq.tcp);
  124. fprintf(['<< ',output]);
  125. end
  126. function output = query(ecq,message)
  127. %QUERY Sends command to the device and read the respond.
  128. %The command is send via the WRITE and the respond is collected
  129. %from the device.
  130. %
  131. %See also WRITE, READ
  132. ecq.opc;
  133. output = ecq.query_unsafe(message);
  134. ecq.error;
  135. end
  136. function errorlist = get.error(ecq)
  137. %ERROR Checks for errors on device. If any, throws a warning.
  138. %The function will pull all errors from the device error stack.
  139. %This is up to a maximum of 20 errors. If errors have occured
  140. %the function will throw a warning with all the error messages.
  141. for i = 1:20
  142. output = ecq.query_unsafe('SYSTem:ERRor?'); %Query error message from device.
  143. % [msgstr, msgid] = lastwarn;
  144. % if strcmp(msgid,'instrument:fscanf:unsuccessfulRead')
  145. % error(['Lost connection with ' ecq.name]);
  146. % end
  147. errorlist(i,1:(length(output))) = output; %Store the error message in the errorlist array.
  148. %GPIB protocol states that the error code '0' means no
  149. %error. The for loop will break if the last recieved error
  150. %code is zero.
  151. if str2double(regexp(output,'\d*','match','once'))==0 %Check if last recieved error code is '0'
  152. % If the 'no error' message is the only error then no
  153. % warning will be trown. However if there is more than
  154. % one error in the list. The function gives a warning
  155. % with all error messages.
  156. if i>1 %check for more than one error message.
  157. warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[]));
  158. end
  159. ecq.clear;
  160. break;
  161. end
  162. end
  163. end
  164. end
  165. methods% (Access = protected)
  166. function write_unsafe(ecq,message)
  167. %WRITE_UNSAFE Sends command to device.
  168. %This function does not check if device is ready or check if an
  169. %error occured after writing. If possible one should always use
  170. %the WRITE function.
  171. %
  172. %See also WRITE, QUERY
  173. if ecq.channel >= 0
  174. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  175. fprintf(['>> ++addr ', num2str(ecq.channel),'\n']);
  176. end
  177. fprintf(ecq.tcp, message);
  178. fprintf(['>> ',message,'\n']);
  179. end
  180. function write_noerror(ecq,message)
  181. %WRITE Sends a command to the channel.
  182. %The function will first check if the device is ready to
  183. %recieve a command via the OPC function. Then send the command.
  184. %
  185. %See also WRITE
  186. ecq.opc;
  187. ecq.write_unsafe(message);
  188. end
  189. function output = query_unsafe(ecq,message)
  190. %QUERY_UNSAFE Sends command to device and reads device output.
  191. %This function does not check if device is ready or check if an
  192. %error occured after writing. If possible one should always use
  193. %the QUERY function.
  194. %
  195. %See also QUERY, WRITE, READ
  196. ecq.write_unsafe(message);
  197. output = read(ecq);
  198. end
  199. end
  200. methods (Access = protected, Hidden)
  201. function setPrologix(ecq)
  202. fprintf(ecq.tcp, '++mode 1'); %set device in controller mode
  203. fprintf(ecq.tcp, '++auto 0'); %disable automatic datapull. this avoids errors on equipment.
  204. end
  205. function delete(ecq)
  206. %DELETE Destructs the current object.
  207. ecq.unlock;
  208. Equipment.getTCP(ecq.tcp.RemoteHost,-ecq.tcp.RemotePort);
  209. end
  210. end
  211. methods (Static)
  212. function tcpobject = getTCP(ipAddress,port)
  213. %GETTCP Returns TCP-object for TCP-connection.
  214. %Function makes a TCP-connection for specific ipAddress and
  215. %port. If TCP-connection already exist no new connection is
  216. %made and only existing handle is returned.
  217. %For existing connections the function will store the amount of
  218. %handles in use. Via the DELETE function the connection will be
  219. %closed if the connection is not in use anymore.
  220. %The connection will be removed if the port is negative number.
  221. persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls.
  222. [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress.
  223. if port > 0 %if port number is positive a connection is made.
  224. if isempty(tcpconnection)
  225. %if the tcpconnection is empty (first time function
  226. %call) make a struct in the variable.
  227. tcpconnection = struct;
  228. end
  229. if ~isfield(tcpconnection, ipname) %check if the handle is already made before.
  230. tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection
  231. tcpconnection.(ipname).nopen = 1; %Set number of connections in use to 1
  232. tcpconnection.(ipname).tcp.InputBufferSize = 2^24; %make really large buffer size 64MB. To acquire complete waveforms.
  233. tcpconnection.(ipname).tcp.Timeout = 5; %set timeout to 5 seconds.
  234. fopen(tcpconnection.(ipname).tcp); %Open the TCP-connection
  235. else %If connection already exist. Increase number of connections in use by 1.
  236. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  237. end
  238. tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle.
  239. elseif port < 0 %If the portnumber is negative the connection is removed.
  240. if tcpconnection.(ipname).nopen > 1
  241. %If more than one object uses this tcp-handle. Decrease
  242. %the number of connections in use by 1.
  243. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1.
  244. else
  245. %If only one handle uses this connection. The
  246. %connection is closed and the field is removed from the
  247. %tcpconnection struct.
  248. flushinput(sc.tcp);
  249. fclose(tcpconnection.(ipname).tcp);
  250. tcpconnection = rmfield(tcpconnection,ipname);
  251. end
  252. else
  253. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  254. end
  255. end
  256. function bool = isnum(input)
  257. %ISNUM Tests if the input is numeric scalar
  258. bool = isnumeric(input)&&isscalar(input);
  259. end
  260. function num = forceNum(input)
  261. %FORCENUM Throws an error if the input is not numeric.
  262. if ~Equipment.isnum(input)
  263. error('Input should be a (single) number.');
  264. end
  265. num = input;
  266. end
  267. function iptest(ipAddress)
  268. %IPTEST Checks if the given IPv4-address is valid.
  269. 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');
  270. if isempty(validip)
  271. error('Invalid IP-address');
  272. end
  273. end
  274. function [structname,cleanip] = ip2structname(ipAddress,port)
  275. %IP2STRUCTNAME Returns a structname and ip w/out leading zeros.
  276. %structname to store stuff in struct (especially for GETTCP).
  277. %cleanip is a shortened ip without leading zeros.
  278. cleanip = regexprep(ipAddress,'(?:(?<=\.)|^)(?:0+)(?=\d)','');
  279. Equipment.iptest(cleanip);
  280. structname = matlab.lang.makeValidName(['ip',cleanip,'_',num2str(port)]);
  281. end
  282. end
  283. end