JMeh 11 Jul 2017 - RfcNwTcl
SAP NetWeaver RFC Tcl-API - Rev. 1.12 from 27. Nov 2014
Tcl-Library for executing RFC (Remote Function Calls) functions in and from a SAP R/3 NetWeaver Server. Data can be transfered in both directions on each call and can contain simple values, complex structures or tables (multiple rows of structures). Each field in a structure or table can itself be a structure or table. This allows nested tables to be transfered on a single RFC call.
The C source compiles on Windows and UNIX systems.
You can download the C sources and some examples here:
http://www.mehrdv.de/Downloads/Public/rfcnwtcl.zip
You need to install the SAP NetWeaver Library (with includes) which you get from the SAP AG (if you are a customer). The libs are named libsapnwrfc.so and libsapucum.so (see Makefile). The corersponding SAP installation package also contains a Perl script for precompiling the source (see Makefile).
Nearly all RFC functions return with the text "RFC_OK" on success or "RFC_ERROR" on failure.
The library is well tested with several RFC applications (client and server) running on AIX, Linux and Windows. They transfer many megabytes of data each day.
Here is a simple client example to call a SAP function (Z_RFC_TEST01) on the SAP server and send a text as a function parameter (REQUTEXT = "A simple test."). The RFC function must be defined in the SAP system and you must have the rights to make this call.:
package require RfcNwTcl Rfc::Init Rfc::OpenConnection {DEST TST1} Rfc::InvokeFunction Z_RFC_TEST01 {REQUTEXT "A simple test."} Rfc::CloseConnection
Here is a server example to allow SAP to call the Tcl function named "MyCallback" from a SAP server. The name of the callback function from SAP view is Z_RFC_TEST02. It will be installed by invoking InstallServerFunction with both names. The return value of the Tcl function in this example is a table named "TABDAT" containing two rows with three columns. The structure of the function with their parameters must also be defined in the SAP system!
proc MyCallback {connAttr params} { return {TABDATA {{123 456 "Row 1"} {789 321 "Row 2"}}} } package require RfcNwTcl Rfc::Init Rfc::OpenConnection {DEST TST1} Rfc::InstallServerFunction Z_RFC_TEST02 MyCallback Rfc::CloseConnection while 1 { if {[Rfc::RegisterServer {DEST TST1}] != "RFC_OK"} break set retry true while {$retry} { switch [Rfc::ListenAndDispatch 600] { RFC_OK - RFC_RETRY - RFC_ABAP_EXCEPTION { set retry true } default { set retry false } } } Rfc::CloseConnection }
For all above examples the SAP connection requires to have a NetWeaver INI file named "sapnwrfc.ini" with the following contents:
DEST=TST1 TYPE=A PROGRAM_ID=NWTEST1 GWHOST=192.168.0.100 GWSERV=sapservice ASHOST=192.168.0.100 SYSNR=123 CLIENT=001 USER=sapuser PASSWD=sappasswd LANG=DE TRACE=0
The contents must be modified to match your SAP server requirements.
Functions:
Rfc::DictToString dictValue
Convert dictionary to readable text.
Example:
set info [Rfc::DescribeFunction Z_RFC_TEST01] puts [Rfc::DictToString $info]
Rfc::GetVersion
Return the version number of the C-library.
Example:
set info [Rfc::GetVersion]
Rfc::Init
Initialize the Library. Must be the first RFC-function to call.
Example:
set rc [Rfc::Init]
Rfc::OpenConnection destination
Opens a client connection to the SAP server. The parameter must specify a destination of the INI file (see above) or the whole destination definition (without the INI file).
Example 1:
set rc [Rfc::OpenConnection {DEST TST1}]
Example 2:
set connectionInfo { TYPE A PROGRAM_ID NWTEST1 GWHOST 192.168.0.100 GWSERV sapservice ASHOST 192.168.0.100 SYSNR 123 CLIENT 001 USER sapuser PASSWD sappasswd LANG DE TRACE 0 } set rc [Rfc::OpenConnection $connectionInfo]
Rfc::GetConnectionAttributes
Read the connection attributes (as a Tcl dictionary) from an existing SAP connection.
The following dict items will be filled:
Example:
set info [Rfc::GetConnectionAttributes]
Rfc::DescribeFunction functionName
Get a function description (as a Tcl dictionary) from an existing SAP function.
The dict contains the following elements:
A parameter itself contains the following items:
The following RFC data types exist:
Example:
set info [Rfc::DescribeFunction Z_RFC_TEST01] puts [Rfc::DictToString $info]
Rfc::GetErrorInfo
Get the error description of the last RFC error.
Example:
set info [Rfc::GetErrorInfo] puts [Rfc::DictToString $info]
Rfc::InvokeFunction functionName parameterDict
Call a RFC function in the SAP system. The parameterDict must be filled with parameter values (see Rfc::DescribeFunction).
Example:
set params { PARAMTEXT "Only a Test." PARAMVAL1 4711 PARAMVAL2 3.14159 PARAMTAB { {123 456 "Hello there"} {789 321 "With german umlaut: äöüß ÄÖÜ"} {893 111 "Third row"} } } set result [Rfc::InvokeFunction Z_RFC_TEST01 $params]
Rfc::InstallServerFunction functionName callbackProc
Installs a Tcl callback function which can be called from SAP to transfer data to or from the client (or both).
Example:
proc MyCallback {connAttr params} { return {TABDATA {{123 456 "Row 1"} {789 321 "Row 2"}}} } Rfc::InstallServerFunction Z_RFC_TEST02 MyCallback
Rfc::RegisterServer loginParams
Connects to the SAP server and registers a server which can invoke Tcl functions. The connection to the SAP system must be closed to call this function. To install the callback function you have first connect to SAP as a client, call DescribeFunction, disconnect from SAP and then call RegisterServer to connect as a logical sever.
Example:
Rfc::OpenConnection {DEST TST1} Rfc::InstallServerFunction Z_RFC_TEST02 MyCallback Rfc::CloseConnection set rc [Rfc::RegisterServer {DEST TST1}]
Rfc::ListenAndDispatch timeoutSeconds
After calling RegisterServer we can wait for incoming calls from the SAP system. This function is blocked until SAP makes a call or until a timeout event occurred.
The following result values are defined:
Example:
set rc [Rfc::ListenAndDispatch 600] if {$rc == "RFC_OK"} { # yeah :-) }
Rfc::Ping
Make a RFC connection check to the SAP server.
Example:
set rc [Rfc::Ping]