09/29/2023

How to read Data Layer Nodes through Visual Studio


Introduction

Visual Studio 2022 is a popular integrated development environment (IDE) used by many software developers to create applications for various platforms. One of the key features of Visual Studio 2022 is its ability to work with dynamic-link libraries (DLLs) to provide additional functionality and modularity to an application. In this article, we will discuss how to use comm_datalayer.dll in Visual Studio 2022 and explore its benefits in terms of code reuse, maintainability, and performance optimization. We will also provide step-by-step instructions on how to reference and use DLLs in your Visual Studio 2022 project to read ctrlX CORE datalayer nodes.

Diagram for usecase

Prerequisites

Configuration

To create a such project in Visual Studio please follow step by step description given below.

Step 1: Create a Console app project in visual studio

Open a Visual Studio and click on create a new project as shown in picture below.

Create a new project

Now you can see the templates library for creating a new project. In the search bar type in "console app".

create a console app

From the drop-down select a project with C++. After double-clicking on that project new dialogue box will open. Give your project a relevant name and click on Create. Make sure to check the location where you create this project.

create project

Now your environment should look like the picture below.

visual studio environment

Step 2: Write code in the cpp file to read datalayer nodes

In the cpp file copy the code below. Depending on your application you can change the address of the datalayer and your ctrlX CORE login credential. By executing the code below you will be able to read the CPU utilisation of ctrlX CORE.



#include <stdio.h>
#include <iostream>

#include "comm/datalayer/datalayer.h"
#include "comm/datalayer/datalayer_system.h"


static std::string getConnectionString(
const std::string& ip = "192.168.1.1",
const std::string& user = "boschrexroth",
const std::string& password = "boschrexroth",
int sslPort = 443)
{

std::string connectionString = DL_TCP + user + std::string(":") + password + std::string("@") + ip;

if (443 == sslPort)
{
return connectionString;
}

return connectionString + std::string("?sslport=") + std::to_string(sslPort);
}


int main()
{

int counter = 1;

comm::datalayer::Variant data;
comm::datalayer::DlResult result;
comm::datalayer::DatalayerSystem datalayerSystem;
datalayerSystem.start(false);
std::cout << getConnectionString() << std::endl;
comm::datalayer::IClient* dataLayerClient = datalayerSystem.factory()->createClient(getConnectionString());
std::cout << dataLayerClient << std::endl;
if (dataLayerClient->isConnected())
{
std::cout << "Bingo! it is connected" << std::endl;
}
else {
std::cout << "Client is not connected" << std::endl;
}

//endless loop
for (int p = 0; p < 1000; p++)
{
//std::string co = "ping 192.168.1.1";
//std::system(co.c_str());
//std::cout << "Loop #" << counter++ << std::endl;
//Synchronous read to the Data Layer path "framework/met..."
result = dataLayerClient->readSync("framework/metrics/system/cpu-utilisation-percent", &data);

if (result == DL_OK) {
std::cout << "Value CPU utilisation: " << double(data) << " %" << std::endl;
}
else {
std::cout << "Reading the CPU utilisation failed with: " << result.toString() << std::endl;
}

}

delete dataLayerClient; // Callbacks are no more called
}


Save this code. You will notice that it shows many errors now. This is because headers are not configured yet.

Step 3: Configure headers

To configure headers go to solution explorer on the left or right side and right-click on the project. In the drop-down menu select properties. The alternative in the Menubar project-->properties.

project properties

 

In the properties dialogue box go to C++-->General-->Additional Include Directories-->edit.

edit properties for headers

When you click on edit new window will pop up in this window define the address where your 'ctrlx-automation-sdk/include/comm.datalayer'. You either manually type in or locate via clicking on the button with three dots.

locate sdk repository

After locating the right folder click on ok. Now your headers are configured because in this included directory all necessary headers for the datalayer are located.

But this is not enough. We also need to feed this project additional dependencies by locating respective .lib and .dll files.

Step 4: Configure .lib files

again go to the properties dialogue box. this time Linker-->Input-->Additional Dependencies-->edit

locating .lib files in properties

In windows of additional dependencies type the exact same .lib as highlighted in the picture above.

Here we have only provided the names of the .lib file which need to be added but it does not know where to find such .libs. Therefore go to Linker-->General-->additional library directories-->edit and define the location of each .lib file in SDK as shown below in the picture.

define location of dependencies

Click on ok and .libs are configured.

Step 5: Build and release the project

To build a project go to Menubar -->Build-->Build project_name or just press ctrl+B.

output of build

The build process has to be finished by '1 succeeded'. If not check the location of configured headers.

Now change the mode from debug to release and click on local Windows Debugger as shown in the picture below.

select release in toolbar

Now release will end with an error dialogue box. Don't worry about that, just click on Close and Open file explorer.

Step 6: Copy DLLs at the project  location

The above error occurred because it could not file dll. and there is the reason why we have not resolved this error before. Because if we have built a release in the first place then there would be no release folder inside the project directory.

Now to resolve this error go to your 'ctrlx-automation-sdk-->bin'. Inside bin there are several folders and in some folders you will find 'win-msvc-x64-->release' go to this folder and copy every file in this folder to 'your_project-->x64-->Release'.

location dll inside sdk

After copying the necessary files your Release folder should look like the picture below in my case there are files named vscode, which is the name of my project.

Look of Release folder

 

Step 7: Rebuild and Run the code

After locating DLL dependencies inside the release folder now go to the visual studio project.

To rebuild first go to 'Menubar-->Build-->Clean Project_name' after the cleaning process is finished again go to 'build-->Build Project_name'. Now the build is finished and click on the 'Local Windows Debugger' button on the toolbar.

Now you can realise your output in the console window of the visual studio as can be seen in the picture below.

Output of program

At runtime If you encounter any error related to OpenSSL DLLS then go to: https://slproweb.com/products/Win32OpenSSL.html install OpenSSL 1.1.1 relevant files and add those to environment variables.

 

If adding an environment variable is not an option for you then, manually copy dlls of "C:\Program Files\OpenSSL-Win64\bin" to Release folder.

At the bottom, you can find a zip file of my project which is already configured with dependencies.

Thank you for taking the time to read this article. I hope you found it informative and enjoyable. If you have any questions, comments or encounter any unusual problems with the project, feel free to leave them in the comments section below. I would love to hear from you and continue the conversation. Your feedback is always appreciated!

3
ReadDL.zip
8.39MB

Latest published/updated articles

Types
How-to
Attachments
Project