How to WRITE to CSV file

kmckinney
2015-12-18
2015-12-24
  • kmckinney - 2015-12-18

    Hello, I'm trying to create & write to a CSV file.

    Using the CAA library example files I am able to create the directory and the file. The problem is writing to it.

    Using a CASE statement I'm using the below FOR loop, the below code only write the last [5,0].sRelayName and it doesn't tab to get the DINT value

    I've been at this for a while so I could really use some suggestions. The goal is to write string values to first column and DINT value to second column of CSV file. Then go to next line and re-do till you get to 5 (for loop max number)

    Lastly I get these characters after the carraige return and line feed: ÔaŸåPŠàp ãPWãªÔçÅçp‡âùÿÿêãÅç@ ã´@Já@ ã²@Já@ ã´@Já
    I'm guessing the reason I get them is because string has default character limit and these characters account for that limit. I'd like to get rid of these characters as I'm going to have to read these values back, if necessary, and I don't want to read these misc. characters

    Again I could surely use some help

    2:( write text in the file )

            filwr.hFile:=hFile;
        FOR i := 0 TO 5 DO
                DataString[i,0] := strRelay[i,0].sRelayName;
                DataString[i,0] := CONCAT(DataString[i,0],CHR_TO_STRING(C:= 09)); (*HT horizontal tab*)
    
                DataString := CONCAT(DataString,DINT_TO_STRING( strRelay[i,0].diFunctionLocation));
    
                DataString[i,0] := CONCAT(DataString[i,0],CHR_TO_STRING(C:= 13)); (* CR *)
                DataString[i,0] := CONCAT(DataString[i,0],CHR_TO_STRING(C:= 10)); (* LF *)
    
                filwr.pBuffer:=ADR(DataString[i,0]);
                szFileSize[i,0]:=SIZEOF(DataString[i,0]);
                filwr.szSize:=szFileSize[i,0];
                filwr.udiTimeOut:=100000;
    
    
        END_FOR
            filwr( xExecute:=TRUE);
    
    
        IF filwr.xDone THEN
            uiFileStdState:=4;
        END_IF
        IF filwr.xError THEN
        (* error handling*)
            ;
        END_IF
    
     
  • Bakkertje - 2015-12-21

    Hi,

    Try this for the carraige return and line feed:

    DataString[i,0] := CONCAT(DataString[i,0], '$R$L');
    

    Regards, Bakkertje

     
  • kmckinney - 2015-12-23

    Thank you for your response, I've tried your suggestion and sadly, it still didn't work. Code is only writing the first iteration of for loop and not all.

    This surely is exasperating.

    If you have any further suggestions please pass them along

     
  • Anonymous - 2015-12-24

    Originally created by: scott_cunningham

    It appears to me you are executing the FOR loop all five times and then calling your filewr(execute) once afterwards. Are you sure you are building a complete string? One of your lines just shows DataString without any array... And it's not clear to my why you are defining a two dimensional array?

    It looks to me your code should write the last entry of the FOR loop.

    TIP: be careful of FOR loops with PLC coding - machine cycle time can suffer if the loop has high resource demand. It may work fine on high end hardware, but lower end hardware could have problems. Also, CONCAT is "slow". It maybe faster to write each element of the string instead of concating it.

     

Log in to post a comment.