Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

132 řádky
4.0KB

  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. ecq.tcp = Equipment.getTCP(ipAddress,port);
  16. ecq.locked = false;
  17. ecq.channel = channel;
  18. ecq.name = ecq.idn();
  19. end
  20. function id = idn(ecq)
  21. id = ecq.query('*idn?');
  22. end
  23. function clear(ecq)
  24. ecq.write('*cls');
  25. end
  26. function opc(ecq)
  27. ecq.query('*OPC?');
  28. end
  29. function unlock(ecq)
  30. if ecq.channel >= 0
  31. fprintf(ecq.tcp,'++loc');
  32. ecq.locked = false;
  33. else
  34. warning('Device does not support unlocking')
  35. end
  36. end
  37. function lock(ecq)
  38. if ecq.channel >= 0
  39. fprintf(ecq.tcp,'++llo');
  40. ecq.locked = true;
  41. else
  42. warning('Device does not support locking')
  43. end
  44. end
  45. function beep(ecq)
  46. ecq.write('SYST:BEEP')
  47. end
  48. function delete(ecq)
  49. %zorgen dat het device geunlocked is!
  50. %ecq.unlock;
  51. Equipment.getTCP(ecq.tcp.RemoteHost,-1);
  52. end
  53. function write(ecq,message)
  54. if ecq.channel >= 0
  55. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  56. end
  57. fprintf(ecq.tcp, message);
  58. end
  59. function output = read(ecq)
  60. if ecq.channel >= 0
  61. fprintf(ecq.tcp,'++read');
  62. end
  63. output = fscanf(ecq.tcp);
  64. end
  65. function output = query(ecq,message)
  66. write(ecq,message);
  67. output = read(ecq);
  68. end
  69. function e = get.error(ecq)
  70. e = ecq.query('SYSTem:ERRor?');
  71. end
  72. end
  73. methods (Static)
  74. function tcpobject = getTCP(ipAddress,port)
  75. persistent tcpconnection;
  76. ipname = Equipment.ip2structname(ipAddress);
  77. if port > 0
  78. if isempty(tcpconnection)
  79. tcpconnection = struct;
  80. end
  81. if ~isfield(tcpconnection, ipname)
  82. tcpconnection.(ipname).tcp = tcpip(ipAddress,port);
  83. tcpconnection.(ipname).nopen = 1;
  84. fopen(tcpconnection.(ipname).tcp);
  85. else
  86. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  87. end
  88. tcpobject = tcpconnection.(ipname).tcp;
  89. elseif port == -1
  90. if tcpconnection.(ipname).nopen > 1
  91. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1;
  92. else
  93. fclose(tcpconnection.(ipname).tcp);
  94. tcpconnection = rmfield(tcpconnection,ipname);
  95. end
  96. else
  97. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  98. end
  99. end
  100. function bool = isnum(input)
  101. bool = isnumeric(input)&&isscalar(input);
  102. end
  103. function iptest(ipAddress)
  104. 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');
  105. if isempty(validip)
  106. error('Invalid IP-address');
  107. end
  108. end
  109. function structname = ip2structname(ipAddress)
  110. Equipment.iptest(ipAddress);
  111. ipmatch = regexprep(ipAddress,'\.','_');
  112. structname = ['ip',ipmatch];
  113. end
  114. end
  115. end