Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

187 linhas
6.3KB

  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. end
  25. function id = idn(ecq)
  26. %IDN Queries identificantion from device via '*IDN?'.
  27. id = ecq.query_unsafe('*idn?');
  28. end
  29. function clear(ecq)
  30. %CLEAR Sends clear command to device via '*CLS'.
  31. ecq.write_unsafe('*cls');
  32. end
  33. function opc(ecq)
  34. %OPC executes 'operation complete query' to device.
  35. %Function holds untill device returns '1'. Must be
  36. %used to avoid interrupting busy device.
  37. ecq.query_unsafe('*OPC?');
  38. end
  39. function unlock(ecq)
  40. %UNLOCK Sets the device back to 'local control'
  41. %This function only supports devices via the Prologix.
  42. if ecq.channel >= 0
  43. fprintf(ecq.tcp,'++loc');
  44. ecq.locked = false;
  45. end
  46. end
  47. function lock(ecq)
  48. %UNLOCK Sets the device back to 'remote control'
  49. %This function only supports devices via the Prologix.
  50. if ecq.channel >= 0
  51. fprintf(ecq.tcp,'++llo');
  52. ecq.locked = true;
  53. else
  54. warning('Device does not support locking')
  55. end
  56. end
  57. function beep(ecq)
  58. %BEEP Sends the beep command to the device
  59. ecq.write('SYST:BEEP')
  60. end
  61. function write(ecq,message)
  62. %WRITE Sends a command to the channel.
  63. %The function will first check if the device is ready to
  64. %recieve a command via the OPC function. Then send the command
  65. %and eventually check if the device has thrown an error via
  66. %the ERROR function.
  67. %
  68. %See also QUERY
  69. ecq.opc;
  70. ecq.write_unsafe(message);
  71. ecq.error;
  72. end
  73. function output = read(ecq)
  74. %READ Extracts recieved data from the TCP-buffer.
  75. %This
  76. if ecq.channel >= 0
  77. fprintf(ecq.tcp,'++read');
  78. end
  79. output = fscanf(ecq.tcp);
  80. end
  81. function output = query(ecq,message)
  82. ecq.opc;
  83. output = ecq.query_unsafe(message);
  84. ecq.error;
  85. end
  86. function errorlist = get.error(ecq)
  87. for i = 1:20
  88. output = ecq.query_unsafe('SYSTem:ERRor?');
  89. errorlist(i,1:(length(output))) = output;
  90. %errorcode = regexp(output,'\d*','match','once');
  91. if str2double(regexp(output,'\d*','match','once'))==0
  92. if i>1
  93. warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[]));
  94. ecq.clear;
  95. end
  96. break;
  97. end
  98. end
  99. end
  100. end
  101. methods (Access = protected, Hidden)
  102. function write_unsafe(ecq,message)
  103. if ecq.channel >= 0
  104. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  105. end
  106. fprintf(ecq.tcp, message);
  107. end
  108. function output = query_unsafe(ecq,message)
  109. ecq.write_unsafe(message);
  110. output = read(ecq);
  111. end
  112. function delete(ecq)
  113. %DELETE Destructs the current object.
  114. ecq.unlock;
  115. Equipment.getTCP(ecq.tcp.RemoteHost,-1);
  116. end
  117. end
  118. methods (Static)
  119. function tcpobject = getTCP(ipAddress,port)
  120. persistent tcpconnection;
  121. ipname = Equipment.ip2structname(ipAddress);
  122. if port > 0
  123. if isempty(tcpconnection)
  124. tcpconnection = struct;
  125. end
  126. if ~isfield(tcpconnection, ipname)
  127. tcpconnection.(ipname).tcp = tcpip(ipAddress,port);
  128. tcpconnection.(ipname).nopen = 1;
  129. fopen(tcpconnection.(ipname).tcp);
  130. else
  131. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  132. end
  133. tcpobject = tcpconnection.(ipname).tcp;
  134. elseif port == -1
  135. if tcpconnection.(ipname).nopen > 1
  136. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1;
  137. else
  138. fclose(tcpconnection.(ipname).tcp);
  139. tcpconnection = rmfield(tcpconnection,ipname);
  140. end
  141. else
  142. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  143. end
  144. end
  145. function bool = isnum(input)
  146. bool = isnumeric(input)&&isscalar(input);
  147. end
  148. function num = forceNum(input)
  149. if ~isnumeric(input)
  150. error('Input should be a (single) number.');
  151. end
  152. num = input;
  153. end
  154. function iptest(ipAddress)
  155. 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');
  156. if isempty(validip)
  157. error('Invalid IP-address');
  158. end
  159. end
  160. function structname = ip2structname(ipAddress)
  161. Equipment.iptest(ipAddress);
  162. ipmatch = regexprep(ipAddress,'\.','_');
  163. structname = ['ip',ipmatch];
  164. end
  165. end
  166. end