Structured Text - ENUM to String?


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
        logStr := 'X_GOTO_POS_REF';

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)

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

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

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

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;

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 (