Codesys CSV file export

2016-12-07
2019-03-16
  • dongyaocode - 2016-12-07

    Hello guys,
    I'm using codesys to work on a project now. I want to export CSV file of the history data. So I can analyse it with matlab. I searched many things and find the program in the attachments. I have made some test, but no file exported.
    Does anyone have any idea what the problem is? Or maybe kindly teach me how can I get the CSV data file. Thank you!

    IMG: Unbenannt.PNG

     
  • wollvieh

    wollvieh - 2016-12-08

    look at this sample program...

    it writes 100 real values multiplied by 3600 rows (360000 values) in a csv file.
    maybe it gives you an inspiration ?

    I used he Codesys V3.5SP9Patch5 and the Codesys ControlWINV3 as runtime...

    wollvieh.

    IMG: csv.JPG

    test_csv.project [121.25 KiB]

     
  • rickj - 2016-12-08

    After writing to the file you need to wait until the write operation is actually performed before closing the file. The SysFile lib you are using should tell how to examine error, status, etc

     
  • dongyaocode - 2016-12-16

    Hello wollvieh,
    Thanks for your help. I have seen your program in another post. But maybe because the different patch, I can not run your program. I have no idea what is the problem. Seems like the program I found is right, but no file exported. I will try find out what is going on. Thanks again.

    wollvieh hat geschrieben:
    look at this sample program...
    it writes 100 real values multiplied by 3600 rows (360000 values) in a csv file.
    maybe it gives you an inspiration ?
    I used he Codesys V3.5SP9Patch5 and the Codesys ControlWINV3 as runtime...
    csv.JPG
    test_csv.project
    wollvieh.

     
  • dongyaocode - 2016-12-16

    Hello rjafrate,
    Yes, I think so but dont know how to deal with it. I waited till the time out and stopped running, still no file! I plan to try a new idea, OPC communication with matlab. Hope it will work.

    rjafrate hat geschrieben:
    After writing to the file you need to wait until the write operation is actually performed before closing the file. The SysFile lib you are using should tell how to examine error, status, etc

     
  • CanBohr - 2019-03-13

    Hello Wollvieh,

    Thanks a lot for your testfile. I am using it to write data into a csv-file.
    It's working 1000 times.

    But after 1000 write operations the systems (Codesys) exits and needs to be restarted.

    Have you had this issue also? Do you have an explanation for it?

    Cheers,
    CanBohr

    IMG: Codesys write CSV Data Fehler.jpg

     
  • dFx

    dFx - 2019-03-13

    In this project, there is :

    fopen(
       xExecute:= TRUE, 
    

    At the time you execute the file close, this may be a problem. Try to execute fopen only if your file hasn't been opened yet (xDone = false) and you want to write in it.

     
  • CanBohr - 2019-03-13

    I am doing the write process a bit different and I am using a FileOpenData flag to check if the file is already open.
    So this might not be the problem.
    Any other hints?

    IF NOT FileOpenData THEN
       
       
       FilePathAndName:=Concat(PLC_PRG.sFilePathData,PLC_PRG.sFileNameData);
       
       handleData := SysFileOpen(szFile:= FilePathAndName, AM_APPEND_PLUS , ADR( rte) );      
             
       (* Write Datastring to file *)
       IF SysFileWrite(hFile:= handleData, pbyBuffer:= ADR(resultdata_csv), ulSize:=pEnd - ADR(resultdata_csv) , pResult:=ADR( rte) )= 0 THEN
          
          iERRORData := 92; (* Could not write to file *)
          sStatusData := 'Could not write file.';
          SysFileClose(hFile:= handleData ) ;
       
          IF FileOpenData THEN
             
             (* Close file *)
             IF SysFileClose(hFile:= handleData ) =0 THEN
                   FileOpenData := FALSE;
                   sStatusData := 'File Saved.';
                   iERRORData := 91; (* Could not close file *)
                   sStatusData := 'Could not close file.';
                   xReadyForData:=FALSE;
             END_IF
          END_IF
       ELSE
          xWritingData:=FALSE;   
          xStartData := FALSE;
          xReadyForData:=TRUE;
             
       END_IF
    END_IF
    
     
  • dFx

    dFx - 2019-03-13

    Opening and closing a file is asynchronous in most systems, meaning that it will need several cpu cycles to process (on fast cycle tasks).
    Using CAA.File may help sequencing correctly (with xDone/xError).

    Your code is closing twice the same file (bad placed end_if ?).

    purposal of nicer sequencing that may fit asynchronous writing (not tested, need completion on error handling):

    IF WriteToFile THEN
       CASE FileWriteStep OF
          0: // Open the file
             FilePathAndName:=Concat(PLC_PRG.sFilePathData,PLC_PRG.sFileNameData);
             handleData := SysFileOpen(szFile:= FilePathAndName, SysFile.AM_APPEND_PLUS , ADR( SysFileOpenResult) );  
             IF handleData <> RTS_INVALID_HANDLE AND SysFileOpenResult = CmpErrors.Errors.ERR_OK THEN
                FileWriteStep := 1;
             ELSE
                FileWriteStep := 3; // Throw error
             END_IF
          1: // Write data
             SysFileWritenBytes := SysFileWrite(hFile:= handleData, pbyBuffer:= ADR(resultdata_csv), ulSize:=pEnd - ADR(resultdata_csv) , pResult:=ADR(SysFileWriteResult) );
             IF SysFileWritenBytes <> 0 AND SysFileWriteResult = CmpErrors.Errors.ERR_OK THEN
                FileWriteStep := 2;
             ELSE
                FileWriteStep := 4;   // Throw error
             END_IF
          2: // Close file and end sequence
             SysFileCloseResult := SysFileClose(hFile:= handleData );
             IF SysFileCloseResult = CmpErrors.Errors.ERR_OK THEN
                WriteToFile := FALSE;
             ELSE
                FileWriteStep := 5; // Throw error
             END_IF
          ELSE // Do some error management here
            WriteToFile := FALSE;
       END_CASE;
    ELSE
       FileWriteStep := 0;
    END_IF
    

    Edit: Slight syntax correction

     
  • CanBohr - 2019-03-13

    Thanks for your hint and effort.
    I will try it and let you know the result.
    Cheers,
    CanBohr

     
  • CanBohr - 2019-03-16

    I managed to get it to work perfectly based on your code.
    Thanks a lot for your help.
    Cheers,
    CanBohr

     

Log in to post a comment.