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

132 行
4.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. %lock
  9. end
  10. methods
  11. function ecq = Equipment(name,ipAddress,port,channel)
  12. ecq.name = name;
  13. ecq.tcp = Equipment.getTCP(ipAddress,port);
  14. ecq.channel = channel;
  15. end
  16. function idn(ecq)
  17. fprintf(ecq.tcp,['++addr ',num2str(ecq.channel)]);
  18. fprintf(ecq.tcp,'*idn?');
  19. fprintf(ecq.tcp,'++read');
  20. fprintf(fscanf(ecq.tcp));
  21. end
  22. function delete(ecq)
  23. %zorgen dat het device geunlocked is!
  24. Equipment.getTCP(ecq.tcp.RemoteHost,-1);
  25. end
  26. end
  27. methods (Static)
  28. function tcpobject = getTCP(ipAddress,port)
  29. persistent tcpconnection;
  30. ipname = Equipment.ip2structname(ipAddress);
  31. if port > 0
  32. if isempty(tcpconnection)
  33. tcpconnection = struct;
  34. end
  35. if ~isfield(tcpconnection, ipname)
  36. tcpconnection.(ipname).tcp = tcpip(ipAddress,port);
  37. tcpconnection.(ipname).nopen = 1;
  38. fopen(tcpconnection.(ipname).tcp);
  39. else
  40. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1;
  41. end
  42. tcpobject = tcpconnection.(ipname).tcp;
  43. elseif port == -1
  44. if tcpconnection.(ipname).nopen > 1
  45. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1;
  46. else
  47. fclose(tcpconnection.(ipname).tcp);
  48. tcpconnection = rmfield(tcpconnection,ipname);
  49. end
  50. else
  51. error([num2str(port),' is not a valid port number try a value between 1 and 65535.']);
  52. end
  53. end
  54. function iptest(ipAddress)
  55. 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');
  56. if isempty(validip)
  57. error('Invalid IP-address');
  58. end
  59. end
  60. function structname = ip2structname(ipAddress)
  61. Equipment.iptest(ipAddress);
  62. ipmatch = regexprep(ipAddress,'\.','_');
  63. structname = ['ip',ipmatch];
  64. end
  65. end
  66. end
  67. % classdef BankAccount < handle
  68. % properties (Access = ?AccountManager)
  69. % AccountStatus = 'open'
  70. % end
  71. % properties (SetAccess = private)
  72. % AccountNumber
  73. % AccountBalance
  74. % end
  75. % properties (Transient)
  76. % AccountListener
  77. % end
  78. % events
  79. % InsufficientFunds
  80. % end
  81. % methods
  82. % function BA = BankAccount(accNum,initBal)
  83. % BA.AccountNumber = accNum;
  84. % BA.AccountBalance = initBal;
  85. % BA.AccountListener = AccountManager.addAccount(BA);
  86. % end
  87. % function deposit(BA,amt)
  88. % BA.AccountBalance = BA.AccountBalance + amt;
  89. % if BA.AccountBalance > 0
  90. % BA.AccountStatus = 'open';
  91. % end
  92. % end
  93. % function withdraw(BA,amt)
  94. % if (strcmp(BA.AccountStatus,'closed')&& BA.AccountBalance <= 0)
  95. % disp(['Account ',num2str(BA.AccountNumber),' has been closed.'])
  96. % return
  97. % end
  98. % newbal = BA.AccountBalance - amt;
  99. % BA.AccountBalance = newbal;
  100. % if newbal < 0
  101. % notify(BA,'InsufficientFunds')
  102. % end
  103. % end
  104. % function getStatement(BA)
  105. % disp('-------------------------')
  106. % disp(['Account: ',num2str(BA.AccountNumber)])
  107. % ab = sprintf('%0.2f',BA.AccountBalance);
  108. % disp(['CurrentBalance: ',ab])
  109. % disp(['Account Status: ',BA.AccountStatus])
  110. % disp('-------------------------')
  111. % end
  112. % end
  113. % methods (Static)
  114. % function obj = loadobj(s)
  115. % if isstruct(s)
  116. % accNum = s.AccountNumber;
  117. % initBal = s.AccountBalance;
  118. % obj = BankAccount(accNum,initBal);
  119. % else
  120. % obj.AccountListener = AccountManager.addAccount(s);
  121. % end
  122. % end
  123. % end
  124. % end