filmov
tv
CODESYS - How to use the 'ANY' - data type

Показать описание
- ANY Data Type
- ANY Structure (TypeClass, pValue, diSize)
- Example how to use the ANY Data Type in FB
- Creating a FB to convert ANY_INT in REAL with a decimal point.
- CODESYS Generic Compare Function with ANY
NOTE: The FB must be finished if you want to use it in your project.
Some Tips:
- Set an ERROR if the passed Var doesn't match the CASE.
- Reset the Output result if the passed Var doesn't match any CASE.
- For 64 bit Var (LINT) use other data types as an output (Change the output to LREAL). You can store 32Bit Var in 64Bit Var but not the other way around.
- You don't necessarily need the THIS^.POINTER or the same METHOD to copy the bytes from the ANY to the Buffer. The THIS^.POINTER points to the members of the class and it is used to distinguish those from function members. (not only)
- You don't necessarily need the ENUM to divide the Result, but it is a good practice.
M_COPY a closer look:
_ptrDest : POINTER TO BYTE; _diBuffer : DINT = 4 Bytes [0][1][2][3] = 32 Bits;
_ptrDest := ADR(_diBuffer) = Points to the start address of the DINT or better said to the [0]Byte
_ptrDest[_diCount] := IN_ANY.pValue[_diCount] = copy the bytes from the input ANY var to the buffer var (_diCount = 0..3)
The same can be achieved with pointer dereferencing and countig the address + 1;
while (_diCount SMALLER IN_ANY.diSize) do
_ptrDest^ := IN_ANY.pValue^; // house symbol = dereferencing = Access the value of the pointed byte
_diCount := _diCount + 1;
if _diCount = IN_ANY.diSize then
RETURN;
else
_ptrDest := _ptrDest + 1; //SET THE POINTER TO THE NEXT BYTE
IN_ANY.pValue := IN_ANY.pValue + 1; //SET THE POINTER TO THE NEXT BYTE
end_if
end_while
YOU SHOULD NEVER go beyond the size of the pointed value! It‘s possible to do that and you can assign a value, but the result will not be pleasant.
_ptrDest:=adr(DINT_Var);
_ptrDest[4]:= SomeValue;
- error-prone and unexpected behavior, if your system doesn’t crash immediately.
- ANY Structure (TypeClass, pValue, diSize)
- Example how to use the ANY Data Type in FB
- Creating a FB to convert ANY_INT in REAL with a decimal point.
- CODESYS Generic Compare Function with ANY
NOTE: The FB must be finished if you want to use it in your project.
Some Tips:
- Set an ERROR if the passed Var doesn't match the CASE.
- Reset the Output result if the passed Var doesn't match any CASE.
- For 64 bit Var (LINT) use other data types as an output (Change the output to LREAL). You can store 32Bit Var in 64Bit Var but not the other way around.
- You don't necessarily need the THIS^.POINTER or the same METHOD to copy the bytes from the ANY to the Buffer. The THIS^.POINTER points to the members of the class and it is used to distinguish those from function members. (not only)
- You don't necessarily need the ENUM to divide the Result, but it is a good practice.
M_COPY a closer look:
_ptrDest : POINTER TO BYTE; _diBuffer : DINT = 4 Bytes [0][1][2][3] = 32 Bits;
_ptrDest := ADR(_diBuffer) = Points to the start address of the DINT or better said to the [0]Byte
_ptrDest[_diCount] := IN_ANY.pValue[_diCount] = copy the bytes from the input ANY var to the buffer var (_diCount = 0..3)
The same can be achieved with pointer dereferencing and countig the address + 1;
while (_diCount SMALLER IN_ANY.diSize) do
_ptrDest^ := IN_ANY.pValue^; // house symbol = dereferencing = Access the value of the pointed byte
_diCount := _diCount + 1;
if _diCount = IN_ANY.diSize then
RETURN;
else
_ptrDest := _ptrDest + 1; //SET THE POINTER TO THE NEXT BYTE
IN_ANY.pValue := IN_ANY.pValue + 1; //SET THE POINTER TO THE NEXT BYTE
end_if
end_while
YOU SHOULD NEVER go beyond the size of the pointed value! It‘s possible to do that and you can assign a value, but the result will not be pleasant.
_ptrDest:=adr(DINT_Var);
_ptrDest[4]:= SomeValue;
- error-prone and unexpected behavior, if your system doesn’t crash immediately.
Комментарии