Storing Parameter Values in CodeSys: Using RETAIN and PERSISTENT Variables Instead of Siemens DB

Question:

Greetings to everyone! As I embark on my journey with CodeSys, I've quickly realized that unlike S7, CodeSys doesn't utilize data blocks. I am in need of a method to store various parameter values of Function Blocks (FBs). In S7, I would typically use a shared DB to save these values. In CodeSys, it appears that RETAIN and PERSISTENT variables are the way to go, although I am uncertain about the process. It is clear that I must establish a new Global variable or Structure to act as a data repository. Could you offer a straightforward example to help me kickstart this process? Essentially, I am looking for a storage solution for a boolean, an integer, and a REAL variable, making a struct an obvious choice. Your assistance is greatly appreciated!

Top Replies

mikas_m asked for help with using CodeSys and mentioned the differences in data handling compared to S7. In S7, a shared DB is used to store parameter values, while in CodeSys, RETAIN and PERSISTENT variables are required. To create a data holder, a new Global variable or Structure needs to be made. For example, a struct can be used to store a bool, int, and REAL variable. To get started, create an FB named DATA or something similar to store the data. Declare the variables as VAR_GLOBAL, marking them as RETAIN and PERSISTENT in Codesys.

The global variables are set to retain their values persistently in the PLC memory. These variables include runtime and shift time measurements, production statistics, and shift statistics. The runtime measurements are stored in seconds, minutes, and hours, while the production statistics track total count, good count, bad count, and reject count. The shift statistics similarly track total count, good count, bad count, and reject count for each shift. This setup allows for efficient tracking and monitoring of production processes.

Greetings bkottaras, thank you for your response. Instead of using struct (DUT), I will be developing a Function Block (FB). Is it feasible to input parameter data through HMI (Human-Machine Interface) and save these values within the FB? In previous projects, I frequently utilized databases to retain parameters inputted by operators through HMI.

Certainly, I see no reason why not. I utilize Wago and Weintek HMIs, both of which operate on the Modbus protocol. This includes the (4xxxxx) registers featured in this setup. Interestingly, the "Smiley face" should be a DINT value. It seems that when I copied the variables, they were mistakenly replaced with a smiley face.

I sincerely hope this information provides valuable assistance in addressing your needs.

Hello! I see you're transitioning from S7 to CodeSys. It's a different way of thinking, but once you get the hang of it, it's quite easy. You're right about Retain and Persistent variables; they serve a similar purpose to shared DBs. To define these variables, simply go to your Global Variables list and add them like any other variable, but with RETAIN or PERSISTENT modifier in front. As to your specific query, here's a simple example: STRUCT_Example : STRUCT RETAIN BOOL_BoolExample : BOOL; RETAIN INT_IntExample : INT; RETAIN REAL_RealExample : REAL; END_STRUCT You've defined a struct with a boolean, an integer, and a real variable, each retained across PLC reboots. You can now use this global struct in any function blocks or functions where you need them. Good luck with your coding!

Hey there! You're on the right track with using RETAIN and PERSISTENT variables in CodeSys. To set up a structure for your parameters, you can create a new structure type that holds your boolean, integer, and REAL values. For example, define a structure like this: ```pascal TYPE ParameterStruct : STRUCT myBool : BOOL; myInt : INT; myReal : REAL; END_STRUCT END_TYPE ``` Then, in a Global Variable list, declare a variable of this structure type with the RETAIN or PERSISTENT attribute. You might do something like this: ```pascal VAR_GLOBAL PERSISTENT myParameters : ParameterStruct; ``` Now, you can access and modify `myParameters.myBool`, `myParameters.myInt`, and `myParameters.myReal` throughout your function blocks. Just remember that PERSISTENT variables retain their values even when the PLC goes into stop mode, so they'll keep your data safe! Let me know if you have any questions as you go along!

Hey there! Welcome to the world of CodeSys! You’re on the right track with using RETAIN and PERSISTENT variables for storing your parameters. Creating a structure is definitely a solid way to consolidate your varied data types. For example, you could define a structure like this: ```pascal TYPE MyParams : STRUCT myBool : BOOL; myInt : INT; myReal : REAL; END_STRUCT; END_TYPE ``` Then, you can declare a global variable of this type with `RETAIN` or `PERSISTENT` to ensure your values are stored across reboots. For instance: ```pascal VAR_GLOBAL RETAIN params : MyParams; END_VAR ``` This will help you keep your values intact even after a controller restart. You can then access and modify `params.myBool`, `params.myInt`, and `params.myReal` as needed within your FBs. Hope this helps you get started!

Absolutely! You're on the right track with using a structure to store your parameters in CodeSys. Here's a quick example to help you out: first, define a structure that includes your variables – let’s call it `FBParameters`. It could look something like this: ```pascal TYPE FBParameters : STRUCT myBool : BOOL; myInt : INT; myReal : REAL; END_STRUCT END_TYPE ``` Next, declare a global variable of this structure type with either `RETAIN` or `PERSISTENT` based on your needs. For instance: ```pascal VAR RETAIN myFBParams : FBParameters; END_VAR ``` Now, you can easily access and modify these values within your function blocks. Just remember to initialize them as needed and you’re all set! If you have any further questions, feel free to ask!

More Replies →

Streamline Your Asset Management
See How Oxmaint Works!!

✅   Work Order Management

✅   Asset Tracking

✅   Preventive Maintenance

✅   Inspection Report

We have received your information. We will share Schedule Demo details on your Mail Id.

To add a comment, please sign in or register if you haven't already..   

Frequently Asked Questions (FAQ)

FAQ: 1. Question: Why do we use RETAIN and PERSISTENT variables in CodeSys instead of data blocks like in Siemens S7?

Answer: Answer: CodeSys does not utilize data blocks like Siemens S7, hence RETAIN and PERSISTENT variables are used to store parameter values in Function Blocks (FBs) in CodeSys.

FAQ: 2. Question: How can I store parameter values using RETAIN and PERSISTENT variables in CodeSys?

Answer: Answer: To store parameter values in CodeSys, you can create a new Global variable or Structure to act as a data repository. You can then use this structure to store boolean, integer, and REAL variables for your storage needs.

FAQ: 3. Question: Can you provide a simple example of using RETAIN and PERSISTENT variables in CodeSys to store parameter values?

Answer: Answer: Sure, you can create a struct in CodeSys to store a boolean, an integer, and a REAL variable as a storage solution. By initializing these variables as RETAIN or PERSISTENT, you can retain their values across program cycles.

Ready to Simplify Maintenance?

Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.

Request Demo  â†’