| @@ -90,29 +90,48 @@ classdef Equipment < handle | |||||
| end | end | ||||
| function output = query(ecq,message) | function output = query(ecq,message) | ||||
| %QUERY Sends command to the device and read the respond. | |||||
| %The command is send via the WRITE and the respond is collected | |||||
| % | |||||
| ecq.opc; | ecq.opc; | ||||
| output = ecq.query_unsafe(message); | output = ecq.query_unsafe(message); | ||||
| ecq.error; | ecq.error; | ||||
| end | end | ||||
| function errorlist = get.error(ecq) | function errorlist = get.error(ecq) | ||||
| %ERROR Checks for errors on device. If any, throws a warning. | |||||
| %The function will pull all errors from the device error stack. | |||||
| %This is up to a maximum of 20 errors. If errors have occured | |||||
| %the function will throw a warning with all the error messages. | |||||
| for i = 1:20 | for i = 1:20 | ||||
| output = ecq.query_unsafe('SYSTem:ERRor?'); | |||||
| errorlist(i,1:(length(output))) = output; | |||||
| %errorcode = regexp(output,'\d*','match','once'); | |||||
| if str2double(regexp(output,'\d*','match','once'))==0 | |||||
| if i>1 | |||||
| output = ecq.query_unsafe('SYSTem:ERRor?'); %Query error message from device. | |||||
| errorlist(i,1:(length(output))) = output; %Store the error message in the errorlist array. | |||||
| %GPIB protocol states that the error code '0' means no | |||||
| %error. The for loop will break if the last recieved error | |||||
| %code is zero. | |||||
| if str2double(regexp(output,'\d*','match','once'))==0 %Check if last recieved error code is '0' | |||||
| % If the 'no error' message is the only error then no | |||||
| % warning will be trown. However if there is more than | |||||
| % one error in the list. The function gives a warning | |||||
| % with all error messages. | |||||
| if i>1 %check for more than one error message. | |||||
| warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[])); | warning('Error from device: %s %s',ecq.name,reshape(errorlist(1:i-1,:)',1,[])); | ||||
| ecq.clear; | |||||
| end | end | ||||
| ecq.clear; | |||||
| break; | break; | ||||
| end | end | ||||
| end | end | ||||
| end | end | ||||
| end | end | ||||
| methods (Access = protected, Hidden) | |||||
| methods (Access = protected) | |||||
| function write_unsafe(ecq,message) | function write_unsafe(ecq,message) | ||||
| %WRITE_UNSAFE Sends command to device. | |||||
| %This function does not check if device is ready or check if an | |||||
| %error occured after writing. If possible one should always use | |||||
| %the WRITE function. | |||||
| % | |||||
| %See also WRITE | |||||
| if ecq.channel >= 0 | if ecq.channel >= 0 | ||||
| fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]); | fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]); | ||||
| end | end | ||||
| @@ -123,11 +142,13 @@ classdef Equipment < handle | |||||
| ecq.write_unsafe(message); | ecq.write_unsafe(message); | ||||
| output = read(ecq); | output = read(ecq); | ||||
| end | end | ||||
| end | |||||
| methods (Access = protected, Hidden) | |||||
| function delete(ecq) | function delete(ecq) | ||||
| %DELETE Destructs the current object. | %DELETE Destructs the current object. | ||||
| ecq.unlock; | ecq.unlock; | ||||
| Equipment.getTCP(ecq.tcp.RemoteHost,-1); | |||||
| Equipment.getTCP(ecq.tcp.RemoteHost,-ecp.tcp.RemotePort); | |||||
| end | end | ||||
| end | end | ||||
| @@ -135,8 +156,8 @@ classdef Equipment < handle | |||||
| methods (Static) | methods (Static) | ||||
| function tcpobject = getTCP(ipAddress,port) | function tcpobject = getTCP(ipAddress,port) | ||||
| persistent tcpconnection; | persistent tcpconnection; | ||||
| ipname = Equipment.ip2structname(ipAddress); | |||||
| if port > 0 | |||||
| [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); | |||||
| if port > 0 | |||||
| if isempty(tcpconnection) | if isempty(tcpconnection) | ||||
| tcpconnection = struct; | tcpconnection = struct; | ||||
| end | end | ||||
| @@ -149,7 +170,7 @@ classdef Equipment < handle | |||||
| tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1; | tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1; | ||||
| end | end | ||||
| tcpobject = tcpconnection.(ipname).tcp; | tcpobject = tcpconnection.(ipname).tcp; | ||||
| elseif port == -1 | |||||
| elseif port < 0 | |||||
| if tcpconnection.(ipname).nopen > 1 | if tcpconnection.(ipname).nopen > 1 | ||||
| tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; | tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; | ||||
| else | else | ||||
| @@ -178,10 +199,10 @@ classdef Equipment < handle | |||||
| error('Invalid IP-address'); | error('Invalid IP-address'); | ||||
| end | end | ||||
| end | end | ||||
| function structname = ip2structname(ipAddress) | |||||
| Equipment.iptest(ipAddress); | |||||
| ipmatch = regexprep(ipAddress,'\.','_'); | |||||
| structname = ['ip',ipmatch]; | |||||
| function [structname,cleanIP] = ip2structname(ipAddress,port) | |||||
| cleanIP = regexprep(ipAddress,'(?:(?<=\.)|^)(?:0+)(?=\d)',''); | |||||
| Equipment.iptest(cleanIP); | |||||
| structname = matlab.lang.makeValidName(['ip',cleanIP,'_',num2str(port)]); | |||||
| end | end | ||||
| end | end | ||||
| end | end | ||||