From 094e45f12f8e626f9770d427d553c6298f767b5a Mon Sep 17 00:00:00 2001 From: Wouter Horlings Date: Tue, 14 Nov 2017 12:20:34 +0100 Subject: [PATCH] IP cleaning toegevoegd. Port ondersteuning. veel comments --- OOequipment/Equipment.m | 51 +++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/OOequipment/Equipment.m b/OOequipment/Equipment.m index 1022da4..2b54948 100644 --- a/OOequipment/Equipment.m +++ b/OOequipment/Equipment.m @@ -90,29 +90,48 @@ classdef Equipment < handle end 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; output = ecq.query_unsafe(message); ecq.error; end 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 - 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,[])); - ecq.clear; end + ecq.clear; break; end end end end - methods (Access = protected, Hidden) + methods (Access = protected) 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 fprintf(ecq.tcp,['++addr ', num2str(ecq.channel)]); end @@ -123,11 +142,13 @@ classdef Equipment < handle ecq.write_unsafe(message); output = read(ecq); end + end + methods (Access = protected, Hidden) function delete(ecq) %DELETE Destructs the current object. ecq.unlock; - Equipment.getTCP(ecq.tcp.RemoteHost,-1); + Equipment.getTCP(ecq.tcp.RemoteHost,-ecp.tcp.RemotePort); end end @@ -135,8 +156,8 @@ classdef Equipment < handle methods (Static) function tcpobject = getTCP(ipAddress,port) persistent tcpconnection; - ipname = Equipment.ip2structname(ipAddress); - if port > 0 + [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); + if port > 0 if isempty(tcpconnection) tcpconnection = struct; end @@ -149,7 +170,7 @@ classdef Equipment < handle tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen + 1; end tcpobject = tcpconnection.(ipname).tcp; - elseif port == -1 + elseif port < 0 if tcpconnection.(ipname).nopen > 1 tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; else @@ -178,10 +199,10 @@ classdef Equipment < handle error('Invalid IP-address'); 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 \ No newline at end of file