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
- 1. Invisible Combobox
- 2. Use enum symbol as an index for array of strings
- 3. Get string already in PLC
- 4. Creating a dedicated function in HMI
- Example file
This image displays the whole solution. Descriptions of the each way will refer here.
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.
Leave a Reply