FB_CsvRead Ejemplo

Descripción general

En el ejemplo siguiente se muestra cómo los valores que se leen de un archivo CSV se almacenan en el búfer que proporciona la aplicación para continuar el procesamiento.

Ejemplo de archivo CSV

Contenido del archivo example.csv con delimitador especificado como punto y coma (;):

PNo;PName;PValue;PUnit;Description
1;Velo_Max;1500;rpm;maximum velocity of the motor
2;Velo_Min;100;rpm;minimum velocity of the motor
3;Velo_ManMode_Slow;150;rpm;velocity manual mode slow move
4;Velo_ManMode_Fast;600;rpm;velocity manual mode fast move

Ejemplo de programa

PROGRAM SR_Example
VAR
  fbRead :FFU.FB_CsvRead;
  xCmdRead :BOOL;
  asCsvTable:ARRAY[0..c_uiNumOfRows-1,0..c_uiNumOfColumns-1] OF STRING(c_uiLengthOfValue);
  stCsvTable: FFU.ST_CsvTable;
END_VAR
VAR CONSTANT
  c_uiNumOfRows :UINT:= 8;
  c_uiNumOfColumns :UINT:= 7;
  c_uiLengthOfValue :UINT:= 20;
END_VAR

fbRead.i_stTableReadValues.pbyTable := ADR(asCsvTable);
fbRead.i_stTableReadValues.uiNumOfRows := c_uiNumOfRows;
fbRead.i_stTableReadValues.uiNumOfColumns := c_uiNumOfColumns;
fbRead.i_stTableReadValues.udiSizeOfTable := SIZEOF(asCsvTable);

fbRead.i_stReadParameter.sDelimiter := ';';
fbRead.i_stReadParameter.etReadMode := FFU.ET_CsvReadMode.AllValues;
fbRead.i_stReadParameter.udiNumOfRow := 0;
fbRead.i_stReadParameter.udiNumOfColumn := 0;

fbRead(
       i_xExecute:= xCmdRead, 
       i_sFilePath:= '/sd0/Example.csv', 
       i_stTableReadValues:= , 
       i_stReadParameter:= , 
       i_timTimeout:= , 
       q_xDone=> , 
       q_xBusy=> , 
       q_xError=> , 
       q_etResult=> , 
       q_sResultMsg=> , 
       q_stFileInformation=> , 
       q_stCsvWarnValueTruncated=> , 
       xValueTruncated=> );

Búfer

El búfer que proporciona la aplicación contiene los elementos que se leen del archivo CSV:

Índice de matriz

0

1

2

3

4

5

6

0

PNo

PName

PValue

PUnit

Descripción

1

1

Velo_Max

1500

rpm

velocidad máxima de

2

2

Velo_Min

100

rpm

velocidad mínima de

3

3

Velo_ManMode_Slow

150

rpm

modalidad manual de velocidad

4

4

Velo_ManMode_Fast

600

rpm

modalidad manual de velocidad

5

6

7

NOTA: Los valores de la columna Descripción se han truncado al leer el archivo porque la longitud de STRINGs en el búfer de lectura está limitada a 20 caracteres. La salida q_stWarnVal­ueTruncated del bloque de funciones indica que el valor de la fila 2 y de la columna 5 se ha truncado.