classdef Equipment < handle %EQUIPMENT Summary of this class goes here % Detailed explanation goes here properties (SetAccess=private) name tcp channel locked end properties (Dependent, SetAccess=private) error end methods function ecq = Equipment(ipAddress,port,channel) ecq.tcp = Equipment.getTCP(ipAddress,port); ecq.locked = false; ecq.channel = channel; ecq.name = ecq.idn(); end function id = idn(ecq) id = ecq.query('*idn?'); end function unlock(ecq) end function lock(ecq) end function delete(ecq) %zorgen dat het device geunlocked is! %ecq.unlock; Equipment.getTCP(ecq.tcp.RemoteHost,-1); end function write(ecq,message) if ecq.channel >= 0 fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]); end fprintf(ecq.tcp, message); end function output = read(ecq) if ecq.channel >= 0 fprintf(ecq.tcp,'++read'); end output = fscanf(ecq.tcp); end function output = query(ecq,message) write(ecq,message); output = read(ecq); end function e = get.error(ecq) e = ecq.query('SYSTem:ERRor?'); end end methods (Static) function tcpobject = getTCP(ipAddress,port) persistent tcpconnection; ipname = Equipment.ip2structname(ipAddress); if port > 0 if isempty(tcpconnection) tcpconnection = struct; end if ~isfield(tcpconnection, ipname) tcpconnection.(ipname).tcp = tcpip(ipAddress,port); tcpconnection.(ipname).nopen = 1; fopen(tcpconnection.(ipname).tcp); else tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1; end tcpobject = tcpconnection.(ipname).tcp; elseif port == -1 if tcpconnection.(ipname).nopen > 1 tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; else fclose(tcpconnection.(ipname).tcp); tcpconnection = rmfield(tcpconnection,ipname); end else error([num2str(port),' is not a valid port number try a value between 1 and 65535.']); end end function iptest(ipAddress) 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'); if isempty(validip) error('Invalid IP-address'); end end function structname = ip2structname(ipAddress) Equipment.iptest(ipAddress); ipmatch = regexprep(ipAddress,'\.','_'); structname = ['ip',ipmatch]; end end end