Every so often questions popup about using enums in visualizations or about how to cast to an enumerated type. Here are a couple of tips that some here may find useful.
First of all, as far as I know, there is no codesys conversion or cast to enumerated types. The workaround is to remove the 'strict' attribute from the enum declaration and then use the underlying integer implementation (usually UDINT by default?).
The other thing that is very convenient is to use the 'to_string' attribute in the enum declaration. This allows enum values to be converted to text using the TO_STRING() macro/function.
In the example below an enumerated type is defined to represent color values that can be used in visualization components. It show how to convert the enum to string and to/from the underlying integer implementation.
Thanks for sharing, Rick!
One other trick I have found, when the enum comes from a library (and so can't be decorated with those attributes), is to use https://store.codesys.com/fb-visu-creator.html to generate the necessary text list for conversion.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Every so often questions popup about using enums in visualizations or about how to cast to an enumerated type. Here are a couple of tips that some here may find useful.
First of all, as far as I know, there is no codesys conversion or cast to enumerated types. The workaround is to remove the 'strict' attribute from the enum declaration and then use the underlying integer implementation (usually UDINT by default?).
The other thing that is very convenient is to use the 'to_string' attribute in the enum declaration. This allows enum values to be converted to text using the TO_STRING() macro/function.
In the example below an enumerated type is defined to represent color values that can be used in visualization components. It show how to convert the enum to string and to/from the underlying integer implementation.
Although this may be rather obvious to some, others less familiar with codesys may find the above simple example helpful.
more posts ...
Thanks for sharing, Rick!
One other trick I have found, when the enum comes from a library (and so can't be decorated with those attributes), is to use https://store.codesys.com/fb-visu-creator.html to generate the necessary text list for conversion.