xxsimConnect - Connects with 20-sim. 20-sim editor should be open with XMLRPC interface enabled before connecting. Syntax: retval = xxsimConnect() retval = xxsimConnect(URI) retval = xxsimConnect(URI, timeout) Inputs: URI = connection specified as URI (scheme://server:port?options) . timeout = Connection and command time-out (optional, default = 300 {s}). Supported protocols: - http : the official xmlrpc protocol according to the specification (Matlab and Octave). - tcp : binary serialized version transported via tcp (faster than http; Octave only). - serial : binary serialized version transported via serial port (Octave only). Servers 20-sim - Default localhost only. - Default HTTP port 5580 (range 5580-5589). - Default TCP port 5520 (range 5520-5529). - Default serial port disabled. Note: Matlab only supports the HTTP protocol. Note: TCP is faster than the HTTP protocol. 20-sim 4C - Default localhost only. - Default HTTP port 5480 (range 5480-5489). - Default TCP port 5420 (range 5420-5429). - Default serial port disabled. Targets - Default any host. - Default HTTP port 1580 (1580-1589). - Default TCP port 1502 (1502-1511). - Default serial port disabled. Outputs: returns true if connection is made. Examples: xxsimConnect('http://localhost:5580') - connect via the HTTP(native xml-rpc) to the local computer on port 5580. xxsimConnect('tcp://mytarget:1502') - connect via the TCP(binary) protocol to remote server 'mytarget' on port 1502. xxsimConnect('serial://com1?baud=115200') - connect via the serial port(binary) protocol via COM1 with baud rate of 115200. xxsimConnect('tcp://localhost:5520', 500) - connect via the TCP(binary) protocol to the local computer on port 5520 with a connection and command time-out of 500 seconds. See also: xxsimDisconnect Author: Controllab Products B.V. email: info@controllab.nl Website: http://www.controllab.nl December 2017
0001 function [retval] = xxsimConnect(varargin) 0002 % xxsimConnect - Connects with 20-sim. 0003 % 20-sim editor should be open with XMLRPC interface enabled before connecting. 0004 % 0005 % Syntax: 0006 % retval = xxsimConnect() 0007 % retval = xxsimConnect(URI) 0008 % retval = xxsimConnect(URI, timeout) 0009 % 0010 % Inputs: 0011 % URI = connection specified as URI (scheme://server:port?options) . 0012 % timeout = Connection and command time-out (optional, default = 300 {s}). 0013 % 0014 % Supported protocols: 0015 % - http : the official xmlrpc protocol according to the specification (Matlab and Octave). 0016 % - tcp : binary serialized version transported via tcp (faster than http; Octave only). 0017 % - serial : binary serialized version transported via serial port (Octave only). 0018 % 0019 % Servers 0020 % 20-sim 0021 % - Default localhost only. 0022 % - Default HTTP port 5580 (range 5580-5589). 0023 % - Default TCP port 5520 (range 5520-5529). 0024 % - Default serial port disabled. 0025 % Note: Matlab only supports the HTTP protocol. 0026 % Note: TCP is faster than the HTTP protocol. 0027 % 20-sim 4C 0028 % - Default localhost only. 0029 % - Default HTTP port 5480 (range 5480-5489). 0030 % - Default TCP port 5420 (range 5420-5429). 0031 % - Default serial port disabled. 0032 % Targets 0033 % - Default any host. 0034 % - Default HTTP port 1580 (1580-1589). 0035 % - Default TCP port 1502 (1502-1511). 0036 % - Default serial port disabled. 0037 % 0038 % Outputs: 0039 % returns true if connection is made. 0040 % 0041 % Examples: 0042 % xxsimConnect('http://localhost:5580') 0043 % - connect via the HTTP(native xml-rpc) to the local computer on port 5580. 0044 % 0045 % xxsimConnect('tcp://mytarget:1502') 0046 % - connect via the TCP(binary) protocol to remote server 'mytarget' on port 1502. 0047 % 0048 % xxsimConnect('serial://com1?baud=115200') 0049 % - connect via the serial port(binary) protocol via COM1 with baud rate of 115200. 0050 % 0051 % xxsimConnect('tcp://localhost:5520', 500) 0052 % - connect via the TCP(binary) protocol to the local computer on port 5520 0053 % with a connection and command time-out of 500 seconds. 0054 % 0055 % See also: xxsimDisconnect 0056 % 0057 % Author: Controllab Products B.V. 0058 % email: info@controllab.nl 0059 % Website: http://www.controllab.nl 0060 % December 2017 0061 0062 %------------- BEGIN CODE -------------- 0063 % get the scripting directory 0064 [rootDir, name, ext] = fileparts (mfilename('fullpath')); 0065 [rootDir, name, ext] = fileparts (rootDir); 0066 [rootDir, name, ext] = fileparts (rootDir); 0067 0068 if ( exist('OCTAVE_VERSION') ~= 0 ) 0069 %Add the octave folder to the path 0070 addpath( [rootDir, filesep(),'library',filesep(), 'octave'] ); 0071 else 0072 %Add the matlab folder to the path 0073 addpath( [rootDir, filesep(),'library',filesep(), 'matlab'] ); 0074 end 0075 0076 %make the xrl function globally available 0077 xrlloadfunctions(); 0078 0079 if(length(varargin)>2) 0080 error('Too many input arguments were specified: xxsimConnect expects up to 2 (optional) input arguments.'); 0081 end; 0082 0083 if (length(varargin)>=2) 0084 xrlsettimeout(varargin{2}); 0085 else 0086 xrlsettimeout(300); % set timeout to 5 minutes 0087 end 0088 0089 %load the 20sim specification 0090 if ( exist('OCTAVE_VERSION') ~= 0 ) 0091 specificationFile = xrlgetspecificationfile(); 0092 0093 if xrlloadspecification(specificationFile)==0 0094 error(['Could not load specification' specificationFile]); 0095 end 0096 end 0097 0098 % Do housekeeping of the connections available 0099 global xxsim; 0100 if length(xxsim) == 0 0101 global xxsim; 0102 version = struct('major', 1, 'minor', 0, 'revision', 0, 'patch', 0); %Add the version of the scripting library 0103 xxsim = struct('verbose', logical(0), 'version', version, 'activeconnection', int32(0), 'connections', {{}}); 0104 end 0105 0106 %Make the connection 0107 if length(varargin)>=1 0108 try 0109 xrlconnect(varargin{1}); 0110 catch 0111 error('Error making connection with 20-sim! Please make sure 20-sim is running and XMLRPC scripting interface enabled'); 0112 end 0113 else 0114 try 0115 if ( exist('OCTAVE_VERSION') ~= 0 ) 0116 % Connect using the Controllab optimized binary XML-RPC protocol 0117 xrlconnect('tcp://localhost:5520'); 0118 else 0119 % Connect using the official XML-RPC protocol 0120 xrlconnect('http://localhost:5580'); 0121 end 0122 catch 0123 error('Error making connection with 20-sim! Please make sure 20-sim is running and XMLRPC scripting interface enabled'); 0124 end 0125 end 0126 0127 % Get and check the version with interface 1.0.3 0128 try 0129 versionInfo = xrlinvoke('xxsim.getVersion'); 0130 catch 0131 error('Error making connection with 20-sim! Please make sure 20-sim is running and XMLRPC scripting interface enabled!'); 0132 end 0133 0134 versionCell = strsplit( versionInfo.interfaceVersion, '.' ); 0135 if str2num(versionCell{1}) ~= 1 || str2num(versionCell{2}) ~= 0 || str2num(versionCell{3}) < 3 0136 retval = false; 0137 error('20sim interface version mismatch!') 0138 end 0139 0140 % now register the active connection 0141 % for now we can have only one connection at the time, so make this explicit for now 0142 % but only if we have not a model loaded yet 0143 global xxsim; 0144 if length(xxsim.connections) == 0 0145 xxsim.activeconnection = length(xxsim.connections)+int32(1); 0146 xxsim.connections{xxsim.activeconnection} = struct('activemodel', int32(0), 'models',[]); 0147 end 0148 0149 retval = true; 0150 end 0151 %------------- END OF CODE --------------