Trends
Trends – Connecting Live Data from CODESYS (Generate Code Documentation)
This guide explains how to use the “Generate Code” button to connect live data from a PLC – e.g., a CODESYS project – to the PLCVisu Trend module. The goal is to cyclically write measured values (e.g., temperature, pressure, fill level) to a database, which is then visualized in the Trend Viewer.
Purpose
- Automatically write live data from CODESYS to a trend database
- Visualize that data in real time on an HMI (e.g. WAGO WP400)
- Minimal setup effort thanks to the built-in code generator in PLCVisu
Step-by-Step Guide
1. Generate Code
Click the “Generate Code” button in the Trend UI. The code will be automatically copied and can be pasted directly into your CODESYS project.
(* DECLARATION SECTION *)
trendsFBlinetrend1Instance: PLCVisu.trendsFB; (* FB instance for current trend *)
slinetrend1Columns:STRING(4096):= 'dataset1';
slinetrend1Values:STRING(4096);
(* CODE SECTION *)
(* Initiate the instance parameters: path_to_file, max_records (cyclic buffer) *)
trendsFBlinetrend1Instance.init('$$PLCVisuDB$$/trends/linetrend1.db', {{max_records}});
(* write one record into the DB *)
slinetrend1Values := PLCVisu.ELA_UTIL.CONCAT_4096('"', REAL_TO_STRING({{dataset1_value}}));
slinetrend1Values := PLCVisu.ELA_UTIL.CONCAT_4096(slinetrend1Values, '"');
trendsFBlinetrend1Instance.write('',slinetrend1Columns, slinetrend1Values);
(* write delimiter line - optional - i.e. end of the batch -> this will cut the line in front-end view *)
(* trendsFBlinetrend1Instance.writeFinish(''); *)
Optional:
UsewriteFinish('')to insert visual divider lines in the frontend view (e.g., to mark batch transitions).
2. Insert Your Sensor Variable
Replace {{dataset1_value}} with your real PLC variable, for example:
REAL_TO_STRING(GVL.TemperatureSensor1)
Notes:
- The variable must be declared as
REAL - For other types, use the appropriate converter:
INT_TO_STRING(...)for integersBOOL_TO_STRING(...)for booleans, etc.
3. Connect CODESYS & PLCVisu
To allow PLCVisu to access the data, configure the connection as follows:
In CODESYS:
- Open the Symbol Configuration
- Mark the relevant GVL variables
- Enable “Access Rights”
- CODESYS Download
In PLCVisu:
- Requirement is the setup of the connection string "eth" -> Go to connections...
- Simply go to the editor to a Pages, then select a Control and click on the Connection String application. Then a window will open up, and to see your CODESYS data simply click on the top right green button to refresh, and then you'll see your edit CODESYS data.
4. Initialize the Database
In CODESYS:
trendsFBlinetrend1Instance.init('$$PLCVisuDB$$/trends/linetrend1.db', 10000);
- Creates (or opens) the database file in the trend directory
10000defines the maximum number of stored records- Must be called once before
.write()– e.g., during project initialization
5. Write Values Cyclically
In CODESYS:
The following example writes the current sensor value during each cycle:
slinetrend1Values := PLCVisu.ELA_UTIL.CONCAT_4096('"', REAL_TO_STRING(GVL.TemperatureSensor1));
slinetrend1Values := PLCVisu.ELA_UTIL.CONCAT_4096(slinetrend1Values, '"');
trendsFBlinetrend1Instance.write('', 'dataset1', slinetrend1Values);
Caution: this function should be called in a very lw preority task, because otherwise the realtime behavior of the other Programm code can be destroyed.
Typical use case:
- Called within a cyclic task (e.g., every 1 second)
- Or triggered via a timer function block
Summary: CODESYS ↔ PLCVisu Trend
| Step | Purpose |
|---|---|
| Insert sensor value | Use *_TO_STRING(...) to convert your real variable |
| .init() | Create the database and define buffer size |
| .write() | Log values cyclically to the DB |
| Optional: .writeFinish | Create visual batch separators in the frontend |
Troubleshooting
| Problem | Possible Cause |
|---|---|
| No data visible in Trend | .init() missing or empty value |
Error on .write() |
Incorrect format (e.g., missing ',' in CSV string) |
| Database not created | Path error or write permissions missing |
| No connection to the PLC | Symbol access not enabled or incorrect IP address |
Recommendation
- Use the generated code as a template – adjust
dataset1, variable names, and file paths as needed - Need multiple signals? → Use multiple
.write()calls or definedataset2,dataset3, etc. - Test your trends with a single REAL variable first before scaling up