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

BUG: Why does I get garbage values when I use CONCAT and Array with CAA File?

Volvo742
2017-09-25
2019-07-04
  • Volvo742 - 2017-09-25

    Hi!

    I am trying to write "a;" to a csv file, but the results is the picture below.

    I think this is a bug. I have posted my code here. If you want to try this code. You can replace this code line

    sFileName := Standard.CONCAT(sFileName, '.csv'); // <-- Change fileName here if you want to try
    

    Whith your file location.

    If I use this code

    [code]sendText := STANDARD.CONCAT('a', ';'); // <-- Why does this not working?[/code]
    

    it will not working for me. But if I use

    sendText :='a;'.
    

    It works perfect. Why? It is the same if I set the code as

    sendText := SQL_Array_Text[i];
    

    I get garbarge values too. Why?

    PROGRAM SparaDataTillSQL
    VAR
            xFileStdInit:       BOOL:=FALSE;
            uiFileStdState:     UINT:=0;
            sFileName:          CAA.FILENAME;
            hFile:              CAA.HANDLE;
            szFileSize1:        CAA.SIZE := 0;
            szFileSize2:        CAA.SIZE := 0;
            filop:              FILE.Open;
            filwr:              FILE.Write;
            filrd:              FILE.Read;
            filcl:              FILE.Close;
            filsp:              FILE.SetPos;
          Date_and_time_format: DATE_AND_TIME;
          datumNamn: STRING;
          SQL_Array_Text: ARRAY[0..1024] OF STRING;
          // Räknare
          i: INT;
       // Texten som skall skickas
       sendText: STRING;
    END_VAR
    IF NOT xFileStdInit THEN
       filop(xExecute:=FALSE);
        filcl(xExecute:=FALSE);
        filwr(xExecute:=FALSE);
        //filrd(xExecute:=FALSE);
       xFileStdInit:=TRUE;
        uiFileStdState:=0;
       
       //Skapa filnamn
       datumNamn := DT_TO_STRING(Date_and_time_format);
       datumNamn := STANDARD.REPLACE(datumNamn,'_', 1, 17); // Här tar vi bort :
       datumNamn := STANDARD.REPLACE(datumNamn,'CC', 1, 20); // Samma här också.
       sFileName := Standard.CONCAT('\FlashDrive\Project\Project Files\Reports\FTP_Get\',datumNamn);
       sFileName := Standard.CONCAT(sFileName, '.csv'); // <-- Change fileName here if you want to try
       
       // Skapa en lång array med värden som skall loggas
       SQL_Array_Text[0] := GVL.Ordernummer;
       SQL_Array_Text[1] := GVL.VentilNamn;
       SQL_Array_Text[2] := GVL.SerieNummer;
       SQL_Array_Text[3] := GVL.VentilPort;
       SQL_Array_Text[4] := INT_TO_STRING(GVL.TestNummer);
       SQL_Array_Text[5] := GVL.FlodesInstallning;
       SQL_Array_Text[6] := GVL.TryckInstallning;
       SQL_Array_Text[7] := GVL.LastInstallning;
       SQL_Array_Text[8] := GVL.Operator;
       SQL_Array_Text[9] := INT_TO_STRING(GVL.MaxFlode);
       SQL_Array_Text[10] := GVL.VentilTyp;
       SQL_Array_Text[11] := BOOL_TO_STRING(GVL.Externt_Lackage);
       SQL_Array_Text[12] := BOOL_TO_STRING(GVL.Hysterestest);
       SQL_Array_Text[13] := LREAL_TO_STRING(GVL.Hysteresvarde);
       SQL_Array_Text[14] := BOOL_TO_STRING(GVL.Shuntslidkarvning);
       SQL_Array_Text[15] := REAL_TO_STRING(GVL.Shuntslidkarvningvarde);
       SQL_Array_Text[16] := BOOL_TO_STRING(GVL.Backventillackage);
       SQL_Array_Text[17] := REAL_TO_STRING(GVL.Backventillackagevarde);
       SQL_Array_Text[18] := BOOL_TO_STRING(GVL.Slidlackage);
       SQL_Array_Text[19] := REAL_TO_STRING(GVL.Slidlackagevarde);
       SQL_Array_Text[20] := BOOL_TO_STRING(GVL.Shuntlackage);
       SQL_Array_Text[21] := REAL_TO_STRING(GVL.ShuntlackageVarde);
       
       // 
       
       
       
    ELSE
        CASE uiFileStdState OF
            0:(* create a new file *)
                filop.sFileName:=sFileName;
                filop.eFileMode:=FILE.MODE.MRDWR;
                filop.xExclusive:=TRUE;
                filop( xExecute:=TRUE);
                IF filop.xDone THEN
                    hFile:=filop.hFile;
                    uiFileStdState:=1;
                END_IF
                IF filop.xError THEN
                    (* error handling*)
                    ;
                END_IF
            1:(* write text in the file *)
                filwr.hFile:=hFile;
                filwr.pBuffer:=ADR('#,');
                szFileSize1:=SIZEOF('#,');
                filwr.szSize:=szFileSize1;
                filwr.udiTimeOut:=100000;    (* 100ms Timeout *)
                filwr( xExecute:=TRUE);
                IF filwr.xDone THEN
                    uiFileStdState:=4;
                END_IF
                IF filwr.xError THEN
                    (* error handling*)
                    ;
                END_IF
            4:(* close file  - TestFile.txt *)
                filcl.hFile:=hFile;
                filcl( xExecute:=TRUE);
                IF filcl.xDone THEN
                    uiFileStdState:=5;
                filop(xExecute:=FALSE);
                filcl(xExecute:=FALSE);
                filwr(xExecute:=FALSE);
                END_IF
                IF filcl.xError THEN
                    (* error handling*)
                    ;
                END_IF
            5: (* Open - Append mode *)
             filop.sFileName:=sFileName;
                filop.eFileMode:=FILE.MODE.MAPPD;
                filop.xExclusive:=TRUE;
                filop( xExecute:=TRUE);
                IF filop.xDone THEN
                    hFile:=filop.hFile;
                    uiFileStdState:=6;
                END_IF
                IF filop.xError THEN
                    (* error handling*)
                    ;
                END_IF
          6:(* write text in the file *)
                filwr.hFile:=hFile;
             sendText := STANDARD.CONCAT('a', ';'); // <-- Why does this not working?
                filwr.pBuffer:=ADR(sendText);
                szFileSize1:=SIZEOF(sendText);
                filwr.szSize:=szFileSize1;
                filwr.udiTimeOut:=100000;    (* 100ms Timeout *)
                filwr( xExecute:=TRUE);
                IF filwr.xDone THEN
                    IF( i < 20) THEN
                   i := i + 1;
                ELSE
                   uiFileStdState:=8;
                END_IF
                filwr(xExecute:=FALSE);
                END_IF
                IF filwr.xError THEN
                    (* error handling*)
                    ;
                END_IF
             
          8:(* close file  - TestFile.txt *)
                filcl.hFile:=hFile;
                filcl( xExecute:=TRUE);
                IF filcl.xDone THEN
                filop(xExecute:=FALSE);
                filcl(xExecute:=FALSE);
                filwr(xExecute:=FALSE);
                    uiFileStdState:=9;
                //i := 0;
                END_IF
                IF filcl.xError THEN
                    (* error handling*)
                    ;
                END_IF
          9: (* Nothing *)
             ;
             
        END_CASE
    END_IF
    

    IMG: Capture.PNG

     

    Related

    Talk.ru: 1
    Talk.ru: 11
    Talk.ru: 2
    Talk.ru: 3
    Talk.ru: 5
    Talk.ru: 7
    Talk.ru: 8

  • Volvo742 - 2017-09-25

    Hello!

    I found the solution to this problem!

    Insted of

    szFileSize1:= SIZEOF(sendText);
    

    Use

    szFileSize1:= INT_TO_UINT(STANDARD.LEN(sendText));
    

    Moderator need to report this so the online help shows correct example.

     
  • mos89@yahoo.com - 2019-07-04

    Volvo742 hat geschrieben:
    Hello!
    I found the solution to this problem!
    Insted of

    szFileSize1:= SIZEOF(sendText);
    

    Use

    szFileSize1:= INT_TO_UINT(STANDARD.LEN(sendText));
    

    Moderator need to report this so the online help shows correct example.

    hi
    i add
    sFileStringtmp:=CONCAT(sFileTestString,';');
    sFileStringtmp:=CONCAT(sFileStringtmp,'$p');

    but in my file data are written on a row no line feed

    1.11; 0.0; 2.2;

    i want :
    1.11;
    0.0;
    2.2;

     
  • e.kislov - 2019-07-04

    Try $N

     

Log in to post a comment.