If I have a structure called sHisto:
STRUCT
History: ARRAY [1..64] OF REAL:
MaxValue: REAL;
MinValue: REAL;
AvgValue: REAL;
END_STRUCT
And I pass it to a functionblock as a pointer:
Measurement: POINTER TO sHisto;
How do I reference the members inside a functionblock? Is the following right?
Measurement.History[3] := 1.0; //set a single array value
temp := Measurement.History[3]^ ; //get a single array value
Measurement.MaxValue := 5.0; //set a member variable
temp := Measurement.MaxValue^; //get a member variable
SomePointerToReal := ADR(Measurement.MaxValue); //pass the addr of a value
SomePointerToRealArr := ADR(Measurement.History); //pass the address of an array
SomePointerTosHisto := (Measurement); //Pass the address of the structure
Best regards,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Someone will correct me if i'm wrong but when you use a pointer and want to access the values directly you need to de-reference it by using the ^. The de-reference comes after the variable pointer name and before the struct member.
So to access a pointer inside the FB you would use:
Measurement^.History[3] := 1.0; //set a single array value
temp := Measurement^.History[3] ; //get a single array value
Measurement^.MaxValue := 5.0; //set a member variable
temp := Measurement^.MaxValue; //get a member variable
If you are trying to pass the addr of a value while still in the FB you wouldn't need the ADR() since it is already referenced as a pointer. Otherwise you are passing an address of an address.
SomePointerToReal := Measurement.MaxValue; //pass the addr of a value
SomePointerToRealArr := Measurement.History; //pass the address of an array
SomePointerTosHisto := Measurement; //Pass the address of the structure
Hello,
I would like to save some values in a structure named Test
The structures is
TYPE test :
STRUCT
generalinfo :general; // 2 variables as string
testfreatures : freatures; // 2 variables as string
END_STRUCT
END_TYPE
I'm using a pointer to the struct test
VAR
INIValues1 : POINTER TO ARRAY [0..1000] OF STRING;
END_VAR
INIValues1 := ADR(Test);
// I want to write the values as follow
INIvalues1^[0].^[0] := 'Test';
How can I mange to do this... I would like later only to replace the struct so my function fits for many different structures
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To achieve what you want, you do not declare a "POINTER TO ARRAY[] OF STRING". You declare a "POINTER TO STRING". You can use the array subscript notation directly with this pointer. The array subscript notation performs dereferencing, just like using "^". In fact, ptr[0] and ptr^ are equivalent as far as I can tell.
Hi readers,
If I have a structure called sHisto:
STRUCT
History: ARRAY [1..64] OF REAL:
MaxValue: REAL;
MinValue: REAL;
AvgValue: REAL;
END_STRUCT
And I pass it to a functionblock as a pointer:
Measurement: POINTER TO sHisto;
How do I reference the members inside a functionblock? Is the following right?
Measurement.History[3] := 1.0; //set a single array value
temp := Measurement.History[3]^ ; //get a single array value
Measurement.MaxValue := 5.0; //set a member variable
temp := Measurement.MaxValue^; //get a member variable
SomePointerToReal := ADR(Measurement.MaxValue); //pass the addr of a value
SomePointerToRealArr := ADR(Measurement.History); //pass the address of an array
SomePointerTosHisto := (Measurement); //Pass the address of the structure
Best regards,
Someone will correct me if i'm wrong but when you use a pointer and want to access the values directly you need to de-reference it by using the ^. The de-reference comes after the variable pointer name and before the struct member.
So to access a pointer inside the FB you would use:
Measurement^.History[3] := 1.0; //set a single array value
temp := Measurement^.History[3] ; //get a single array value
Measurement^.MaxValue := 5.0; //set a member variable
temp := Measurement^.MaxValue; //get a member variable
If you are trying to pass the addr of a value while still in the FB you wouldn't need the ADR() since it is already referenced as a pointer. Otherwise you are passing an address of an address.
SomePointerToReal := Measurement.MaxValue; //pass the addr of a value
SomePointerToRealArr := Measurement.History; //pass the address of an array
SomePointerTosHisto := Measurement; //Pass the address of the structure
Related
Talk.ru: 3
Hello,
I would like to save some values in a structure named Test
The structures is
TYPE test :
STRUCT
generalinfo :general; // 2 variables as string
testfreatures : freatures; // 2 variables as string
END_STRUCT
END_TYPE
I'm using a pointer to the struct test
VAR
INIValues1 : POINTER TO ARRAY [0..1000] OF STRING;
END_VAR
INIValues1 := ADR(Test);
// I want to write the values as follow
INIvalues1^[0].^[0] := 'Test';
How can I mange to do this... I would like later only to replace the struct so my function fits for many different structures
To achieve what you want, you do not declare a "POINTER TO ARRAY[] OF STRING". You declare a "POINTER TO STRING". You can use the array subscript notation directly with this pointer. The array subscript notation performs dereferencing, just like using "^". In fact, ptr[0] and ptr^ are equivalent as far as I can tell.
Thannk you fajean i replied to you in the folllowing link https://forge.codesys.com/forge/talk/Engineering/thread/b64911d912/?limit=25#0c0f