Calling a program
Introduction:
Description of program and parameters
This example shows the execution of a MYPGM
program from the MYLIB
library, with the following parameters:
Description | Type | Direction | Value |
---|---|---|---|
Text 1 | CHAR(10) | Input | Hello |
Text 2 | CHAR(10) | Input | World |
Starting position | BYTE(1) | Input | 0x00 |
Return variable length | BYTE(4) | Input | 128 |
Return variable | CHAR(*) | Output | vide |
Error code | CHAR(50) | InputOutput | vide |
We want to read different values from the return variable, composed as follows:
Offset | Length | Description |
---|---|---|
0 | 64 | Message 1 |
64 | 64 | Message 2 |
The call would be made with the following code:
Step 1/4: Opening the connection
We start by declaring a new instance of NTiConnection
and opening it:
//Open an NTi connection:
NTiConnection conn = new NTiConnection(connectionString);
conn.Open();
Step 2/4: Parameter creation
Next, we refer to the description of the parameters expected by the program or PLC to be executed (above). Create the various parameters and their values:
//Create parameter list:
List parms = new List() {
new NTiProgramParameter("Hello", 10).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter("World", 10).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter(new byte[] {0x00}).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter(128).AsInput(), //INTEGER (BYTE(4)) INPUT
new NTiProgramParameter("", 128).AsOutput(), //CHAR(128) OUTPUT
new NTiProgramParameter("", 50) //CHAR(50) I/O
};
Step 3/4: Calling the program
The program can be called up using the CallProgram()
method of the NTiCOnnection
class:
//Program call
conn.CallProgram("MYLIB", "MYPGM", parms);
Step 4/4: Retrieving returned data
Once the program has been called, we retrieve the returned data
//Data retrieval from return variable (parameter n°5)
string message1 = parms[4].GetString(0, 64);
string message2 = parms[4].GetString(64, 64);
Code summary
The final code for calling an IBM i program from .NET with NTi, passing parameters and retrieving data in return is as follows:
//Open connection
var conn = new NTiConnection(connectionString);
conn.Open();
//Create parameter list:
List parms = new List() {
new NTiProgramParameter("Hello", 10).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter("World", 10).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter(new byte[] {0x00}).AsInput(), //CHAR(10) INPUT
new NTiProgramParameter(128).AsInput(), //INTEGER (BYTE(4)) INPUT
new NTiProgramParameter("", 128).AsOutput(), //CHAR(128) OUTPUT
new NTiProgramParameter("", 50) //CHAR(50) I/O
};
//Program call
conn.CallProgram("MYLIB", "MYPGM", parms);
//Data retrieval from return variable (parameter n°5)
string message1 = parms[4].GetString(0, 64);
string message2 = parms[4].GetString(64, 64);
//Closing the connection
conn.Close();
You should also remember to close the connection at the end of processing if you don't want to use it again. However, when the
conn
object is destroyed at the end of the program or when the current method is exited, the connection will automatically be closed.