Post by damian177 on Persistence variables PFC200
CODESYS Forge
talk
(Post)
Hi, I using persistence variables like below: VAR_GLOBAL PERSISTENT RETAIN id_worker1: ARRAY [0..3] OF INT := [231,234,176,222]; id_worker2: ARRAY [0..3] OF INT := [211,129,125,221]; Initial values can cause some problems?
Last updated: 2023-12-08
Post by john-robinson on Limiting Memory Access of an Array to Within its Bounds
CODESYS Forge
talk
(Post)
Thanks Tim! This was exactly the functionality we were looking for. We are already using implicit checks to check for divide by 0 errors, we will add implicit checking for array bounds.
Last updated: 2024-03-06
Post by xabier on Remove white spaces in file .txt
CODESYS Forge
talk
(Post)
Hi, I want write a array with line break in file.txt (I attacehd the screen array) and when i check the file appear with white spaces (I attached file .txt ) Somepeople know how remove this white spaces?? Thanks you Regars XABI
Last updated: 2024-04-22
Post by fless on FB having single input but initialized with Array
CODESYS Forge
talk
(Post)
with every call of the FB500 it keeps adding the INTs of the array to the sum variable. set sum to 0 before you start the loop.
Last updated: 2024-05-06
Post by ph0010421 on How to manage variable types larger than 64 bits - Ethernet/IP
CODESYS Forge
talk
(Post)
So it's currently mapped to an array of BYTES? Can you create a UNION? TYPE sBytesString : UNION AsBytes: ARRAY[0..127] OF BYTE; AsString: STRING(128); END_UNION END_TYPE Map it to the bytes, read it in the STRING!
Last updated: 2024-09-23
Post by pernockham on Is there a pragma for init/instantiation of local variables in FB (like in Methods)
CODESYS Forge
talk
(Post)
Never seen VAR_TEMP before, but seems to be what I was looking for! Thank you!!
Last updated: 2024-11-05
Post by hazarath on How extract JSONElement containing Array data
CODESYS Forge
talk
(Post)
I want to extract each element of an Array. Can someone help me with this. When I use JSONElementToString (part of JSON Utilities SL), I am seeing output as "ARRAY" instead of actual data. Here is the JSON content that I want to read : { "value1Unit": { "units": "M" }, "data": { "stepSize": 300.23, "points": [6,8] } } I would like to read each item of the element "points" i.e. 6 and 8. Here is the code I used, // Reading the content as JSON reader ( xExecute := execute , pwData := ADR ( converted_value ), jsonData := jsonDataStorage ); // Get the JSON Element jsonDataStorage.FindFirstValueByKey( wsKey := fidKeyVar, diStartIndex := searchElem, jsonElement => jsonElement ); JSON.JSONElementToString ( element := jsonElement , wsResult := valueWstring ); The content of the output of valueWstring is shown as "ARRAY" instead of the array items i.e. 6 and 8 Please can someone help me.
Last updated: 2024-07-30
Post by micik on Reverse bytes in an array
CODESYS Forge
talk
(Post)
Hello, I'm getting the data in Codesys that is an array of 8 bytes. From this array, I need to foram LREAL number, however, because of different endiannes I need to reverse bytes in this array and then copy to a LREAL variable. For this I'm using a loop and it works OK. I wonder if there is a built in function to do this. I have found CAA Memory library but it has functions like reverse bytes in DWORD. But it seems it doesn't have what I need. https://content.helpme-codesys.com/en/libs/CAA%20Memory/Current/CAA_Memory/Reverse-Bit-Swap-ByteWord-order/ReverseBYTEsInDWORD.html What I need is a function to reverse bytes in an 8 byte array, or something similar.
Last updated: 2024-08-22
Post by micik on Reverse bytes in an array
CODESYS Forge
talk
(Post)
Hello, I'm getting the data in Codesys that is an array of 8 bytes. From this array, I need to foram LREAL number, however, because of different endiannes I need to reverse bytes in this array and then copy to a LREAL variable. For this I'm using a loop and it works OK. I wonder if there is a built in function to do this. I have found CAA Memory library but it has functions like reverse bytes in DWORD. But it seems it doesn't have what I need. https://content.helpme-codesys.com/en/libs/CAA%20Memory/Current/CAA_Memory/Reverse-Bit-Swap-ByteWord-order/ReverseBYTEsInDWORD.html What I need is a function to reverse bytes in an 8 byte array, or something similar.
Last updated: 2024-08-22
Post by john-robinson on Limiting Memory Access of an Array to Within its Bounds
CODESYS Forge
talk
(Post)
Recently we had an issue regarding some simple code to calculate a rolling average. The code indexes from zero to 199 to properly store the current input into a circular buffer which then allows us to calculate a rolling average: VAR input_5s : REAL; outs_arr : ARRAY[0..199] OF REAL; i : USINT := 0; END_VAR ___ //this code runs every five seconds, calculating a rolling average outs_arr[i] := input_5s; i := i + 1; output := OSCAT_BASIC.ARRAY_AVG(ADR(outs_arr), SIZEOF(outs_arr)); IF i >= SIZEOF(outs_arr) THEN i := 0; END_IF There is a simple bug in this code where the index will be set to 0 when it has surpassed the length of the array in bytes (800 in this case) rather than larger than the number of reals in the array (200). The solution here is simple, replacing i >= SIZEOF(outs_arr) with i >= SIZEOF(outs_arr)/SIZEOF(outs_arr[0]). In this example when the index increased to 201 and the line outs_arr[201] := input_5s was called, codesys arbitrarily wrote to the address in memory that is where outs_arr[201] would be if the array was that long. I would like to find a way to wrap the codesys array inside of a wrapper class that checks if an input is within the bounds of an array before writing to that value. I know how I would implement that for a specific array, I could create a method or class that takes an input of an array of variable length, ie. ARRAY[*] OF REAL, but I don't know how to make this for any data type. I am wondering if anyone has ever done anything similar to this, or has any better suggestions to ensure that none of the programmers on this application accidentally create code that can arbitrarily write to other locations in memory.
Last updated: 2024-03-05
Post by youness on No source code available (cip object)
CODESYS Forge
talk
(Post)
Hi yotaro, hope your problem was resolved. I had the same, but with an other library title. This exception is not detected during compilation, but rather at a given position in the program (when switching to a given visualization). Although the exception is generated at this point, it does not involve the visualization in question. This error is due to one of 3 reasons: 1) A division by zero somewhere: The program is able to detect divisions by zero at compile time. But in the case of a variable, which takes a valid value at init and changes to 0 at a later stage. 2) An invalid pointer: (either because it has a value of 0, or because it points outside the memory reserved for the program) is being dereferenced. Locate any pointers or interfaces you have in the code and check them - you should also be wary of mixing inline modifications and pointers. 3) Array overflow: Generally when a processing operation is executed outside the array's definition range. Example: a write loop with n iterations is executed on an array of dimmension n-1. On the other hand, the error message may not appear. In the latter case, the error may have fatal consequences, as the overflow has induced writing to potentially forbidden memory areas. This problem can be explained by the fact that it's not always the adjacent memory areas of PLC_PRG that are overwritten, but the memory areas that are randomly allocated to the array during compilation. In this case, however, there is no entry in the log, so you need to integrate the "Implicit Check function", which checks the line of code generating the error. To integrate this functions, click on Application --> POU for implicit controls Regards,
Last updated: 2024-07-16
Post by ihatemaryfisher on Sorting array of any-sized structure
CODESYS Forge
talk
(Post)
In my machine's operation, I need to display multiples tables containing arrays of structured variables. The arrays change during operation, and my supervisor has advised me to write a new bubble-sort for each array. I think I can make a function to sort an array of any data type. This was my own project, and I'm a relatively new coder. I want to know the weaknesses in my approach, and a better method, if one exists. As far as I can test, the function accepts an array of a structured variable of any size, and sort it by any VAR in that structure. But it relies heavily on pointers, which I've heard are bad practice? Function call: // SORT BY BYTE-SIZED VAR IF xDoIt[6] THEN FUNBubbleSortSansBuffer( IN_pbySourcePointer := ADR(astArray[1]), // address of first byte in first element of array IN_pbyComparePointer:= ADR(astArray[1].byCompByte), // points to first byte of the comparing variable (variable you sort by) IN_uiStructureSize := SIZEOF(TYPE_STRUCTURE), // size, in bytes, of the structured variable IN_uiCompareSize := SIZEOF(astArray[1].byCompByte), // size, in bytes, of the comparing variable (variable you sort by) diArrayElements := UPPER_BOUND(astArray,1), // number of elements in array IN_xSmallToLarge := xSortOrder // whether to sort by small2large or large2small ); END_IF Function: FUNCTION FUNBubbleSortSansBuffer : BOOL VAR_INPUT IN_pbySourcePointer : POINTER TO BYTE; // points to beginning of array (first byte of first element) IN_pbyComparePointer: POINTER TO BYTE; // points to first byte of the comparing variable (variable you sort by) IN_uiStructureSize : UINT; // size, in bytes, of the structured variable IN_uiCompareSize : UINT; // size, in bytes, of the comparing variable (variable you sort by) diArrayElements : DINT; // number of elements in array IN_xSmallToLarge : BOOL; // whether to sort by small2large or large2small END_VAR VAR j : DINT; // repeat iteration over array until array ends i : DINT; // iterarte over array, swapping when necesary k : DINT; // iterator from 1 to size of structure (stepping 'through' a single element in array) dwSize : DWORD; // internal var for use in MEMUtils.MemCpy(<size>) // FOR SORTING BY BYTE VAR pbySourcePointer : POINTER TO BYTE; pbySourcePointer2 : POINTER TO BYTE; pbyComparePointer : POINTER TO BYTE; pbyComparePointer2 : POINTER TO BYTE; pbyPointerToBuffer : POINTER TO BYTE; // pointer to single byte buffer byBufferByte : BYTE; // single byte buffer END_VAR dwSize := UINT_TO_DWORD(IN_uiStructureSize); // get structure size (number of bytes) pbyPointerToBuffer := ADR(byBufferByte); // assign pointer to address of buffer byte (because MEMUtils.MemCpy requires a pointer input) CASE IN_uiCompareSize OF // depending on the size of the VAR to sort by (current functionality for BYTE and WORD/INT 1: // BYTE (8 BIT) FOR j := 1 TO diArrayElements DO // for number of elements in array FOR i := 1 TO (diArrayElements-1) DO // same thing, but row[i+1] row is included in swap logic pbySourcePointer := IN_pbySourcePointer + dwSize*(i-1); // point at #1 byte in array element[i] pbySourcePointer2 := pbySourcePointer + dwSize; // point at #1 byte in array element[i+1] // NOTE: because of memory locations, each array element is offset from one another by a number of bytes equal to the size of the structure // We can "walk" from array[i] to array[i+1] via steps equal to the size of the structure // e.g., ADR(array[i+1]) == ADR(array[i]) + SIZEOF([array datatype]) pbyComparePointer := IN_pbyComparePointer + dwSize*(i-1); // point to sorting variable in array element[i] pbyComparePointer2 := pbyComparePointer + dwSize; // point to sorting variable in array element[i+1] // using sort order (small -> large/large -> small) IF SEL(IN_xSmallToLarge, (pbyComparePointer2^ > pbyComparePointer^),(pbyComparePointer2^ < pbyComparePointer^)) THEN // This is where it gets tricky. We've identified pointers for the starting bytes of aArray[i] and aArray[i+1] // and we know the size of aArray[i]. We are going to swap individual bytes, one at a time, from aArray[i] and aArray[i+1] // this allows us to use only a single byte var as a buffer or temporary data storage // e.g., consider a structure consisting of a word, a byte, and a string. it is stored like this // |------WORD-------| |--BYTE-| |STRING------...| // astArray[1] == 1000 0100 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 0001 0010 0100 1000 0011 1100 0101 0101.... etc // performing a single swap (copy into a buffer, etc.) of the first byte of each array element creates this // astArray[1] == 0001 0100 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 1000 0010 0100 1000 0011 1100 0101 0101.... etc // incrementing the pointer adresses for the swap by 1 and swapping again swaps the next byte in each array element // astArray[1] == 0001 0010 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 1000 0100 0100 1000 0011 1100 0101 0101.... etc // continuing this from k to SIZEOF(TYPE_STRUCTURE) results in a toally swapped row FOR k := 1 TO IN_uiStructureSize DO // copy single byte[k] of array element 1 to buffer MEMUtils.MemCpy(pbyDest := (pbyPointerToBuffer), pbySrc := (pbySourcePointer+k-1), dwSize := 1); // copy single byte[k] of array element 2 to 1 MEMUtils.MemCpy(pbyDest := pbySourcePointer+k-1, pbySrc := (pbySourcePointer2+k-1), dwSize := 1); // copy buffer to byte[k] array element 2 MEMUtils.MemCpy(pbyDest := (pbySourcePointer2+k-1), pbySrc := pbyPointerToBuffer, dwSize := 1); END_FOR END_IF END_FOR END_FOR
Last updated: 2023-08-17
Post by rodberna on Library for arrays
CODESYS Forge
talk
(Post)
Hi, I'm creating a program where I need to handle arrays, like: - Declare lenght variable arrays - [array(1..var) of "sometype"]. This is not possible in pure codesys, right? - Know array lenght, min, max, avg values in runtime - append/delete itens in an array - and so on Is there a library know for this in codesys. Is it possible to build our own libraries using some high level language like python or java script?
Last updated: 2024-01-10
Post by szotyi on Array of Program
CODESYS Forge
talk
(Post)
Hi! What is the proper and the easiest way to call programs through looping of array of programs, or pointer of program? instead of calling the programs one by one: prog1(inputX: 11); prog2(inputY: 12); prog3(inputZ: 13); similar like: VAR programsToCall : ARRAY [1..3] OF PROGRAM; END_VAR programsToCall[1] := prog1(inputX: 11); programsToCall[2] := prog2(inputY: 12); programsToCall[3] := prog3(inputZ: 13); FOR i := 1 TO 3 DO programsToCall[i] (); END_FOR Thanks.
Last updated: 2024-08-28
Post by wollvieh on Array of UDT Compare
CODESYS Forge
talk
(Post)
use sysmemcmp. https://content.helpme-codesys.com/en/libs/SysMem23/Current/POUs/SysMemCmp.html
Last updated: 2024-04-08
Post by kislov on Table How to set row number from 1 ( not from 0)
CODESYS Forge
talk
(Post)
Use declaration with ARRRAY [1..x] instead of ARRAY [0..x]
Last updated: 2024-05-27
Post by bertrand-major on Cancelling a selection from a "combobox array" in Visu
CODESYS Forge
talk
(Post)
Hi Joanm, Did you get an answer from anybody in anather section ?
Last updated: 2024-07-17
Post by vladimirsmall on Send data to USB
CODESYS Forge
talk
(Post)
Hello/ Need send some file ( for example Array of string) to USB. Which library need used for this. Thank you
Last updated: 2024-07-20
Post by tvm on Cannot pass array of constant size to a function as a reference
CODESYS Forge
talk
(Post)
maybe this would be a better approach, then you don't have to pass the constant at all. FUNCTION fun : INT VAR_IN_OUT arr: ARRAY[*] OF INT; END_VAR VAR lower: DINT; upper: DINT; END_VAR lower:= LOWER_BOUND(arr, 1); upper:= UPPER_BOUND(arr, 1); see here as well https://help.codesys.com/api-content/2/codesys/3.5.12.0/en/_cds_datatype_array/
Last updated: 2024-01-08
Post by timvh on Limiting Memory Access of an Array to Within its Bounds
CODESYS Forge
talk
(Post)
Add the object "POU for Implicit Checks" to your application, then select "Bounds check". This will create a function which is automatically called each time an array is write accessed. In the function you can handle "out of bounds" situations. By default it will be limited to its lower or upper bound.
Last updated: 2024-03-06
Post by bjarne-pagaard on Multiple WebVisu
CODESYS Forge
talk
(Post)
You can use the switch frame variable, but there should be a variable for each active visu user. You can put the switch frame variable in an array, and use CURRENTCLIENTID as index into the array to get what you want. The CURRENTCLIENTID is assigned to new users automagically by the Visu Management.
Last updated: 2024-03-14
Post by timvh on Configuring a 2's compliment
CODESYS Forge
talk
(Post)
You could create a DUT of the Type Union and add an array of 2 bytes + an Int. Then write the byte values in the array of the Union and read the Int. Or VAR iInt : INT; byHigh : BYTE := 2#1111_1111; byLow : BYTE := 2#1111_1111; END_VAR iInt := TO_INT(byHigh*16#100 + byLow);
Last updated: 2024-09-28
Post by derpaul on PFC200 Update to 4.9.0.0 - No Connection
CODESYS Forge
talk
(Post)
Hallo Today I decided to perform an Update on my PFC200 but after the Update the Device was not reachable anymore. I tried reseting to factory reset. Software reset and so on, but nothing helps... In my Router I can see it, I cann SSH into the PFC200 but it is not reachable. Then I try to go back to 4.8.0.0 But now I get this error: [INFORMATION] Befehl SSH auf root@192.168.1.17 ausführen: Laufzeitsystem stoppen [FEHLER] Fehlerausgabe: /etc/init.d/codesyscontrol: line 34: /opt/codesys/scripts/init-functions: No such file or directory [INFORMATION] Standardausgabe: /etc/init.d/codesyscontrol: line 34: /opt/codesys/scripts/init-functions: No such file or directory Please help, I can't get this thing working... Regards
Last updated: 2023-09-27
Post by derpaul on PFC200 Update to 4.9.0.0 - No Connection
CODESYS Forge
talk
(Post)
Hallo Today I decided to perform an Update on my PFC200 but after the Update the Device was not reachable anymore. I tried reseting to factory reset. Software reset and so on, but nothing helps... In my Router I can see it, I cann SSH into the PFC200 but it is not reachable. Then I try to go back to 4.8.0.0 But now I get this error: [INFORMATION] Befehl SSH auf root@192.168.1.17 ausführen: Laufzeitsystem stoppen [FEHLER] Fehlerausgabe: /etc/init.d/codesyscontrol: line 34: /opt/codesys/scripts/init-functions: No such file or directory [INFORMATION] Standardausgabe: /etc/init.d/codesyscontrol: line 34: /opt/codesys/scripts/init-functions: No such file or directory Please help, I can't get this thing working... Regards
Last updated: 2023-09-27
Post by sturmghost on Initialization of visualization variables and cyclic code execution
CODESYS Forge
talk
(Post)
I'm looking for a smart and short way to implement initialization of visualization variables depending on the visualization input. For an easy example consider a rectangle which rests at XPos := 0 when the input state is false and at XPos := 50 when the input state is true. My visualization variables look like this: VAR_IN_OUT State : BOOL; END_VAR VAR XPos : INT; END_VAR I put this rectangle via a visualization frame element into another visualization and link a frame reference variable with the state to it. If the variable is true, the rectangle should rest at XPos := 50 and false at XPos := 0 at visualization init but how should I assign the 50 or 0 to the internal visualization variable XPos? I would need some init-methode for the visualization but I dont want a global init-method for such tasks. I want to do it inside of the visualization element but I can't see any solution for this? It would be good to be able to define ST-code within the visualization element which runs cyclic at each VISU_TASK task-cycle then I could just check the input state and change the XPos accordingly. Does someone have a solution?
Last updated: 2023-10-01
To search for an exact phrase, put it in quotes. Example: "getting started docs"
To exclude a word or phrase, put a dash in front of it. Example: docs -help
To search on specific fields, use these field names instead of a general text search. You can group with AND
or OR
.