When programming a machine it may be useful to determine if a user is logged into the device with the Integrated Design Environment (Codesys). This can be accomplished using the "AppNumofActiveSessions" function call. The function call is part of the "CmpApp" library.
The following example explains how to use the function to determine how many users are connected to the device through the programming environment. This function is not used to determine if users are logged into the user management of the runtime - i.e. if someone is logged in through an HMI device.
The first step is to add the "CmpApp" library to the Library Manager:
Next, navigate to the "CmpApp" library in the Library Manager. Find the function "AppNumOfActiveSessions" function and take note of the inputs and output of the function.
The first input required is a "POINTER TO APPLICATION". This can be found by using another function contained in the "CmpApp" library. For this example the function "AppFindApplicationByName" will be used. This example is a single application project and the name of the application is known making the use of the function appropriate. Note that there are other functions which could be used to create the pointer to application.
Taking a look at the "AppFindApplicationByName" function in the library manager, this function takes two inputs and returns a "POINTER TO APPLICATION".
Start by adding a variable to the declaration window "pApp" of type "POINTER TO CmpApp.Application". Next, add a variable "pResult" of type "POINTER TO RTS_IEC_RESULT". (If an error appears at this point indicating that "RTS_IEC_RESULT" is not defined, add the library "SysTypes Interfaces" to the library manager.
Now the variables necessary to call "AppFindApplicationByName" are available. Call the function as shown below:
This function call has created the pointer to the application which will be monitored for a Login. Note that the input to the function for the name of the application must match the name in the project tree.
Next, this pointer can be used with the "AppNumOfActiveSessions" to find the number of users logged into the device. A "POINTER TO UDINT" is also needed as an input to the function. Create the variables shown below:
Now, everything necessary to call "AppNumOfActiveSessions" is available. Call the function as shown below:
Downloading the project to the device and starting the application shows that a result of "1" is assigned to "udiNumberOfSessions".
To further test that the function is appropriately indicating the number of users logged in, "udiNumberOfSessions" was added to a visualization. In this way, the value of the variable can be watched outside of the IDE to verify a value change.
Logged in:
Logged out:
VL 20211110