Post by rickj on Sept 25, 2021 19:54:58 GMT
The first time I attempted saving/restoring ENUM types to/from the CSV configuration file I failed miserably. It was one of those cases where I had almost gotten it right. I did much better recently and so thought I would share some tips.
Let's start by creating an eColor ENUM as follows. Since there is no codesys conversion or cast to enumerated types we must remove the 'strict' attribute from the enum. We will then use the underlying integer implementation (UDINT) in the CSV file, using the TO_UDINT() macro/function. We will also use the 'to_string' attribute so that we can also convert enum values to text using the TO_STRING() macro/function.
Let's now assume we've created a function block that implements the configuration manager interface and declare some variables using this definition as follows.
FUNCTION_BLOCK MyFunctionBlockFB IMPLEMENTS ConfigMgrInt
Now let's look at a few basic conversion examples
The ProvideConfig method performs the type conversion in preperation for saving to the CSV file. Notice that we must first convert Color_CI to an UDINT to obtain a numeric value and then convert the result to string.
METHOD ProvideConfig : INT
The Color_CI is stored in the CSV as a string containing a numeric value we can simply convert it back to a UDINT and then assign that value to the Color_CI enumerated variable.
METHOD AcceptConfig : INT
Hope this is helpful to someone.
Let's start by creating an eColor ENUM as follows. Since there is no codesys conversion or cast to enumerated types we must remove the 'strict' attribute from the enum. We will then use the underlying integer implementation (UDINT) in the CSV file, using the TO_UDINT() macro/function. We will also use the 'to_string' attribute so that we can also convert enum values to text using the TO_STRING() macro/function.
{attribute 'qualified_only'} // Values must begine with type (i.e. eColor.Red)
// {attribute 'strict'} // Comment out 'strict' to allow conversion from UDINT
{attribute 'to_string'} // Allow string conversion (i.e. TO_STRING(eColor.Red) = 'Red')
TYPE eColor :
(
Red := 16#FFFF0000, // Assign RGB color values that can be used in visualization components
Green := 16#FF00FF00, // Note: most significant byte (00-transparent, FF-opaque)
Blue := 16#FF0000FF,
Yellow := 16#FFFFFF00
) UDINT; // Explicitly specify UDINT as underlying integer implementation
END_TYPE
Let's now assume we've created a function block that implements the configuration manager interface and declare some variables using this definition as follows.
FUNCTION_BLOCK MyFunctionBlockFB IMPLEMENTS ConfigMgrInt
VAR
Color_CI : eColor;
ColorVar : eColor;
ColorTxt : STRING;
ColorVal : UDINT;
END_VAR
Now let's look at a few basic conversion examples
ColorVar := eColor.Green; // ColorVar = eColor.Green
ColorTxt := TO_STRING(ColorVar); // ColorTxt = 'Green'
ColorVal := TO_UDINT(ColorVar); // ColorVal = 16#FF00FF00
Color_CI := 16#FF00FF00; // Color_CI = eColor.Green
ColorTxt := TO_STRING(Color_CI); // ColorTxt = 'Green'
ColorVal := TO_UDINT(Color_CI); // ColorVal = 16#FF00FF00
Color_CI := ColorVar OR eColor.Red; // Color_CI = eColor.Yellow
ColorTxt := TO_STRING(Color_CI); // ColorTxt = 'Yellow'
ColorVal := TO_UDINT(Color_CI); // ColorVal = 16#FFFFFF00
The ProvideConfig method performs the type conversion in preperation for saving to the CSV file. Notice that we must first convert Color_CI to an UDINT to obtain a numeric value and then convert the result to string.
METHOD ProvideConfig : INT
ConfigArray[0] := MyTitle;
ConfigArray[1] := TO_STRING(TO_UDINT(Color_CI));
// Return the size of the resulting array (your last index + 1)
ProvideConfig := 2;
The Color_CI is stored in the CSV as a string containing a numeric value we can simply convert it back to a UDINT and then assign that value to the Color_CI enumerated variable.
METHOD AcceptConfig : INT
IF ConfigArray[00] <> '' THEN MyTitle := ConfigArray[00]; END_IF // Only replace the default if a replacement exists
Color_CI := TO_UDINT(ConfigArray[1]);
// Return the size of the resulting array (your last index + 1)
AcceptConfig := 2;
IAmConfigured := TRUE;
Hope this is helpful to someone.