- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-13 10:53 PM
How to create typed enum?
Hi,
It seems that enumerations default to a DINT type.
I would like to change the type to a USINT so I can avoid casting every use of the enum.
I also don't want the type to be DINT across the project because I don't need the range of a DINT and it just makes program heavier to run.
The enum is per the following
State: (
Idle := 0,
Pending := 2,
Active := 3,
Fault := 4
);
I've tried casting in the following manners;
State: (
Idle := TO_USINT(0),
Pending := TO_USINT(2),
Active := TO_USINT(3),
Fault := TO_USINT(4)
);
State: (
Idle := 0,
Pending := 2,
Active := 3,
Fault := 4
) USINT;
Thanks.
- Labels:
-
HVAC
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-13 11:12 PM
Hi,
unfortunately there is not a way to specify the type of an Enumeration, which is assumed by the compiler as a DINT.
You should write:
usiVar := TO_USINT(State #Idle);
If you want to force a different type, I suggest to use CONSTANT variable, in this case you can specify the type.
If you want to define the type of an explicit value in your code, you can write USINT#5 and keep TO_USINT() for casting of variables, expressions.
Regards,
Federico
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-13 11:12 PM
Hi,
unfortunately there is not a way to specify the type of an Enumeration, which is assumed by the compiler as a DINT.
You should write:
usiVar := TO_USINT(State #Idle);
If you want to force a different type, I suggest to use CONSTANT variable, in this case you can specify the type.
If you want to define the type of an explicit value in your code, you can write USINT#5 and keep TO_USINT() for casting of variables, expressions.
Regards,
Federico
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-13 11:26 PM
I was hoping to avoid having to cast each use of the enum.
Constant variables have lots of overhead because they have to be defined as VAR_EXTERNAL anyway.
DINT will have to suffice.
Thanks @FedericoM
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-13 11:38 PM
Global constants can be used inside functions and fbs without the need of additional declaration inside the block
Federico
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-20 11:39 PM
As far as I know, with codesys you can : CODESYS Online Help
TYPE COLOR : ( white := 16#FFFFFF00, yellow := 16#FFFFFF00, green := 16#FF00FF00, blue := 16#FF0000FF, black := 16#88000000 ) DWORD := black; // Basic data type is DWORD, default initialization for all COLOR variables is black END_TYPE
Extensions to the IEC 61131-3 standard
The basic data type for an enumeration declaration is INT by default. However, you can also declare enumerations that are based explicitly on another integer data type.
basic data type : INT | UINT | SINT | USINT | DINT | UDINT | LINT | ULINT | BYTE | WORD | DWORD | LWORD
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-04-21 12:21 AM
Hello,
the question was related to the software Ecostruxure Machine Expert HVAC, which is not Codesys based.
Federico
- Tags:
- english
Link copied. Please paste this link to share this article on your social media post.

