• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - Sequencer
  • CSVFileReader

CSVFileReader

The CSVFileReader makes it possible to map contents from a .csv (or similar) file into Arguments.

The CSV File Format

CSV (Comma-Separated Values) files attempt to represent table-data. A CSV file is a delimiter based file, where each item is delimited (separated) by a character, typically one of ; (semicolon), , (comma), \t (tab), or some other character that is not used in the data that the file holds. The CSV file typically has a header containing the names (and order) of the data items. It can also have comments and empty lines, that the machine parser skips. Here is a short example .csv file content:

# this is a comment
A1;A2;A3
1;10;100
2;11;101
3;12;102

The file above is typically interpreted like this:

  • Line1 is a comment, and is skipped.
  • Line2 is the header. It lists the names of the values in the file. Typically, A1 is the first column, A2 is the second column, and so on.
  • Line3 contains the first set of values: A1=1,A2=10 and A3=100
  • Line4 contains the second set of values: A1=2,A2=11 and A3=101
  • Line5 contains the last set of values: A1=3,A2=12 and A3=102

Note: See the CSVFileReader_HeaderMapping Argument description for more information about mapping of the header to the .csv values.

CSVFileReader Usage

The CSVFileReader is typically configured with FileName, Delimiter and SkipPrefix. These are internal Arguments that can be set by editing them inside the CSVFileReader instance.

To tell the CSVFileReader which items from the header should be used, add arguments with names matching the CSV header names to the CSVFileReader. The Argument type must match the datatype of the content of the .csv file, or else the content will be truncated or misrepresented. Make sure to update the Line Data member with all the Arguments to emit. Connect the Read from a trigger source, and connect the Line to where you want to process the data.

For the CSV example above, add 3 Argument<unsigned int> named A1, A2 and A3 to the CSVFileReader. Select each argument and uncheck the Input property to make the arguments appear on the output side of the CSVFileReader ServiceBlock. Select Line, then select the Data property and make sure that A1, A2, A3 and Index are checked (The Index argument can be used to keep track of the sequence currently being emitted).

Note: In some cases it is useful to add data from other sources to the CSVFileReader as Arguments, and to also emit those along with the .csv data. This is allowed, and can be achieved by adding those arguments to the CSVFileReader, and then updating the Line.Data to emit them along with the .csv data.

How It Works

The CSVFileReader has two different modes, selectable by HeaderMapping. It is possible to choose between HeaderMapsToColumns (the default) and HeaderMapsToRows.

If HeaderMapping=HeaderMapsToColumns; when the CSVFileReader Read event is triggered, the following happens (pseudo-code):

  • The file is opened and read line by line. If error, Error is emitted.
  • The first non-empty, non-skipped line is identified as the header, and is mapped against the arguments in Line.Data. If error, Error is emitted.
  • For each line in the file, map values from that line into the corresponding arguments. If error, Error is emitted, else a Line EventOut is emitted:
  • In the first Line, A1=1, A2=10, A3=100 and Index=0.
  • In the second Line, A1=2, A2=11, A3=101 and Index=1.
  • In the third Line, A1=3, A2=12, A3=102 and Index=2.
  • The file is closed and Done is emitted.

If HeaderMapping=HeaderMapsToRows; when the CSVFileReader Read event is triggered, the following happens (pseudo-code):

  • The file is opened and read line by line. If error, Error is emitted.
  • The first non-empty, non-skipped line is identified as the header, and is mapped against the arguments in Line.Data. If error, Error is emitted.
  • Each line in the file is read, and values from that line is mapped into an array. If error, Error is emitted.
  • When the amount of lines read matches the number of items in the header, values from the array are mapped in a left to right manner (column by column) into the correct arguments. Each data-set is emitted using the Line EventOut:
  • In the first Line, A1=1, A2=2, A3=3 and Index=0.
  • In the second Line, A1=10, A2=11, A3=12 and Index=1.
  • In the third Line, A1=100, A2=101, A3=102 and Index=2.
  • The file is closed and Done is emitted.

Events and Arguments

The CSVFileReader ServiceBlock has the following events:

NameTypeDescription
InitInputUsed for initialization setup and chaining (CSVFileReader Init just emits Inited)
ReadInputEmits the CSV content as Line events according to how the CSVFileReader has been configured
InitedOutputThe CSVFileReader has been initialized
LineOutputSends one coherent set of data according to the header and configured output Arguments. Make sure to edit the Data member of Line to select which arguments should be passed in the Line message.
ErrorOutputAn error has occurred, check ErrorText for more information.
DoneOutputSignifies that the last data has been emitted.

The following Arguments can be used to configure the CSVFileReader:

Argument NameTypeValueDescription
FileNameInputRelative or full native path nameThe file name to read from, with the optional path prefix, relative to the application folder
DelimiterInputAny single character, or '\t'Character used as a separator for the data, typically ';'. If the tab is used as a separator, set the Delimiter to '\t' (without the quotes).
SkipPrefixInputAny stringAny line that starts with this string is skipped. Note also that empty lines are skipped.
HeaderMappingInputHeaderMapsToColumns, HeaderMapsToRowsSpecify whether the header maps to Columns (ordinary .csv mapping), or whether it maps to rows. If it maps to columns, then the number of columns (horizontal items) must match the number of header items. If it maps to rows, then the number of rows (vertical items after the header) must match the number of header items. Any mismatch will cause an Error to be emitted.
IndexOutput[0..n]0-based index that is incremented for each data-set emit. When HeaderMapping=HeaderMapsToColumns: Index equals the current row number. When HeaderMapping=HeaderMapsToRows: Index equals the current column number.
ErrorTextOutputA descriptive textA text containing the cause of the error being emitted in the Error EventOut.
<Header-item>Outputmapped from .csvAdd Arguments to the CSVFileReader and name the items exactly the same as the items in the .csv header. Any arguments matching names in the .csv header will get the corresponding values from the csv-file.

Troubleshooting

During debugging and testing, we recommend connecting the Error EventOut to a Sequencer::DebugPrinter ServiceBlock to quickly identify issues caused by incorrect configuration or corrupt .csv files.

See also MessageArgument, ReceiveMessage, SendMessage, DebugPrinter, Basic Block, and Service Blocks.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top