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.

251 lines
11KB

  1. classdef Equipment < handle
  2. %EQUIPMENT Summary of this class goes here
  3. % Detailed explanation goes here
  4. properties (SetAccess=private)
  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. ecq.write_unsafe('*cls');
  35. end
  36. function opc(ecq)
  37. %OPC executes 'operation complete query' to device.
  38. %Function holds untill device returns '1'. Must be
  39. %used to avoid interrupting busy device.
  40. ecq.query_unsafe('*OPC?');
  41. end
  42. function unlock(ecq)
  43. %UNLOCK Sets the device back to 'local control'
  44. %This function only supports devices via the Prologix.
  45. if ecq.channel >= 0
  46. fprintf(ecq.tcp,'++loc');
  47. ecq.locked = false;
  48. end
  49. end
  50. function lock(ecq)
  51. %UNLOCK Sets the device back to 'remote control'
  52. %This function only supports devices via the Prologix.
  53. if ecq.channel >= 0
  54. fprintf(ecq.tcp,'++llo');
  55. ecq.locked = true;
  56. else
  57. warning('Device does not support locking')
  58. end
  59. end
  60. function beep(ecq)
  61. %BEEP Sends the beep command to the device
  62. ecq.write('SYST:BEEP')
  63. end
  64. function write(ecq,message)
  65. %WRITE Sends a command to the channel.
  66. %The function will first check if the device is ready to
  67. %recieve a command via the OPC function. Then send the command
  68. %and eventually check if the device has thrown an error via
  69. %the ERROR function.
  70. %
  71. %See also QUERY
  72. ecq.opc;
  73. ecq.write_unsafe(message);
  74. ecq.error;
  75. end
  76. function output = read(ecq)
  77. %READ Extracts recieved data from the TCP-buffer.
  78. %This function sends the '++read'-command to the Prologix if
  79. %needed. Will always read the TCP-buffer in matlab.
  80. if ecq.channel >= 0
  81. ecq.write_unsafe('++read');
  82. end
  83. output = fscanf(ecq.tcp);
  84. fprintf(['<< ',output]);
  85. end
  86. function output = query(ecq,message)
  87. %QUERY Sends command to the device and read the respond.
  88. %The command is send via the WRITE and the respond is collected
  89. %from the device.
  90. %
  91. %See also WRITE, READ
  92. ecq.opc;
  93. output = ecq.query_unsafe(message);
  94. ecq.error;
  95. end
  96. function errorlist = get.error(ecq)
  97. %ERROR Checks for errors on device. If any, throws a warning.
  98. %The function will pull all errors from the device error stack.
  99. %This is up to a maximum of 20 errors. If errors have occured
  100. %the function will throw a warning with all the error messages.
  101. for i = 1:20
  102. output = ecq.query_unsafe('SYSTem:ERRor?'); %Query error message from device.
  103. errorlist(i,1:(length(output))) = output; %Store the error message in the errorlist array.
  104. %GPIB protocol states that the error code '0' means no
  105. %error. The for loop will break if the last recieved error
  106. %code is zero.
  107. if str2double(regexp(output,'\d*','match','once'))==0 %Check if last recieved error code is '0'
  108. % If the 'no error' message is the only error then no
  109. % warning will be trown. However if there is more than
  110. % one error in the list. The function gives a warning
  111. % with all error messages.
  112. if i>1 %check for more than one error message.
  113. warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[]));
  114. end
  115. ecq.clear;
  116. break;
  117. end
  118. end
  119. end
  120. end
  121. methods (Access = protected)
  122. function write_unsafe(ecq,message)
  123. %WRITE_UNSAFE Sends command to device.
  124. %This function does not check if device is ready or check if an
  125. %error occured after writing. If possible one should always use
  126. %the WRITE function.
  127. %
  128. %See also WRITE, QUERY
  129. if ecq.channel >= 0
  130. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  131. fprintf(['>> ++addr ', num2str(ecq.channel),'\n']);
  132. end
  133. fprintf(ecq.tcp, message);
  134. fprintf(['>> ',message,'\n']);
  135. end
  136. function output = query_unsafe(ecq,message)
  137. %QUERY_UNSAFE Sends command to device and reads device output.
  138. %This function does not check if device is ready or check if an
  139. %error occured after writing. If possible one should always use
  140. %the QUERY function.
  141. %
  142. %See also QUERY, WRITE, READ
  143. ecq.write_unsafe(message);
  144. output = read(ecq);
  145. end
  146. end
  147. methods (Access = protected, Hidden)
  148. function setPrologix(ecq)
  149. fprintf(ecq.tcp, '++mode 1'); %set device in controller mode
  150. fprintf(ecq.tcp, '++auto 0'); %disable automatic datapull. this avoids errors on equipment.
  151. end
  152. function delete(ecq)
  153. %DELETE Destructs the current object.
  154. ecq.unlock;
  155. Equipment.getTCP(ecq.tcp.RemoteHost,-ecq.tcp.RemotePort);
  156. end
  157. end
  158. methods (Static)
  159. function tcpobject = getTCP(ipAddress,port)
  160. %GETTCP Returns TCP-object for TCP-connection.
  161. %Function makes a TCP-connection for specific ipAddress and
  162. %port. If TCP-connection already exist no new connection is
  163. %made and only existing handle is returned.
  164. %For existing connections the function will store the amount of
  165. %handles in use. Via the DELETE function the connection will be
  166. %closed if the connection is not in use anymore.
  167. %The connection will be removed if the port is negative number.
  168. persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls.
  169. [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress.
  170. if port > 0 %if port number is positive a connection is made.
  171. if isempty(tcpconnection)
  172. %if the tcpconnection is empty (first time function
  173. %call) make a struct in the variable.
  174. tcpconnection = struct;
  175. end
  176. if ~isfield(tcpconnection, ipname) %check if the handle is already made before.
  177. tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection
  178. tcpconnection.(ipname).nopen = 1; %Set number of connections in use to 1
  179. fopen(tcpconnection.(ipname).tcp); %Open the TCP-connection
  180. else %If connection already exist. Increase number of connections in use by 1.
  181. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  182. end
  183. tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle.
  184. elseif port < 0 %If the portnumber is negative the connection is removed.
  185. if tcpconnection.(ipname).nopen > 1
  186. %If more than one object uses this tcp-handle. Decrease
  187. %the number of connections in use by 1.
  188. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1.
  189. else
  190. %If only one handle uses this connection. The
  191. %connection is closed and the field is removed from the
  192. %tcpconnection struct.
  193. fclose(tcpconnection.(ipname).tcp);
  194. tcpconnection = rmfield(tcpconnection,ipname);
  195. end
  196. else
  197. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  198. end
  199. end
  200. function bool = isnum(input)
  201. %ISNUM Tests if the input is numeric scalar
  202. bool = isnumeric(input)&&isscalar(input);
  203. end
  204. function num = forceNum(input)
  205. %FORCENUM Throws an error if the input is not numeric.
  206. if ~Equipment.isnum(input)
  207. error('Input should be a (single) number.');
  208. end
  209. num = input;
  210. end
  211. function iptest(ipAddress)
  212. %IPTEST Checks if the given IPv4-address is valid.
  213. 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');
  214. if isempty(validip)
  215. error('Invalid IP-address');
  216. end
  217. end
  218. function [structname,cleanip] = ip2structname(ipAddress,port)
  219. %IP2STRUCTNAME Returns a structname and ip w/out leading zeros.
  220. %structname to store stuff in struct (especially for GETTCP).
  221. %cleanip is a shortened ip without leading zeros.
  222. cleanip = regexprep(ipAddress,'(?:(?<=\.)|^)(?:0+)(?=\d)','');
  223. Equipment.iptest(cleanip);
  224. structname = matlab.lang.makeValidName(['ip',cleanip,'_',num2str(port)]);
  225. end
  226. end
  227. end