Work with CSV files in CODESYS

How to evaluate CSV and TSV export and import in CODESYS: what changes when PLC data becomes delimited text, readability and compatibility limits, and what to verify.

Is CSV a good fit for your CODESYS data?

CSV is a reasonable choice when a CODESYS application has to hand flat, row-based data to people or tools outside the controller. Typical examples are logged measurements, batch records, parameter lists and recipe-like tables. The main benefit is reach: a delimited text file can be opened in a spreadsheet, a text editor or a script without special software, and it stays human-readable.

The main tradeoff is that CSV carries characters, not types. Every REAL, TIME, BOOL or structured value has to be converted into text on the way out and parsed back on the way in. That conversion is where precision, formatting and meaning can drift. CSV has no standard way to say which delimiter, decimal separator, character encoding or date format a file uses, so the producer and every consumer have to agree on those conventions.

Another format is usually the better choice in these cases:

  • The data is not naturally tabular, for example event messages with free-form text, nested structures or variable-length arrays. A simple line-per-event log or a structured format such as JSON or XML usually fits better.
  • The values must round-trip exactly between controllers. Text conversion of floating-point values can lose digits unless the formatting is controlled and tested.
  • The only consumer is another CODESYS component. Built-in persistence or recipe mechanisms may avoid hand-written parsing, but check what your version and device support.

What turning PLC data into CSV actually changes

Writing a CSV file from CODESYS is a stored-character transformation. Variable values are converted into strings, joined with a delimiter, ended with a line break and written to a file on the device. Nothing about the file is a visual effect. Columns, cell alignment and number formatting appear only when a spreadsheet or viewer interprets the text, and different applications can interpret the same bytes differently.

Several points in that conversion need explicit decisions:

  • Delimiter: a comma is common, a semicolon is common where the decimal separator is a comma, and a tab (TSV) avoids most conflicts with numbers and ordinary text.
  • Quoting: a field that contains the delimiter, a double quote or a line break must be wrapped in double quotes, and any embedded double quote must be doubled. This is the convention described in RFC 4180, the most widely referenced CSV description.
  • Line endings: RFC 4180 uses CR LF. In IEC 61131-3 string literals this is written as '$R$N'.
  • Number text: the result of a REAL-to-string conversion depends on the conversion function. It may include more or fewer digits than expected, or exponent notation for very large or small values. Test the exact output on your target instead of assuming a format.
  • Time and date text: IEC conversion functions for TIME and DATE_AND_TIME values typically return typed literal forms, such as a T# or DT# prefix. Spreadsheets will not treat these as times unless you reformat or post-process them.
  • Encoding: a standard STRING holds single-byte characters, and WSTRING holds wide characters. Non-ASCII text such as units, accented names or symbols can appear garbled if the reading application assumes a different encoding.

Here is a worked example of the quoting rule with a comma delimiter. Three values (a name containing a comma, a temperature and a count) produce one line. A name containing double quotes shows the doubling rule.

Input values:   Name = Pump A, line 2   Temp = 21.5   Count = 7
CSV line:       "Pump A, line 2",21.5,7

Input value:    Name = Valve "B"
CSV field:      "Valve ""B"""

A minimal Structured Text sketch for building one semicolon-separated line uses only standard conversion and concatenation functions. CONCAT in the CODESYS Standard library takes two inputs, so the line is built step by step. This sketch does not quote fields. It is safe only if the name can never contain a semicolon, a double quote or a line break.

sLine := CONCAT(sName, ';');
sLine := CONCAT(sLine, REAL_TO_STRING(rTemp));
sLine := CONCAT(sLine, ';');
sLine := CONCAT(sLine, INT_TO_STRING(iCount));
sLine := CONCAT(sLine, '$R$N');

String length is a practical limit in this kind of code. A STRING without a declared length defaults to 80 characters in CODESYS, and the classic Standard library string functions are documented to work only up to a fixed maximum length. A long line can be truncated silently. Declare buffer lengths deliberately, and check the limits of whichever string library you use.

Will the file stay readable and usable once it leaves the controller?

Copying or moving a CSV file does not change its content, because it is plain text. The risks come from how the receiving application interprets it:

  • Locale mismatch: a spreadsheet may expect a different delimiter or decimal separator than the one the PLC wrote. All data then lands in one column, or numbers are read as text.
  • Automatic conversion: spreadsheets often strip leading zeros from identifiers or reinterpret some values as dates. Re-saving the file from the spreadsheet then makes those changes permanent.
  • Encoding: without an agreed encoding, non-ASCII characters may display incorrectly. Some spreadsheet applications rely on a byte order mark to detect UTF-8, so test with the actual destination.
  • Partial files: if the controller is still writing, or loses power during a write, the last line may be incomplete. Consumers should tolerate or detect a truncated final row.

Accessibility depends mostly on the viewer. A screen reader reading raw CSV in a text editor announces delimiters and values as one stream, which is hard to follow for wide tables. Opened in a spreadsheet, the same data can be navigated cell by cell, and a clear header row lets column names be associated with values. A descriptive first row with units, such as Temp_degC, therefore helps both human readers and search within the file. These are general characteristics. No assistive-technology test results were available for this review, so check your own destination tools.

Provide a plain-text equivalent, such as a short summary or a fixed-width report, when the audience cannot be expected to open the file in a table-aware tool. The same applies when a single value, such as a final batch result, matters more than the full table.

Ways to produce or read CSV files in CODESYS

The usual approach is to combine string handling in the application with a file-access library. In current CODESYS versions, file access is commonly done through the CAA File library, which provides function blocks for opening, writing, reading and closing files. Confirm in the Library Manager of your project which file library and version is available for your device before relying on it.

Practical points that apply whichever library you use:

  • File operations on a controller are typically executed asynchronously through function blocks with execute, busy, done and error outputs. Handle every state, and do not assume a write completes in one cycle.
  • The accessible directories and path syntax depend on the runtime and device. Check where your target allows files to be created and how they can be retrieved, for example over the device's file transfer function or removable media.
  • Frequent small writes to flash storage add wear and overhead. Buffering several lines and writing them together is a common design choice.
  • Reading CSV back into the application means parsing: finding delimiters, handling quoted fields and converting text back to typed values. Validate each field and define what happens with malformed rows.

Some CODESYS features export or store data as text without hand-written file code, for example recipe handling and trace data export. Whether these produce a delimited format that fits your use, and where the commands are located, depends on the CODESYS version and installed components. Verify that in your own installation. Useful search terms for documentation and community answers include "CODESYS CAA File", "CODESYS write string to file", "CODESYS recipe file format" and "CODESYS trace export".

Evidence gaps

No confirmed subject-specific evidence was supplied for this review. That includes CODESYS version numbers, exact function block signatures, device file system paths, conversion output samples and performance figures. The guidance above relies on general CSV conventions and widely documented IEC 61131-3 and CODESYS behavior. Treat named libraries and features as starting points to check, and confirm number, time and encoding output on your actual target before you rely on the files downstream.