CopyPaste

  • Data exchange in PLC using watchlist

    Overview

    Function to exchange non real time data between PLC Engineering and ctrlX CORE. A user can exchange data online from ctrlX CORE watchlist. Both read and write of variables are supported.

    • In version WRK-V-0204 (RM 23.11), only simple types like bool, int, string etc. are supported for data exchange.
    • In version PLE-V-0206 (RM 24.03), simple and flatbuffer type (objects) are supported. Arrays and Structures are not supported.
    • In version PLE-V-0302 (RM 24.07), simple and flatbuffer type (objects, arrays and structures) are supported.

    Data exchange from ctrlX CORE watchlists

    For online data exchange in PLC Engineering, variables need to be added to a watchlist. User can use the existing watchlist of a ctrlX CORE or create a new watchlist in the ctrlX CORE and then add variables to the created watchlist. To add variables to an existing watchlist, login to the ctrlX CORE and navigate to the desired variable in DataLayer and add to watchlist using the button ‘Adds to watchlist’ as shown below:

    Add variable to watchlist


    In this example, 3 variables are added to watchlist. Of these variables, two simple type variables (uint, bool) and one complex type variable (object) are added.
    Note: Variable name for complex type shouldn't contain '_'. 
    ctrlX CORE watchlist

    Add DataLayer variables in PLC Engineering from ctrlX CORE watchlists

    1. Open ctrlX PLC Engineering
    2. Create a new Project or Open an existing Project
    3. Open device node and connect to a ctrlX CORE in Communication Tab
    4. Right click on ‘Application’ node or on a folder node below application and click on context menu Add DataLayer variables -> from ctrlX CORE watchlists…

      Add DataLayer variables in PLC Engineering from ctrlX CORE watchlists

    5. In the next dialog, select the desired watchlist and click OK.

      Select watchlist
    6. In the next dialog, user can select the controller variables that are added to a watchlist. GVL, PRG and Task names are auto filled by the dialog. These names can also be edited. Existing task can be selected through the dropdown menu. Task cycle time can also be adjusted accordingly. After selecting the required variables in the dialog, User can click on ‘Generate PLC objects for writing’ or ‘Generate PLC objects for reading’.

      Select non realtime data from watchlist

      Dialog elements

      GVL nameName of the GVL, which will be added to the Project. By default, GVL name is created by the dialog. This can be changed to any desired name.PRG nameName of the PRG, which will be added to the Project. By default, PRG name is created by the dialog. This can be changed to any desired name.Task nameName of the Task, which will be added to the Project. By default, Task name is created by the dialog. This can be changed to any desired name or select an existing task object from the combo box.
      Type of the task by default is cyclic. This can be changed in the configuration tab of the Task.Task cycle time (ms)By default, Task cycle time is set to 500ms. This can be changed in the configuration tab of the Task.Variable editorHere the list of variables from the selected watchlist are shown, by default all valid variables will be checked. Variables can be checked or unchecked using the checkbox.

      Variable name can be changed to a valid IEC name.

      If the variable doesn’t have a valid IEC type, error icon with a tool tip is displayed.

      Variable access rights can be ‘Read’, ‘Write’ and ‘Read & Write’. Each of the access rights has corresponding icon with a tooltipGenerate PLC objects for writing

      This button will generate required algorithm inside given GVL and PRG objects for writing values to the selected simple variables (INT, STRING, BOOL etc.)

      If a FlatBuffer variables (OBJECT) is selected, then a folder ‘Internal’ is added below the selected node and required FB’s and PRG are generated below this Internal folder. 

      PRG object will be automatically added to the Task object.

       If these objects already exist, overwrite dialog will be shown.

      Same GVL and PRG name cannot be used for both read and write.

      Generate PLC objects for reading

      This button will generate required algorithm inside given GVL and PRG objects for reading values from the selected simple variables (INT, STRING, BOOL etc.)

      If a FlatBuffer variables (OBJECT) is selected, then a folder ‘Internal’ is added below the selected node and required FB’s and PRG are generated below this Internal folder.

      PRG object will be automatically added to the Task object.

       If these objects already exist, overwrite dialog will be shown.

      Same GVL and PRG name cannot be used for both read and write.CancelThis button will close the active dialog
    7. To read value from variable
      1. For simple variables: Login and Start the application. Open GVL added by the dialog and read values displayed under column “Value”.

        GVL editor in PLC Engineering
      2. For FlatBuffers variables: Expand Internal folder and Open PLC_<schema name> (PRG) read values displayed under column “Value”.

        PRG editor in PLC Engineering
    8. To write value into variable
      1. For simple variables: Open GVL added by the dialog and write required value under column “Prepared value”. Right click on the same page to open context menu and click on “Write all Values of <device name.application name>”.

        GVL editor in PLC Engineering
      2. For FlatBuffers variables: Expand Internal folder and Open PLC_<schema name> (PRG) and write required value under column “Prepared value”. Right click on the same page to open context menu and click on “Write all Values of <device name.application name>”.

        PRG editor in PLC Engineering

         

    Using FlatBuffers for type Arrays

    Note: This feature is available in PLC Engineering version PLE-V-0302 and above.

    Using Comfort data exchange complex flatbuffer types like arrays can be generated but due to the complexity involved in arrays, auto generated PLC programm doesn't contain schema (FB) methods that are required for data exchange. In this case a warning 'Schema variables of type Array cannot be autogenerated. Need to be added manually. Refer online documentation for details' is added automatically to the generated program.

    Generated program in PLC Engineering
    User has to initialize and call the required methods in the generated program. After the program is complete, warning line can be removed.

    As a reference open the project archive from the attached zip folder in PLC Engineering version PLE-V-0301 and above. Connect to a virtual/real controller and download the application 'DataProvider'. After successful download, data should be available under plc node in datalayer as shown below:

    Variable inside PLC application

    In the second application 'ReadArrayApplication' in PLC Engineering, a sample program is generated using the above 'struSample' and Array methods are called as shown below:
    Note: Flatbuffer Array-Startindex is 0.

    Generated program

    In a similar way, other complex variable of type Arrays can be used and programmed.

    How-to
    Project
    Watchlist_Array_Read.zip
    74.07MB
    2
    Generate API client libraries from Engineering API

    Overview

    This blog will show you how to generate API client libraries. API client libraries is a code or an app that can access the provided REST services. Client code generators are used to create such API Client Libraries. Few well known client code generators are Swagger Codegen CLIAutoRestNSwag, and OpenAPI Generator. These tools  require NPM or the Java Runtime Environment to be installed on the developers machine. Alternative Java SDK implementations such as the OpenJDK works fine with this extension. 

    The Swagger Codegen CLI and OpenAPI Generator are distributed as JAR files and are downloaded on demand but requires the Java SDK to be installed on the machine. AutoRest is installed on-demand via NPM as a global tool and uses the latest available version.

    NSwagStudio is stand alone UI tool for editing a .nswag specification file for generating code. This tool is optional to install and official installation instructions are available on the NSwag Wiki on Github. If NSwagStudio is not installed on the machine then the Visual Studio Extension will install the NSwag CLI via NPM as a global tool using the latest available version.

    For CSharp Environment

    Prerequisites

    • Latest ctrlX WORKS with PLC and IO Engineering is installed

    • Visual Studio is installed. (As an alternative Visual Studio Code can be used with extension C# for Visual Studio Code)

    • Swagger Codegen CLI is downloaded. For Windows users, you can use Invoke-WebRequest in PowerShell (3.0+) to download swagger-codegen-cli v3.0.33, e.g.

      Invoke-WebRequest -OutFile swagger-codegen-cli.jar https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.33/swagger-codegen-cli-3.0.33.jar
    • If you're looking for the latest stable Swagger Codegen CLI version, you can grab it directly from Maven.org (search for swagger-codegen-cli)

    • Swagger Codegen is a Java app, so you must have Java Runtime Environment installed. Additionally JAVA_HOME environment variable, should point to java.exe path

    Generate API client libraries using Swagger Codegen CLI

    CSharp client library for PLC Engineering

    • Open installed ctrlX WORKS directory and navigate to → Studio\Help\API-CodeGenerator. (For ctrlX WORKS WRK-V-0112.4 version and below,  folder 'API-CodeGenerator' is not available, you can download the files from attachment)

    • Copy the contents of API-CodeGenerator and downloaded swagger-codegen-cli.jar to a folder inside your work directory (e.g. D:\CodeGen) as shown below:

    • To generate PLC Engineering API client library, run 'Generate_Rexroth_PlcEng_APIClient_CSharp.bat'

    • After successful generation, you will see a folder named 'Rexroth_PlcEng_APIClient_CSharp' as shown below:

    • Navigate to 'Rexroth_PlcEng_APIClient_CSharp' directory and run 'build.bat'. This downloads required packages and DLLs are available under bin folder as shown below. (The Codegen also creates a README file with all the information for running and building the API)

     

     

     

     

     

     

     

     

     

    • Open 'Rexroth.Api.PlcEngineering.sln', project will be opened in Visual Studio

    • In Visual Studio, navigate and open Edit → Find and Replace → Replace in Files

    • In the editor, enter base(elementType, name, id, children) in find textbox  and enter base(elementType, name) in replace textbox and click on Replace All as shown below. (This step is required to correct few errors caused by Swagger Codegen CLI for CSharp language. Same errors might not occur in different language(s) or with different code generator(s))

    • Again enter base(jobType, id, state, progress, jobResultInfo) in find textbox  and enter base(jobType, jobResultInfo) in replace textbox and click on Replace All as shown below. (This step is required to correct few errors caused by Swagger Codegen CLI for CSharp language. Same errors might not occur in different language(s) or with different code generator(s))

    • Only for ctrlX WORKS WRK-V-0112.4 version and below, additional changes are required to the generated file (Rexroth_PlcEng_APIClient_CSharp\src\Rexroth.Api.PlcEngineering\Model\RecipeManager.cs). Changes should match to below shown images (see line number in the below image as reference):

      Add missing Assignment
      Select Assignment from the enum
      Select Assignment from the enum
    • In Visual Studio, navigate and click on Build → Rebuild Solution. (Rebuild Solution should be successful, If you still have build error, ask for help in the comment section)

    • After successful build,  DLL's are available (under the `bin` folder) in the C# project as shown below:

    • For more information/examples on available API functions, navigate to menu Help → API-reference in ctrlX PLC Engineering

    CSharp client library for IO Engineering

    • Run 'Generate_Rexroth_IoEng_APIClient_CSharp.bat' and follow steps similar to client library generation for PLC Engineering

    • For more information/examples on available API functions, navigate to menu Help → API-reference in ctrlX IO Engineering

    Java client library for PLC and IO Engineering

    • Run 'Generate_Rexroth_PlcEng_APIClient_Java.bat' and 'Generate_Rexroth_IoEng_APIClient_Java.bat'

    • To build and install the generated API client, follow the README file inside the created 'Rexroth_PlcEng_APIClient_Java' and 'Rexroth_IoEng_APIClient_Java' folder

    Client library for other languages

    • Swagger Codegen CLI also supports many other languages/frameworks. For more information visit there GitHub

    Generate API client libraries using openapi-generator-cli

    • Alternatively, client libraries can also be generated using openapi-generator-cli. Client libraries can be generated in a similar way as described for swagger-codegen-cli.

    • Openapi generator references latest version of 'RestSharp', which is already 'Strong-Named'. This helps the generated client libraries to be used in strong named environment.

    • If you're looking for the latest stable version, you can grab it directly from Maven.org (JAR location) (Java 11 runtime at a minimum).

    • For Windows users, you will need to install wget or you can use Invoke-WebRequest in PowerShell (3.0+), e.g.

      Invoke-WebRequest -OutFile openapi-generator-cli.jar https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.9.0/openapi-generator-cli-7.9.0.jar
    • See online for more CLI Installation options

    • After successful download, copy the 'openapi-generator-cli.jar' to the same folder where PLC & IO api json files are available.

    • Create a batch file and add below code and run the created batch file.

      call java -jar openapi-generator-cli.jar generate -g csharp -i plc-engineering-api-client-v2.json -o Rexroth_PlcEng_APIClient_CSharp --additional-properties packageName=Rexroth.Api.PlcEngineering,targetFramework=net8.0
    • In generated code, 'base' class needs to be replaced using VisualStudio, follow the same process as shown for swagger-codegen-cli.

    • Additionally for error CS0104: 'Task' is an ambiguous reference between 'Rexroth.Api.PlcEngineering.Model.Task' and 'System.Threading.Tasks.Task', use the reference of 'System.Threading.Tasks.Task'

    • Additionally for error CS0104: 'Path' is an ambiguous reference between 'Rexroth.Api.PlcEngineering.Model.Path' and 'System.IO.Path', use the reference of 'System.IO.Path'

    • In Visual Studio, navigate and click on Build → Rebuild Solution. (Rebuild Solution should be successful, If you still have build error, ask for help in the comment section)

    CSharp console application for PLC Engineering

    • In Visual Studio, navigate and click on File → New → Project. Follow the instructions in the dialog and Create an Console App (.NET Framework)

    • Add generated 'Rexroth.Api.PlcEngineering.dll' reference to the created console application as shown below:

    • Open Program.cs file and replace existing code with below code:

      using System;
      using System.Collections.Generic;
      using System.Diagnostics;
      using System.Threading;
      using Rexroth.Api.PlcEngineering.Api;
      using Rexroth.Api.PlcEngineering.Client;
      using Rexroth.Api.PlcEngineering.Model;
       
      namespace ConsoleApp
      {
        public class Program
        {
          static void Main(string[] args)
          {
            try
            {
              Console.WriteLine("Start PLC Engineering and create a new project");
              //Latest installed or latest used ctrlX WORKS instance will be used to start engineering
              //For ctrlX WORKS version WRK-V-0112.4 and below, protocol 'rexrothstudio://plc/?' to start engineering is not supported, use
              //Process.Start(@"path to engineering exe") and open existing project using 'ProjectJob' in API
              Process.Start("rexrothstudio://plc/?hostname=localhost&newproject=d:\\ctrlX\\Projects");
       
              //Wait till Engineering is ready for input
              Thread.Sleep(20000);
       
              //Set base path to the actual URL with port
              Configuration config = new Configuration {BasePath = "http://localhost:9002/plc/engineering/api/v2"};
       
              //Create new device api instance with the actual URL
              DevicesApi devicesApi = new DevicesApi(config);
       
              //Create a device with mandatory parameters
              Device ctrlXCoreDevice = new Device(name: "ctrlXCoreDevice", elementType: "Device");
              DeviceInfo deviceInfo = new DeviceInfo(deviceType: 4096, id: "1001 0080");
              ctrlXCoreDevice.DeviceInfo = deviceInfo;
       
              BaseElement addedDevice = devicesApi.AddDevices(ctrlXCoreDevice);
              Console.WriteLine($"Device added: {addedDevice.Name}");
       
              //Create and add task configuration
              TaskConfig taskConfigObj = new TaskConfig(elementType: "TaskConfig", name: "Task Configuration");
              devicesApi.AddDevicesByPath(taskConfigObj, "/ctrlXCoreDevice/PLC Logic/Application");
              Console.WriteLine("Task Configuration added");
       
              //Create and add a task
              Task taskObj = new Task(elementType: "Task", name: "MyTask")
              {
                Priority = "30",
                TaskGroup = "IEC-Tasks",
                Type = new TaskType("cyclic", "t#20ms"),
                Watchdog = new TaskWatchdog(false, "", "1"),
                PouCalls = new List<TaskPouCall>()
              {
                new TaskPouCall("Prg1", ""),
                new TaskPouCall("Prg2", "")
              }
              };
              devicesApi.AddDevicesByPath(taskObj, "/ctrlXCoreDevice/PLC Logic/Application/task-config");
              Console.WriteLine("Task added");
       
              Console.ReadLine();
            }
            catch (Exception e)
            {
              Console.WriteLine(e);
              throw;
            }
          }
        }
      }
    • After successful Build, Start the console app. This will start the ctrlX PLC Engineering with a new project and a Device, Task Configuration, Task will be added to the project

    How-to
    Controls, IO, PLC
    API-CodeGenerator(WRK-V-0112).zip
    30.25KB
    3

About

Member since
02/03/2022