STRING_TO_CSVFIELD
![STRING_TO_CSVFIELD 1:](Images/gif/35894283__en-US__Web.gif)
The function converts the value of a PLC string variable to a string data field in CSV format. Single quotation marks in the source string are replaced with double quotation marks. If the bQM parameter is set (QM = quotation marks) the outer quotation marks (around the CSV data field) are also added. If successful the function returns the convert string as the result. The function returns an empty string if an error occurred during the conversion, but only if the source string was not an empty string.
The function is usually used together with the function block FB_CSVMemBufferWriter to generate data sets in the PLC memory in CSV format. In the next step the memory content can be written to the file.
The source string must not contain binary data. Binary data with the value zero would terminate and truncate the string in the wrong place. To convert binary data please use the function: ARG_TO_CSVFIELD.
FUNCTION STRING_TO_CSVFIELD: T_MaxString
VAR_INPUT
VAR_INPUT
in : T_MaxString;
bQM : BOOL;
END_VAR
in: Source string whose value is to be converted into a data field in CSV format (type: T_MaxString).
bQM: If this input is TRUE the enclosing quotation marks are added from the result string.
bQM | Description | Source string | Result string | CSV-compliant |
---|---|---|---|---|
FALSE | Source string without enclosing quotation marks should only contain letters and numbers. In this case the source string must not contain any non-printable control character, quotation mark, semicolon, comma (US CSV format) or binary data. |
|
|
|
'Module_XA5' | 'Module_XA5' | Yes | ||
'123456' | '123456' | Yes | ||
'' | '' | Yes | ||
'A""B' | 'A""""B' | No | ||
'A"B' | 'A""B' | No | ||
';' | ';' | No | ||
'$R$N' | '$R$N' | No | ||
'AB$00CD' | 'AB' (string was truncated) | No | ||
TRUE | A source string that is not enclosed in quotation marks should not contain any non-printable control character, quotation mark, semicolon or comma (US CSV format). Binary data are not permitted. '123456' '' 'A""B' 'A"B' ';' '$R$N' 'AB$00CD' | 'Module_XA5' | '"Module_XA5"' | Yes |
'123456' | '"123456"' | Yes | ||
'' | '""' | Yes | ||
'A""B' | '"A""""B"' | Yes | ||
'A"B' | '"A""B"' | Yes | ||
';' | '";"' | Yes | ||
'$R$N' | '"$R$N"' | Yes | ||
'AB$00CD' | '"AB"' (String was truncated) | No |
Example:
PROGRAM MAIN
VAR
s1 : STRING;
s2 : STRING;
END_VAR
s1 := STRING_TO_CSVFIELD( 'Module_"ALFA_$05"_6', TRUE );
s2 := STRING_TO_CSVFIELD( 'Module_50', FALSE );
The result:
s1 = '"Module_""ALFA_$05""_6"'
s2 = 'Module_50'
Further information can be found here: Example: Writing/reading of a CSV file.
Requirements
Development environment |
Target platform |
PLC libraries to be integrated (category group) |
---|---|---|
TwinCAT v3.1.0 |
PC or CX (x86, x64, ARM) |
Tc2_Utilities (System) |