您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

272 行
11KB

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