Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Free way to read and write CSV

galexis
2021-05-31
2021-06-27
  • galexis - 2021-05-31

    Hi,
    I want to read and write CSV. What is the best way to do it for free ? And have you example ?
    Thank for your help.

     
  • galexis - 2021-06-01

    Thank you, but I ask question because I didn't find recent solution with example.

     

    Last edit: galexis 2021-06-01
  • fleaplc

    fleaplc - 2021-06-10

    an example is pretty huge...and it'll referred to a specific file. In terms of concept you had to:
    1-open the csv file (FILE.Open FB) --> get header of file (file.CAA.HANDLE)
    2-using the header you read the 1st row (FILE.Read FB)
    https://help.codesys.com/webapp/VIPLMVOpanJfjqQXFKWGQlzGjxw%2FOpen;product=CAA%20File;version=3.5.17.0
    3-you have to update the header to the last character read (File.GetPos --> File.SetPos)
    4-read the 2nd row (FILE.Read FB)
    ......
    x - close the file (FILE.Close)

    I suggest to read Codesys help and try to read a small piece of a csv, then you'll can proceed to write something into csv

     
  • galexis - 2021-06-27

    For read, my code is limit to 255 butes. How read more ?

    PROGRAM CHARGER_LISTE_OP_CSV
    VAR
        Hfile: DWORD;
        //Adresse du fichier avec la liste des noms d'opérateurs
        FilePathAndName: STRING;
        //Caractère de fin de ligne et retour chariot
        CRLF: STRING(2):='$R$N';
        //Fonction ouvrir le fichier
        fopen : FILE.Open;
        //Fonction lire le fichier
        fread: FILE.Read;
        //Fonction fermer le fichier
        fclose: file.Close;
        //Fonction obtenir la taille du fichier en bytes
        fgetsize: FILE.GetSize;
        //Contenu du fichier limité à 255 butes ou caractères
        sFileString:    STRING(255):='';
        //Numéro action en cours: 1=ouvrir csv , 2=taille du fichier, 5= lire csv, 9=Lecture réussit: transfert du tampon vers la table, 10= fermer le fichier
        Num_Etape: INT;
        //Extraction des noms en cours
        En_Cours: BOOL;
        //Liste des nom d'opérateur
        Liste_Op_Buffer: ARRAY [1..50] OF STRING(40);
        //Longueur de la chaine de caractères récupérée dans le CSV
        Longueur_Chaine: INT;
        //Longueur du nom extrait
        Longueur_Nom: INT;
        //Position du caractère ";"
        Position_separateur: INT;
        //Numéro dans la table du nom de l'opérateur
        Ligne_Tab_Op: INT;
        //Taille du CSV en bytes déterminé par fonction fgetsize
        Taille_Csv: CAA.SIZE;
        //Nombre maxi de nom dans la liste
        Maxi_Liste: INT;
        //Nombre de butes lu par fonction fread
        fread_szSize: CAA.SIZE;
    END_VAR
    
    
    (*Charger la liste des nom d'opérateurs*)
    
    (*PARAMETRES*)
    FilePathAndName:= '/home/partage_pi/operateurs.csv';
    Maxi_Liste:=50;
    
    
    (*FONCTIONS*)
    //Fonction ouverture CSV
    fopen(
    xExecute:= (Num_Etape=1),
    xDone=> ,
    xBusy=> ,
    xError=> ,
    sFileName:= FilePathAndName,
    eFileMode:= FILE.MODE.MREAD,
    xExclusive:= ,
    eError=> ,
    hFile=> );
    
    //Fonction déterminer la taille du csv
    fgetsize(
    xExecute:= (Num_Etape=2),
    xDone=> ,
    xBusy=> ,
    xError=> ,
    sFileName:= FilePathAndName,
    eError=> ,
    szSize=> );
    
    //Fonction lecture CSV
    fread(
    xExecute:= (Num_Etape=5),
    xAbort:= FALSE ,
    udiTimeOut:=100000,
    xDone=> ,
    xBusy=> ,
    xError=> ,
    xAborted=>,
    hFile:=hFile,
    pBuffer:=ADR(sFileString),
    szBuffer:=Taille_Csv,
    eError=> ,
    szSize=> fread_szSize,);
    
    
    //Fonction fermeture CSV
    fclose(
    xExecute:= (Num_Etape=10),
    xDone=> ,
    xBusy=> ,
    xError=> ,
    hFile:= Hfile,
    eError=> );
    
    
    (*GESTION*)
    IF GVL.CHARGER_LISTE_OP=TRUE THEN
        //Init
        IF En_cours=FALSE THEN
            //Effacer la table tampon
            FOR Ligne_Tab_Op:=1 TO Maxi_Liste DO
                Liste_Op_Buffer[Ligne_Tab_Op] := '';
            END_FOR
            Num_Etape:=1;
            sFileString:='';
            Ligne_Tab_Op:=1;
            fread_szSize:=0;
            Taille_Csv:=0;
            Hfile:=0;
            fopen.xExecute:=FALSE;
            fgetsize.xExecute:=FALSE;
            fread.xExecute:=FALSE;
    
            En_Cours:=TRUE;
    
        END_IF
    
        CASE Num_Etape OF
            0:
                En_Cours:=FALSE;
                GVL.CHARGER_LISTE_OP:=FALSE;
    
            1: (*Ouvrir le fichier*)
                IF fopen.xDone THEN
                    Hfile:=fopen.hFile;
                    Num_Etape:=2;
                END_IF
                //Erreur
                IF fopen.xError AND fopen.eError <> 5104 THEN
                    Num_Etape:=10;
                END_IF
                //Le fichier n'existe pas
                IF fopen.eError = 5104 THEN
                    Num_Etape:=10;
                END_IF
    
    
            2: (*Taille du fichier fichier*)
                IF fgetsize.xDone THEN
                    Taille_Csv:=fgetsize.szSize;
                    Num_Etape:=5;
                END_IF
                //Erreur
                IF fgetsize.xError THEN
                    Num_Etape:=10;
                END_IF
    
    
            5:(*Lire 255 bytes*)
                IF fread.xDone THEN
                    Longueur_Chaine :=  LEN( sFileString );
    
                    IF Longueur_Chaine<>0 THEN
    
                        WHILE sFileString<>'' DO
    
    
                            //Chercher le retour à la ligne
                            Position_separateur := FIND( sFileString, CRLF );
    
                            //Récupérer le nom contenu avant le retour à la ligne et le stocker dans la table tampon
                            IF Position_separateur > 1 THEN
                                Liste_Op_Buffer[Ligne_Tab_Op] := LEFT( sFileString, Position_separateur-1);
                            ELSE
                                Liste_Op_Buffer[Ligne_Tab_Op] := sFileString;
                                sFileString:='';
                            END_IF
                                Longueur_Nom := LEN(Liste_Op_Buffer[Ligne_Tab_Op]);
    
                            //Enlever le nom extrait de la chaine de caractère
                            sFileString:=  DELETE( sFileString , Position_separateur + 1 , 1 );
                            Longueur_Chaine :=  LEN( sFileString );
    
                            Ligne_Tab_Op:=Ligne_Tab_Op+1;
    
                        END_WHILE
    
                    END_IF  
                    Num_Etape:=9;   
                END_IF
    
                IF fread.xError THEN
                    Num_Etape:=10;
                END_IF
    
    
            9:(*Lecture fichier réussit: transfert de la table tampon dans la table des noms d'opérateurs*)
                FOR Ligne_Tab_Op:=1 TO Maxi_Liste DO
                    GVL.LISTE_OPERATEURS[Ligne_Tab_Op] := Liste_Op_Buffer[Ligne_Tab_Op];
                END_FOR
                Num_Etape:=10;
    
            10:(* Fermer le fichier *)
                fclose( xExecute:=TRUE);
                IF fclose.xError THEN
                    ;           
                END_IF
                IF  fclose.xDone THEN
                    Num_Etape:=0;
                END_IF
    
    
    
        END_CASE
    
    ELSE
    
        En_cours:=FALSE;
        Num_Etape:=0;
    
    
    END_IF
    

    I don't understand how to use getpos and setpos: what is position ? Position of byte ? Position on memory ?
    Thanks for your help.

     

Log in to post a comment.