Building Controls Virtual Test Bed
exchangeDoublesWithSocket.m
Go to the documentation of this file.
1 function [retVal, flaRea, simTimRea, dblValRea ] = ...
2  exchangeDoublesWithSocket(sockfd, flaWri, nDblRea, simTimWri, dblValWri)
3 % EXCHANGEDOUBLESWITHSOCKET - Exchanges data with the BCVTB
4 %
5 % [retVal, flaRea, simTimRea, dblValRea] = exchangeDoublesWithSocket( ...
6 % sockfd, flaWri, nDblRea, simTimWri, dblValWri);
7 % exchanges data with the BCVTB.
8 %
9 % The input arguments are:
10 % sockfd - Socket file descripter
11 % flaWri - Communication flag to write to the socket stream. Set to zero
12 % for normal operation, or to a negative value to stop
13 % the exchange.
14 % nDblRea - Number of double values to read.
15 % simTimWri - Current simulation time in seconds to write to the BCVTB.
16 % dblValWri - Vector of double values to write to the BCVTB.
17 %
18 % The return values are:
19 % retVal - A non-negative value if the data exchange was successfull,
20 % or a negative value if an error occured.
21 % flaRea - Communication flag read from the socket stream.
22 % flaRea < 0 indicates that the BCVTB will stop due to an error
23 % and not send any more data.
24 % flaRea == 0 is for normal operation.
25 % flaRea == 1 indicates that the final simulation time has
26 % been reached and no more data wil be exchanged.
27 % simTimRea - Current simulation time in seconds read from the socket.
28 % dblValRea - Vector of double values read from the socket.
29 
30 
31 % Revision history
32 % ----------------
33 % 2009-06-26 MWetter@lbl.gov: First version, based on code of
34 % Charles Corbin, UC Boulder
35 
36  % Maximum number of double values. See defines.h for how to change it.
37  NDBLMAX=1024;
38 
39  % Ensure that we will not attempt to read too many double values
40  if ( nDblRea > NDBLMAX )
41  msg=['Attempted to read ', int2str(nDblRea), ...
42  ' double values which is above the limit of ', ...
43  int2str(NDBLMAX), '.'];
44  ME = MException('BCVTB:ConfigurationError', ...
45  msg);
46  throw(ME);
47  retVal = -1;
48  end
49 
50  % Get pointer of arguments
51  INT32PTR='int32Ptr';
52  DBLPTR='doublePtr';
53  myFlaWri = libpointer(INT32PTR, int32(flaWri));
54  myFlaRea = libpointer(INT32PTR, int32(0));
55 
56  nDblWri = libpointer(INT32PTR, length(dblValWri));
57 
58  mySimTimWri = libpointer(DBLPTR, double(simTimWri));
59  myDblValWri = libpointer(DBLPTR, double(dblValWri));
60 
61  mySimTimRea = libpointer(DBLPTR, double(0));
62  myNDblRea = libpointer(INT32PTR, int32(0));
63  myDblValRea = libpointer(DBLPTR, double( zeros(1, nDblRea) ));
64 
65  % Exchange data
66  retVal = calllib(getBCVTBLibName(),'exchangedoubleswithsocket', sockfd, ...
67  myFlaWri, myFlaRea, ...
68  nDblWri, ...
69  myNDblRea, ...
70  mySimTimWri, ...
71  myDblValWri, ...
72  mySimTimRea, ...
73  myDblValRea);
74  VAL='Value';
76 
77  % Ensure that we read as many double values as expected
78  if ( nDblReceived ~= nDblRea )
79  msg=['Read ', int2str(nDblReceived), ...
80  ' double values but expected to read ', int2str(nDblRea), '.'];
81  ME = MException('BCVTB:ConfigurationError', ...
82  msg);
83  throw(ME);
84  retVal = -1;
85  end
86 
87  % Get return values from pointers
88  flaRea = get(myFlaRea, VAL);
91 % end function
function sockfd
exchanges data with the BCVTB The input arguments are
end Get pointer of arguments INT32PTR
end Get return values from pointers flaRea
static void XMLCALL end(void *data, const char *el)
Call back functions that will be used by the expat xml parser.
Definition: util/utilXml.c:707
#define NDBLMAX
Maximum number of double values that can be exchanged by Simulink.
Definition: defines.h:33