TwinCAT 3 CONSTANT initialization

kalabdel

Member
Join Date
Feb 2015
Location
Ontario
Posts
1,108
Hello gentlemen,


Why does this work:
Code:
VAR CONSTANT
    SIZE_OF_dutEVENT : UDINT := SIZEOF (DUT_Event);
    MAX_SIZE_OF_dutEVENT : UDINT := 5;
    NUMBER_OF_BYTES_FOR_ALL_dutEVENTS : UDINT := MAX_SIZE_OF_dutEVENT * SIZE_OF_dutEVENT;
    
END_VAR
And this doesn't work, I get the error :Initialisation of constant variable 'MOTOR_DATA_LOWER_BOUND ' not constant


Code:
MOTOR_DATA_LOWER_BOUND : DINT := LOWER_BOUND(ioaMotors,1);
What am I missing?




Thanks
 
from here:
The operators LOWER_BOUND and UPPER_BOUND can be used to determine the index limits of the array that is actually used at runtime.
Quantities determined "at runtime" cannot be constants.


Perhaps it could be run the other way? Onstead of using a deco,a; value for the lower bound of the declaration of the array, declare the constant as the desired lower bound and then use the constant in the declaration of the array. I.e. instead of
Code:
 VAR
   ioaMotors : ARRAY [4..10] of DINT;
END_VAR
VAR CONSTANT
   MOTOR_DATA_LOWER_BOUND : DINT := LOWER_BOUND(ioaMotors,1);
END_VAR
do this
Code:
VAR CONSTANT
   MOTOR_DATA_LOWER_BOUND : DINT := 4;
END_VAR
VAR
   ioaMotors : ARRAY [MOTOR_DATA_LOWER_BOUND..10] of DINT;
END_VAR
Which appears to be valid cf. here.
 
Last edited:
from here:
Quantities determined "at runtime" cannot be constants.


Perhaps it could be run the other way? Onstead of using a deco,a; value for the lower bound of the declaration of the array, declare the constant as the desired lower bound and then use the constant in the declaration of the array. I.e. instead of
Code:
 VAR
   ioaMotors : ARRAY [4..10] of DINT;
END_VAR
VAR CONSTANT
   MOTOR_DATA_LOWER_BOUND : DINT := LOWER_BOUND(ioaMotors,1);
END_VAR
do this
Code:
VAR CONSTANT
   MOTOR_DATA_LOWER_BOUND : DINT := 4;
END_VAR
VAR
   ioaMotors : ARRAY [MOTOR_DATA_LOWER_BOUND..10] of DINT;
END_VAR
Which appears to be valid cf. here.

Thanks drbitboy. You are correct, I missed that. I will have to look for a different approach.
ioaMotor_Data[*] is In_Out variable length array as per your first link. I will rework the structure to avoid constants altogether. Or some other way so it is calculated at compile time.
 

Similar Topics

I am using twincat 3 to send some strings over TCP/IP. Where the server is a sensor and my PLC is the client. I noticed that the sensor didnt...
Replies
2
Views
88
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
224
Hi! I am trying to run the 'SimpleSample' (https://infosys.beckhoff.com/content/1033/TF5100_TC3_NC_I/Resources/3438746891/.zip ) to understand the...
Replies
2
Views
107
I am developing a library in twincat and one of the function uses IID_ITcVnAccess_TcVnPoint2_DINT and the definition of this type is defined in...
Replies
0
Views
73
Sorry if this has been asked before, and apologies if this seems like a trivial issue, but I am new to Beckhoff and have been banging my head...
Replies
2
Views
156
Back
Top Bottom