25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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