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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
IFNOTFileOpenDataTHEN
 Â
 Â
  FilePathAndName:=Concat(PLC_PRG.sFilePathData,PLC_PRG.sFileNameData);
 Â
  handleData :=SysFileOpen(szFile:=FilePathAndName, AM_APPEND_PLUS , ADR(rte));   Â
     Â
  (*WriteDatastringtofile*)
  IFSysFileWrite(hFile:=handleData, pbyBuffer:=ADR(resultdata_csv), ulSize:=pEnd-ADR(resultdata_csv) , pResult:=ADR(rte))=0THEN
   Â
    iERRORData :=92; (* Could not write to file *)
    sStatusData :='Could not write file.';
    SysFileClose(hFile:=handleData);
 Â
    IFFileOpenDataTHEN
     Â
      (*Closefile*)
      IFSysFileClose(hFile:=handleData)=0THEN
          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_IFEND_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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):
IFWriteToFileTHEN
  CASEFileWriteStepOF
   0: //Openthefile
     FilePathAndName:=Concat(PLC_PRG.sFilePathData,PLC_PRG.sFileNameData);
     handleData :=SysFileOpen(szFile:=FilePathAndName, SysFile.AM_APPEND_PLUS , ADR(SysFileOpenResult));Â
     IFhandleData<>RTS_INVALID_HANDLEANDSysFileOpenResult=CmpErrors.Errors.ERR_OKTHEN
      FileWriteStep :=1;
     ELSE
      FileWriteStep :=3; // Throw error
     END_IF
   1: //Writedata
     SysFileWritenBytes :=SysFileWrite(hFile:=handleData, pbyBuffer:=ADR(resultdata_csv), ulSize:=pEnd-ADR(resultdata_csv) , pResult:=ADR(SysFileWriteResult));
     IFSysFileWritenBytes<>0ANDSysFileWriteResult=CmpErrors.Errors.ERR_OKTHEN
      FileWriteStep :=2;
     ELSE
      FileWriteStep :=4;  // Throw error
     END_IF
   2: //Closefileandendsequence
     SysFileCloseResult :=SysFileClose(hFile:=handleData);
     IFSysFileCloseResult=CmpErrors.Errors.ERR_OKTHEN
      WriteToFile :=FALSE;
     ELSE
      FileWriteStep :=5; // Throw error
     END_IF
   ELSE//Dosomeerrormanagementhere
    WriteToFile :=FALSE;
  END_CASE;ELSE
  FileWriteStep :=0;END_IF
Edit: Slight syntax correction
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
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.
test_csv.project [121.25 KiB]
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
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.
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.
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
In this project, there is :
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.
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?
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):
Edit: Slight syntax correction
Thanks for your hint and effort.
I will try it and let you know the result.
Cheers,
CanBohr
I managed to get it to work perfectly based on your code.
Thanks a lot for your help.
Cheers,
CanBohr