Structured Text - ENUM to String?

Hello,

for logging purpose i’d like to convert ENUM names to string.
At the moment I do it the most un-elegant way:

CASE step OF
    X_GOTO_POS_REF: 
        logStr := 'X_GOTO_POS_REF';
    .
    .
    .
END_CASE

I thought of putting all ENUM names into an Array for quicker access, but thats very confusing when you have to modify it.

Is there a more elegant way?

I have a college which tried this some time ago and did not find an elegant way.

Depending on the usage of this String … VC4, MappView, ArLogger i could think about using the Textsystem to move the Data from the Code to some other source. In this way you can use a Windows Batch to create the Textsystemfile from the .var File entrys. It also enables some additional Language change options. The Batch-File can be added to the Compile of the AS via the Pre-Build Step in the Build Events.

But if you use it in many different Step-Cases it can also grow fast to an un-elegant way.

1 Like

Hi Torsten,

with the “PV_” Functions you can get the names of Variables and structure elements
GuID AS-Help 4.12: 5eae961c-cea3-4847-82df-d5b09c26ef22

The problem is, a enum is not a structure of datatypes… so we have to get creative.

The most elegant un-elegant solution I can think of is this:
Create a datatype with a string and a enum_type (your enum-type)


TYPE
	enum_string_type : 	STRUCT 
		enum_val : enum_test;
		text : STRING[20];
	END_STRUCT;
	enum_test : 
		(
		EN_FIRST := 10,
		EN_SECOND := 20,
		EN_THIRD := 25,
		EN_FOURTH := 33
		);
END_TYPE

and then a variable as an array of how ever many “steps” your enum has…

VAR
	textEnum : ARRAY[0..3] OF enum_string_type;
    i : USINT;
	current_step : STRING[20];
END_VAR

Afterwards you have to copy+paste the text of your enum-name with your enum-value (init-part of task, or in the “.var”-File:

	textEnum[0].enum_val := EN_FIRST;
	textEnum[0].text := 'EN_FIRST';
	textEnum[1].enum_val := EN_SECOND;
	textEnum[1].text := 'EN_SECOND';
	textEnum[2].enum_val := EN_THIRD;
	textEnum[2].text := 'EN_THIRD';
	textEnum[3].enum_val := EN_FOURTH;
	textEnum[3].text := 'EN_FOURTH';

last but not least, you have to write the “currentStep”:

	current_step := 'INVALID STEP';
	FOR i := 0 TO SIZEOF(textEnum)/SIZEOF(textEnum[0])-1 DO
		IF textEnum[i].enum_val = en_test THEN
			current_step := textEnum[i].text;
			EXIT;
		END_IF
	END_FOR

That way your array does not have to have the same order than your enum.

1 Like

If the variable is also activated as on OPC UA variable, this information can be read out with OPC UA client function blocks. This EnumStrings can be reached by adding /0:EnumStrings to the browsepath of this enum variable. I have a sample package, but I have not put it yet on GitHub.

See also following link to AS-Help topic :

Detailed information about data type Enumeration

B&R Online Help (br-automation.com)

Hi @c418917,

It looks like you got some good suggestions from other community members, and it has been a couple of weeks since the last activity on this post. Can you please mark the reply that helped you the most as the solution? Or if you still have open questions on this topic, can you please provide an update?

Here is the GitHub link for the sample:
as-opcua-enumstrings/OpcEnum at main · br-automation-com/as-opcua-enumstrings (github.com)

2 Likes

Hi @corne.geerts ,

Nice sample!
Do you know if it’s currently possible to show the enum string directly in mapp view without using this additional OPC UA Client?

i don’t think mappview can handle browse paths.
You could use the numeric identifier to reach the array of strings, but you have to get this numeric identifier first.
This numeric identifier could problably change when a the project is changed.