Machine Automation Forum
A forum addressing machine automation solutions for the complete machine lifecycle. Including offers like Machine Advisor, Modicon PLC/PacDrive, Lexium or Preventa. Discuss and share knowledge on offers relating to cloud-based service platforms, machine localization and monitoring, industrial operations control, motion products as well as safety function!
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-03-03 08:34 PM
Hi,
Per above, I am received an error G0101 for "Type mismatch in assignment".
I have created a constant structure that I use for state control.
I use it in place of an enumeration because of it's ability to capture type within the structure.
Below is an example of a struct I was able to reproduce the error upon.
TestStruct_t: STRUCT
item0 : USINT := 0;
item1 : USINT := 1;
item2 : USINT := 2;
END_STRUCT;
I instantiate it so that it may be used. Note that because it is a constant, I have to initialise the members.
VAR_GLOBAL
cnt : INT;
usiTestAssignment : USINT;
END_VAR
VAR_GLOBAL CONSTANT
AConstStruct : TestStruct_t := (item0 := 0, item1 := 1, item2 := 2);
END_VAR
Upon use for an assignment
usiTestAssignment := AConstStruct.item0;
the following error results
MAIN(3) - error G0101: USITESTASSIGNMENT => Type mismatch in assignment
As far as I can tell, both 'usiTestAssignment' and 'AConstStruct.item0' are of the same type.
I can resolve this issue by doing change to the line below, instead of using the previous assignment, but this defeats sort of defeats the purpose for using the constant structure in the first place.
usiTestAssignment := AConstStruct.item0 + TO_USINT(0);
How can I resolve this issue without having to use the 'hack' of 'TO_USINT(0)'?
Thanks.
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-03-03 10:53 PM
The problem are the inital values together with the attribut constant on a struct.
If you did not use the attribut constant you don´t need to define in the declaration of the variabele a init value.
If you declare the variable in this way it works proper.
At the moment I did not understand the purpose to use attribut of constant in this behaivor.
best regards
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-03-03 10:53 PM
The problem are the inital values together with the attribut constant on a struct.
If you did not use the attribut constant you don´t need to define in the declaration of the variabele a init value.
If you declare the variable in this way it works proper.
At the moment I did not understand the purpose to use attribut of constant in this behaivor.
best regards
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2022-03-06 03:05 PM
I'm using constant on a struct to approximate the functionality of a typed enum like in C/C++.
When creating an enum in IEC, I don't seem to have the ability to set its type, and hence when using it, I have type cast every instance of the enum to prevent compiler warnings.
My alternative to use a struct to specify the type, and use const keyword, to prevent accidental overwrite of members in the struct.
I'll remove const, but I feel like the compiler should be able to handle it in this instance.
Thanks,
Jack
Link copied. Please paste this link to share this article on your social media post.
Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.