TwinCAT HMI – 4 ways to get ENUM STRING of variable from PLC

In the new HMI there is not a straight easy way to get string of enumerated variable from PLC. Let’s see what are our current possibilities.

Table of Contents

This image displays the whole solution. Descriptions of the each way will refer here.

The example solution can be downloaded at the end of article

1. Invisible Combobox

It is none of the sercet that Combobox can be configured to use enumerated data symbol. It will display all options of enumerated symbol and you can choose from them. We will use this fact to get the string of current value.

So first bind your enumerated variable to the Combobox’s EnumDataSymbol. It is good idea to give your Combobox some recognizable Identifier sou you can easily find it when binding in the next step.

Then bind property SelectedText of the Combobox whenever you like and you are done.

If you don’t want to see the Combobox (hence you are using it just to obtain enum string) then you should set it’s Visibility property to "Collapsed".

2. Use enum symbol as an index for array of strings

Second option can bring you some versatility. This way you are not tied to exact strings of the enum because you will have to make your own (either same or different, as you wish).

First create array as Internal symbol in HMI and fill it with whatever strings you like. You can copy the same strings you used in PLC or you can even try to make array of translations (I was not successful).

When enum variable is resolved it returns integer value of the variable. We will use it to access corresponding index in your array. That’s it! You can either use JS to access an index of the array using [ ] or use built-in function GetElementByIndex(<array symbol>, <index>).

3. Get string already in PLC

So this option is completely out of HMI. Aside of our desired enumeration variable we will create second variable in PLC of type STRING that will help us obtain the string value of the enumeration variable in PLC.

You can convert variable value to string using TO_STRING() function. But here is a catch… if you do this with variable of enumeration type you will get just integer value of enumeration converted to string.

For obtaining string value of enumeration key you have to add {attribute 'to_string'} into your enumeration type definition. Now when you use TO_STRING() you will get name of your enumeration value.

So now you already have the string value of enumeration in PLC so you just bind this second variable instead.

4. Creating a dedicated function in HMI

Final option si to create JS function where you input your enum symbol and the function will return it’s string value. This one is the cleanest solution I would say.

I have managed to do this by passing symbol into function as EnumDataSymbol data type. This way symbol will not be resolved but it will pass object containing it’s symbol expression string instead. We are reading symbol from server (i.e. PLC) so the function has to be asynchronous.

Then I call resolveSchema() function for this object which gives me object containing all the enum options as an array. Lastly I use readEx2() function to read integer value of variable and with this value I return correct index of enum string options array.

// ctx - function have to be asynchronous
// EnumDataSymbol - returns object containing symbol expression instead of resolved value
function GetEnumString(ctx, EnumDataSymbol) {

    // gets symbol expression
    var symbolExpression = EnumDataSymbol.__symbol.__expression.__expression;

    // get enum scheme
    EnumDataSymbol.resolveSchema(function (data) {
        if (data.error === TcHmi.Errors.NONE) {
            var schema = data.schema;

            // read value of enum symbol
            TcHmi.Symbol.readEx2(
                symbolExpression, 
                function (data) {
                
                if (data.error === TcHmi.Errors.NONE) {

                    // ctx.success = return value in HMI async function
                    //      get string from enum scheme by value resolved integer value of enum
                    ctx.success(
                        schema.options[data.value].label);
                } else {
                    ctx.error(TcHmi.Errors.ERROR);
                }
            });


        } else {
            ctx.error(TcHmi.Errors.ERROR);
        }
    });

}

Finally you just call the function with the enum symbol as parameter whenever you like.

Example file

Download the whole example with code.

9 MB


Posted

in

,

by

Comments

5 responses to “TwinCAT HMI – 4 ways to get ENUM STRING of variable from PLC”

  1. Anthony Avatar
    Anthony
    Hello,
    I tried the way #4 but it doesn’t work with the version TE200 v1.12.760.44 (the last one)
    Is there a bug on my side or this new version is not compatible ?
    Anthony
  2. admin Avatar

    Hey, that is wierd. I just tried to run the example with the new version and it works fine. Did you change something? Did you tried to implement it in your own way?

  3. Christian Meindl Avatar
    Christian Meindl
    Hello,
    I tried #4 but it doesn’t work with TwinCAT 4024.55 / HMI 12.760.54.

    The Localization e.g cs is not working !

    1. admin Avatar

      Hello Christian,

       

      Could you please provide more information? Also I cannot see your picture. please try to provide link for your screenshot.

  4. Jakob Avatar
    Jakob
    If you adjust the success action from:
    schema.options[data.value].label);
    to
    schema.options[schema.enum.indexOf(data.value)].label);

    Then you can take an ENUM symbol with your own values.
    e.g. You create your ENUM in your PLC project with own values like 0,10,20,30,… and do not keep the standard values of 0,1,2,3,4,5,…

Leave a Reply

Your email address will not be published. Required fields are marked *

Optionally add an image (JPEG only)