|
|
|
@@ -103,28 +103,35 @@ classdef Equipment < handle |
|
|
|
end |
|
|
|
|
|
|
|
function data = bin_read(ecq,datalength,type,typesize) |
|
|
|
if nargin < 4 |
|
|
|
typesize = 8; |
|
|
|
end |
|
|
|
data = zeros(1,datalength); |
|
|
|
totalread = 0; |
|
|
|
maxlength = floor(ecq.tcp.InputBufferSize/typesize); |
|
|
|
while datalength > 0 |
|
|
|
readlength = min(datalength,maxlength); |
|
|
|
[lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type); |
|
|
|
if lastcount == 0 |
|
|
|
error('Data stream from device shorter than expected'); |
|
|
|
try |
|
|
|
if nargin < 4 |
|
|
|
typesize = 8; |
|
|
|
end |
|
|
|
data = zeros(1,datalength); |
|
|
|
totalread = 0; |
|
|
|
maxlength = floor(ecq.tcp.InputBufferSize/typesize)-16; |
|
|
|
while datalength > 0 |
|
|
|
readlength = min(datalength,maxlength); |
|
|
|
lastcount = 0; |
|
|
|
[lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type); |
|
|
|
if lastcount == 0 |
|
|
|
error('Data stream from device shorter than expected'); |
|
|
|
end |
|
|
|
data(totalread+1:totalread+lastcount) = lastdata; |
|
|
|
totalread = totalread+lastcount; |
|
|
|
datalength = datalength - lastcount; |
|
|
|
end |
|
|
|
data(totalread+1:totalread+lastcount) = lastdata; |
|
|
|
totalread = totalread+lastcount; |
|
|
|
datalength = datalength - lastcount; |
|
|
|
flushinput(ecq.tcp) |
|
|
|
ecq.clear |
|
|
|
catch ME |
|
|
|
flushinput(ecq.tcp); |
|
|
|
rethrow(ME); |
|
|
|
end |
|
|
|
flushinput(ecq.tcp) |
|
|
|
ecq.clear |
|
|
|
end |
|
|
|
|
|
|
|
function data = bin_read_float(ecq,datalength) |
|
|
|
data = bin_read(ecq,datalength,'float',4); |
|
|
|
data = bin_read(ecq,datalength,'float',4); |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
function output = read(ecq) |
|
|
|
@@ -243,13 +250,14 @@ classdef Equipment < handle |
|
|
|
%The connection will be removed if the port is negative number. |
|
|
|
|
|
|
|
persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls. |
|
|
|
[ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress. |
|
|
|
if port > 0 %if port number is positive a connection is made. |
|
|
|
if isempty(tcpconnection) |
|
|
|
if isempty(tcpconnection) |
|
|
|
%if the tcpconnection is empty (first time function |
|
|
|
%call) make a struct in the variable. |
|
|
|
tcpconnection = struct; |
|
|
|
end |
|
|
|
[ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress. |
|
|
|
if port > 0 %if port number is positive a connection is made. |
|
|
|
|
|
|
|
|
|
|
|
if ~isfield(tcpconnection, ipname) %check if the handle is already made before. |
|
|
|
tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection |
|
|
|
@@ -262,7 +270,9 @@ classdef Equipment < handle |
|
|
|
end |
|
|
|
tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle. |
|
|
|
elseif port < 0 %If the portnumber is negative the connection is removed. |
|
|
|
if tcpconnection.(ipname).nopen > 1 |
|
|
|
if ~isfield(tcpconnection, ipname) |
|
|
|
return; |
|
|
|
elseif tcpconnection.(ipname).nopen > 1 |
|
|
|
%If more than one object uses this tcp-handle. Decrease |
|
|
|
%the number of connections in use by 1. |
|
|
|
tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1. |
|
|
|
@@ -270,7 +280,7 @@ classdef Equipment < handle |
|
|
|
%If only one handle uses this connection. The |
|
|
|
%connection is closed and the field is removed from the |
|
|
|
%tcpconnection struct. |
|
|
|
flushinput(sc.tcp); |
|
|
|
flushinput(tcpconnection.(ipname).tcp); |
|
|
|
fclose(tcpconnection.(ipname).tcp); |
|
|
|
tcpconnection = rmfield(tcpconnection,ipname); |
|
|
|
end |
|
|
|
|