Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

102 lignes
3.2KB

  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 unlock(ecq)
  24. end
  25. function lock(ecq)
  26. end
  27. function delete(ecq)
  28. %zorgen dat het device geunlocked is!
  29. %ecq.unlock;
  30. Equipment.getTCP(ecq.tcp.RemoteHost,-1);
  31. end
  32. function write(ecq,message)
  33. if ecq.channel >= 0
  34. fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]);
  35. end
  36. fprintf(ecq.tcp, message);
  37. end
  38. function output = read(ecq)
  39. if ecq.channel >= 0
  40. fprintf(ecq.tcp,'++read');
  41. end
  42. output = fscanf(ecq.tcp);
  43. end
  44. function output = query(ecq,message)
  45. write(ecq,message);
  46. output = read(ecq);
  47. end
  48. function e = get.error(ecq)
  49. e = ecq.query('SYSTem:ERRor?');
  50. end
  51. end
  52. methods (Static)
  53. function tcpobject = getTCP(ipAddress,port)
  54. persistent tcpconnection;
  55. ipname = Equipment.ip2structname(ipAddress);
  56. if port > 0
  57. if isempty(tcpconnection)
  58. tcpconnection = struct;
  59. end
  60. if ~isfield(tcpconnection, ipname)
  61. tcpconnection.(ipname).tcp = tcpip(ipAddress,port);
  62. tcpconnection.(ipname).nopen = 1;
  63. fopen(tcpconnection.(ipname).tcp);
  64. else
  65. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  66. end
  67. tcpobject = tcpconnection.(ipname).tcp;
  68. elseif port == -1
  69. if tcpconnection.(ipname).nopen > 1
  70. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1;
  71. else
  72. fclose(tcpconnection.(ipname).tcp);
  73. tcpconnection = rmfield(tcpconnection,ipname);
  74. end
  75. else
  76. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  77. end
  78. end
  79. function iptest(ipAddress)
  80. 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');
  81. if isempty(validip)
  82. error('Invalid IP-address');
  83. end
  84. end
  85. function structname = ip2structname(ipAddress)
  86. Equipment.iptest(ipAddress);
  87. ipmatch = regexprep(ipAddress,'\.','_');
  88. structname = ['ip',ipmatch];
  89. end
  90. end
  91. end