codesys variables

I rolled my own local variables back in my rs logix days by adding the sub routine number to the sub routine bits and integers.
aka. If it was sub routine 11 all my bits and ints generated in the routine started with 11. i.e. B113, N117.
Just don't see the need now.
I ask this as so machine I don't know how to create local variables. wago, no problem, as the codesys is more vanilla.
 
The old PLC/SLC Rockwell controllers have only "Global" data tables; they can be accessed by any routine in the controller.

Most modern controllers allow you to restrict the scope of a Variable so that it is local to a Subroutine or a Function. In Rockwell, they're "Program Scope" or part of an Add-On Instruction.

In CoDeSys, they're considered Global or Local.
 
Are you asking why you would bother with creating local instead of just making everthing global?
 
but other than exporting sub routines, what are the advantages?

Encapsulation : you don't need to know which variables or code uses internally a function or subroutine to do its work, then it's better that those variables to be hidden to the rest of the program.
 
Easy re-use of code. Say you have multiple machines one one controller that do the exact same thing (yes, it really happens). The logic can be reused, just mapping to different I/O.
 
I've agreed w/ the advantages mentioned.
aka Encapsulation, porting.

But for now, I've decided to make them all global, as I can export the tags via unity to a .txt file. rename the file to a .csv, open in excel, manipulate the info easily, paste into notepad+, yada yada yada, all tags are now in the project.

Only took me 3 hours. Much less if I didn't keep stopping to read help files.

Hopefully I can do the same w/ the FB instances on Monday.
 
Last edited:
Just need to figure out the modbus reads and writes, and how to make a cold start not overwrite my hmi entered set points.
 
Modbus is easy. Just set memory address for your global variable and your are ready to go.
In order to save variable values after cold restart you need to save them as persistent but a persistent variable cant have an address to be accessed directly using Modbus.
I have just started using Codesys (Somachine) myself. The first project had a M241 PLC and an Exor HMI communicating through Modbus Rtu.
What I did was first to create a persistent variables list and map them to my global variables that needed to be saved in non volatile memory.
Then I created a POU that runs on the first plc cycle in order to load the values of the persistent variables into global variables. I created a POU that runs only when I need to save the global variables into persistent list. To do this I create a SAVE PARAMETRES button on HMI to call this POU.

Maybe there is a better way to handle situations like the aforementioned but I'm not aware of it yet.
 
have a look at oscat.de it has a nice function library for it uses a lot of local variables, when you import functions you dont have to bother about all these vars.
Nowadays the speed of processors is so high no need to do it global.
when the programs get big local is better as the vars can be shorter name.

For vars that need to be saved, it is a lot simpler to make a update from the locals to the global vars.
 

Similar Topics

Hello TwinCAT and CODESYS experts: kolyur, AustralIan, busarider29, et. al. I will be once again very glad to get your expert advice on this one...
Replies
4
Views
2,046
Is there any way to bind more than one variable to a Text field? I want to be able to doing something like this <Value> <Unit> 104.6 F or...
Replies
11
Views
3,942
Does anyone have an idea on whether CODESYS NETWORK VARIABLES are the same as NI PXI NETWORK VARIABLES I will program a Codesys based Schneider...
Replies
2
Views
2,683
Hi, I am new to codesys programming and need some help converting a word variable into 2 byte variables. If anyone provides the sample in Codesys...
Replies
9
Views
22,834
I have an existing array in Global Variables, Test_Array: ARRAY [0..3,0..20] OF DINT that is working fine. I can see the values changing when I...
Replies
4
Views
5,208
Back
Top Bottom