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.

169 linhas
5.4KB

  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. if ecq.channel >= 0
  41. fprintf(ecq.tcp,'++loc');
  42. ecq.locked = false;
  43. end
  44. end
  45. function lock(ecq)
  46. if ecq.channel >= 0
  47. fprintf(ecq.tcp,'++llo');
  48. ecq.locked = true;
  49. else
  50. warning('Device does not support locking')
  51. end
  52. end
  53. function beep(ecq)
  54. ecq.write('SYST:BEEP')
  55. end
  56. function delete(ecq)
  57. ecq.unlock;
  58. Equipment.getTCP(ecq.tcp.RemoteHost,-1);
  59. end
  60. function write(ecq,message)
  61. ecq.opc;
  62. ecq.write_unsafe(message);
  63. ecq.error;
  64. end
  65. function write_unsafe(ecq,message)
  66. if ecq.channel >= 0
  67. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  68. end
  69. fprintf(ecq.tcp, message);
  70. end
  71. function output = read(ecq)
  72. if ecq.channel >= 0
  73. fprintf(ecq.tcp,'++read');
  74. end
  75. output = fscanf(ecq.tcp);
  76. end
  77. function output = query(ecq,message)
  78. ecq.opc;
  79. output = ecq.query_unsafe(message);
  80. ecq.error;
  81. end
  82. function output = query_unsafe(ecq,message)
  83. ecq.write_unsafe(message);
  84. output = read(ecq);
  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 (Static)
  102. function tcpobject = getTCP(ipAddress,port)
  103. persistent tcpconnection;
  104. ipname = Equipment.ip2structname(ipAddress);
  105. if port > 0
  106. if isempty(tcpconnection)
  107. tcpconnection = struct;
  108. end
  109. if ~isfield(tcpconnection, ipname)
  110. tcpconnection.(ipname).tcp = tcpip(ipAddress,port);
  111. tcpconnection.(ipname).nopen = 1;
  112. fopen(tcpconnection.(ipname).tcp);
  113. else
  114. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  115. end
  116. tcpobject = tcpconnection.(ipname).tcp;
  117. elseif port == -1
  118. if tcpconnection.(ipname).nopen > 1
  119. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1;
  120. else
  121. fclose(tcpconnection.(ipname).tcp);
  122. tcpconnection = rmfield(tcpconnection,ipname);
  123. end
  124. else
  125. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  126. end
  127. end
  128. function bool = isnum(input)
  129. bool = isnumeric(input)&&isscalar(input);
  130. end
  131. function num = forceNum(input)
  132. if ~isnumeric(input)
  133. error('Input should be a (single) number.');
  134. end
  135. num = input;
  136. end
  137. function iptest(ipAddress)
  138. 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');
  139. if isempty(validip)
  140. error('Invalid IP-address');
  141. end
  142. end
  143. function structname = ip2structname(ipAddress)
  144. Equipment.iptest(ipAddress);
  145. ipmatch = regexprep(ipAddress,'\.','_');
  146. structname = ['ip',ipmatch];
  147. end
  148. end
  149. end