I am trying to create a CSV log file. I have a string within my programme that contains a few variables, some sensor data and internal calculations.
Here's my predicament, as the data in my string changes, I need to write the data every time FiWr becomes true. This is happening with the code below, however each time FiWr becomes true and writes the string to the CSV file it overwrites the existing data, so no matter how many times FiWr becomes true I only ever have one line in my CSV file. I would like to see a new line written to the CSV file each time FiWr becomes true.
STR1:=CONCAT(TEMPSTRINGCO,FACSTRINGCO);STR2:=CONCAT(COUNTSTRINGCO,'$R$N');FINSTR:=CONCAT(STR1,STR2);xFileStdInit:=RUN;IFNOTxFileStdInitTHEN  filop(xExecute:=FALSE);  filcl(xExecute:=FALSE);  filwr(xExecute:=FALSE);  filrd(xExecute:=FALSE);  xFileStdInit:=TRUE;  uiFileStdState:=0;ELSE  CASEuiFileStdStateOF    0:(* create a new file *)      filop.sFileName:=sFileName;      filop.eFileMode:=FILE.MODE.MRDWR;      filop.xExclusive:=TRUE;      filop(xExecute:=TRUE);      IFfilop.xDoneTHEN        hFile:=filop.hFile;        uiFileStdState:=1;      END_IF      IFfilop.xErrorTHEN        (* error handling*)        ;      END_IF          1:(* close file - TestFile.txt *)      filcl.hFile:=hFile;      filcl(xExecute:=TRUE);      IFfilcl.xDoneTHEN        uiFileStdState:=2;      END_IF      IFfilcl.xErrorTHEN        (* error handling*)        ;      END_IF    2:(* end of example *)      ;  END_CASEEND_IFIFNOT FiWrTHEN  filop(xExecute:=FALSE);  filcl(xExecute:=FALSE);  filwr(xExecute:=FALSE);  filrd(xExecute:=FALSE);  filgp(xExecute:=FALSE);  filsp(xExecute:=FALSE);  FiWr:=TRUE;  csvwrite:=0;ELSE  CASEcsvwriteOF    0:(* create a new file *)      filop.sFileName:=sFileName;      filop.eFileMode:=FILE.MODE.MAPPD;      filop.xExclusive:=TRUE;      filop(xExecute:=TRUE);      IFfilop.xDoneTHEN        hFile:=filop.hFile;        csvwrite:=1;      END_IF      IFfilop.xErrorTHEN        (* error handling*)        ;      END_IF              1:(* get actual internal positon file pointer *)         filgp.hFile:=hFile;         filgp(xExecute:=TRUE);        filgp.xBusy;        filgp.xError;          filgp.eError;         IFfilgp.xDone THEN         WritePoint:=filgp.udiPos;         csvwrite:=2;        END_IF        IFfilgp.xErrorTHEN        (* error handling*);        END_IF                  2:(* set actual internal positon file pointer *)         filsp.hFile:=hFile;        filsp(udiPos:=WritePoint);         filsp(xExecute:=TRUE);        filsp.xBusy;        filsp.xError;          filsp.eError;                   IFfilsp.xDone THEN         csvwrite:=3;        END_IF        IFfilgp.xErrorTHEN        (* error handling*);        END_IF    3:(* write text in the file *)      filwr.hFile:=hFile;      filwr.pBuffer:=ADR(FINSTR);      szFileSize1:=SIZEOF(FINSTR);      filwr.szSize:=szFileSize1;      filwr.udiTimeOut:=100000;  (* 100ms Timeout *)      filwr(xExecute:=TRUE);      IFfilwr.xDoneTHEN        csvwrite:=4;      END_IF      IFfilwr.xErrorTHEN        (* error handling*)        ;      END_IF    4:(* close file - TestFile.txt *)      filcl.hFile:=hFile;      filcl(xExecute:=TRUE);      IFfilcl.xDoneTHEN        csvwrite:=5;      END_IF      IFfilcl.xErrorTHEN        (* error handling*)        ;      END_IF    5:(* end of example *)      ;  END_CASEEND_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have used the following code in a function block,
Put these variables in you variable editor:
//ThisFunctionblockrequiresSysFile=SysFile,3.5.6.0intheLibraryManagerFUNCTION_BLOCKTEXT_TO_FILEVAR_INPUT  xFilePath:STRING;  xText_Write:STRING(2000);  xAppend_OverWrite:BOOL;  xAppend_Pos:UDINT;  xWrite:BOOL;  xRead:BOOL;  xLine_No_To_Read:UDINT;  END_VARVAR_OUTPUT  xText_Read:STRING(2000);  xDone:BOOL;END_VARVAR  //WriteVariables  xWrite_Trig:R_TRIG;  xStart_Write:BOOL;  xNew_Line:STRING(2000);  //ReadVariables  xRead_Trig:R_TRIG;  xStart_Read:BOOL;  xLine_Read:UDINT;    hFile      :RTS_IEC_HANDLE:=RTS_INVALID_HANDLE;  udiBytesRead   :UDINT;  udiBytesCopied  :UDINT;  udiWrite    :UDINT;  udiFileSize   :UDINT;  udiPos      :UDINT;//(* Error Codes *)  udiPosError      :RTS_IEC_RESULT;  udiCopyError    :RTS_IEC_RESULT;  udiDeleteError1    :RTS_IEC_RESULT;  udiDeleteError2    :RTS_IEC_RESULT;  udiWriteError1    :RTS_IEC_RESULT;  udiWriteError2    :RTS_IEC_RESULT;  udiWriteError3    :RTS_IEC_RESULT;  udiOpenError1    :RTS_IEC_RESULT;  udiOpenError2    :RTS_IEC_RESULT;  udiOpenError3    :RTS_IEC_RESULT;  udiOpenError4    :RTS_IEC_RESULT;  udiReadError1  :RTS_IEC_RESULT;  udiReadError2  :RTS_IEC_RESULT;  udiReadError3  :RTS_IEC_RESULT;  udiCloseError1  :RTS_IEC_RESULT;  udiCloseError2  :RTS_IEC_RESULT;  udiCloseError3  :RTS_IEC_RESULT;  udiCloseError4  :RTS_IEC_RESULT;  udiSizeError:RTS_IEC_RESULT;  udiDirDeleteError:UDINT;  udiCreateError:UDINT;END_VAR
Then paste this code into the editor.
xWrite_Trig();//xWrite_Trig.PT:=T#500MS;xWrite_Trig.CLK:=xWrite;xStart_Write:=xWrite_Trig.Q;xRead_Trig();//xRead_Trig.PT:=T#500MS;xRead_Trig.CLK:=xRead;xStart_Read:=xRead_Trig.Q;IFxStart_WriteTHENÂ Â xDone:=FALSE;Â Â IFxAppend_OverWriteTHENÂ Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_APPEND,pResult:=ADR(udiOpenError1));Â Â END_IFÂ Â IFxAppend_OverWrite=FALSETHENÂ Â Â Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_WRITE_PLUS,pResult:=ADR(udiOpenError1));Â Â END_IFÂ Â udiPosError:=SysFileGetPos(hFile:=hFile,pulPos:=ADR(udiPos));Â Â IFhFile<>RTS_INVALID_HANDLETHENÂ Â Â Â xNew_Line:=concat('$n',xText_Write);Â Â Â Â IFudiPos=16#0THENÂ Â Â Â Â Â udiWrite:=SysFileWrite(hFile:=hFile,pbyBuffer:=ADR(xText_Write),ulSize:=INT_TO_UDINT(LEN(xText_Write)),pResult:=ADR(udiWriteError1));Â Â Â Â ELSEÂ Â Â Â Â Â udiWrite:=SysFileWrite(hFile:=hFile,pbyBuffer:=ADR(xNew_Line),ulSize:=INT_TO_UDINT(LEN(xNew_Line)),pResult:=ADR(udiWriteError1));Â Â Â Â END_IFÂ Â Â Â udiCloseError1:=SysFileClose(hFile:=hFile);Â Â END_IFÂ Â xDone:=TRUE;END_IFIFxStart_ReadTHENÂ Â xDone:=FALSE;Â Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_READ,pResult:=ADR(udiOpenError1));Â Â IFhFile<>RTS_INVALID_HANDLETHENÂ Â Â Â udiBytesRead:=SysFileRead(hFile:=hFile,pbyBuffer:=ADR(xText_Read),ulSize:=SIZEOF(xText_Read),pResult:=ADR(udiReadError1));Â Â Â Â xText_Read[udiBytesRead]:=0;Â Â END_IFÂ Â //xDone:=TRUE;Â Â END_IF
Be sure that you feed the TEXT_TO_FILE function block with a line that is already in a csv format for example,
variable1,variable2,variable3,variable4
You will see in the code that there is a xNew_Line variable that has the '$n' token to add the new line.
Hope this helps,
Best Regards
Johan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've seen many similar examples of code like yours. For me, I want to have a program which can write and read a csv file in a certain directory. The problem is, that I couldn't figure out until now, where the file is saved/ or if I gave a certain file path, it wouldn't appear... It would mean a great deal to me, if you could enlighten me a bit!
Kind regards,
SpeedyG
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
johan76swart hat geschrieben:
Hi Primative Source,
I have used the following code in a function block,
Put these variables in you variable editor:
//ThisFunctionblockrequiresSysFile=SysFile,3.5.6.0intheLibraryManagerFUNCTION_BLOCKTEXT_TO_FILEVAR_INPUT  xFilePath:STRING;  xText_Write:STRING(2000);  xAppend_OverWrite:BOOL;  xAppend_Pos:UDINT;  xWrite:BOOL;  xRead:BOOL;  xLine_No_To_Read:UDINT;  END_VARVAR_OUTPUT  xText_Read:STRING(2000);  xDone:BOOL;END_VARVAR  //WriteVariables  xWrite_Trig:R_TRIG;  xStart_Write:BOOL;  xNew_Line:STRING(2000);  //ReadVariables  xRead_Trig:R_TRIG;  xStart_Read:BOOL;  xLine_Read:UDINT;    hFile      :RTS_IEC_HANDLE:=RTS_INVALID_HANDLE;  udiBytesRead   :UDINT;  udiBytesCopied  :UDINT;  udiWrite    :UDINT;  udiFileSize   :UDINT;  udiPos      :UDINT;//(* Error Codes *)  udiPosError      :RTS_IEC_RESULT;  udiCopyError    :RTS_IEC_RESULT;  udiDeleteError1    :RTS_IEC_RESULT;  udiDeleteError2    :RTS_IEC_RESULT;  udiWriteError1    :RTS_IEC_RESULT;  udiWriteError2    :RTS_IEC_RESULT;  udiWriteError3    :RTS_IEC_RESULT;  udiOpenError1    :RTS_IEC_RESULT;  udiOpenError2    :RTS_IEC_RESULT;  udiOpenError3    :RTS_IEC_RESULT;  udiOpenError4    :RTS_IEC_RESULT;  udiReadError1  :RTS_IEC_RESULT;  udiReadError2  :RTS_IEC_RESULT;  udiReadError3  :RTS_IEC_RESULT;  udiCloseError1  :RTS_IEC_RESULT;  udiCloseError2  :RTS_IEC_RESULT;  udiCloseError3  :RTS_IEC_RESULT;  udiCloseError4  :RTS_IEC_RESULT;  udiSizeError:RTS_IEC_RESULT;  udiDirDeleteError:UDINT;  udiCreateError:UDINT;END_VAR
Then paste this code into the editor.
xWrite_Trig();//xWrite_Trig.PT:=T#500MS;xWrite_Trig.CLK:=xWrite;xStart_Write:=xWrite_Trig.Q;xRead_Trig();//xRead_Trig.PT:=T#500MS;xRead_Trig.CLK:=xRead;xStart_Read:=xRead_Trig.Q;IFxStart_WriteTHENÂ Â xDone:=FALSE;Â Â IFxAppend_OverWriteTHENÂ Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_APPEND,pResult:=ADR(udiOpenError1));Â Â END_IFÂ Â IFxAppend_OverWrite=FALSETHENÂ Â Â Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_WRITE_PLUS,pResult:=ADR(udiOpenError1));Â Â END_IFÂ Â udiPosError:=SysFileGetPos(hFile:=hFile,pulPos:=ADR(udiPos));Â Â IFhFile<>RTS_INVALID_HANDLETHENÂ Â Â Â xNew_Line:=concat('$n',xText_Write);Â Â Â Â IFudiPos=16#0THENÂ Â Â Â Â Â udiWrite:=SysFileWrite(hFile:=hFile,pbyBuffer:=ADR(xText_Write),ulSize:=INT_TO_UDINT(LEN(xText_Write)),pResult:=ADR(udiWriteError1));Â Â Â Â ELSEÂ Â Â Â Â Â udiWrite:=SysFileWrite(hFile:=hFile,pbyBuffer:=ADR(xNew_Line),ulSize:=INT_TO_UDINT(LEN(xNew_Line)),pResult:=ADR(udiWriteError1));Â Â Â Â END_IFÂ Â Â Â udiCloseError1:=SysFileClose(hFile:=hFile);Â Â END_IFÂ Â xDone:=TRUE;END_IFIFxStart_ReadTHENÂ Â xDone:=FALSE;Â Â hFile:=SysFileOpen(szFile:=xFilePath,am:=ACCESS_MODE.AM_READ,pResult:=ADR(udiOpenError1));Â Â IFhFile<>RTS_INVALID_HANDLETHENÂ Â Â Â udiBytesRead:=SysFileRead(hFile:=hFile,pbyBuffer:=ADR(xText_Read),ulSize:=SIZEOF(xText_Read),pResult:=ADR(udiReadError1));Â Â Â Â xText_Read[udiBytesRead]:=0;Â Â END_IFÂ Â //xDone:=TRUE;Â Â END_IF
Be sure that you feed the TEXT_TO_FILE function block with a line that is already in a csv format for example,
variable1,variable2,variable3,variable4
You will see in the code that there is a xNew_Line variable that has the '$n' token to add the new line.
Hope this helps,
Best Regards
Johan
Hi johan76swart
do you have an exemple of the use of your function block please ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Originally created by: Primitive Source
Hello,
I am trying to create a CSV log file. I have a string within my programme that contains a few variables, some sensor data and internal calculations.
Here's my predicament, as the data in my string changes, I need to write the data every time FiWr becomes true. This is happening with the code below, however each time FiWr becomes true and writes the string to the CSV file it overwrites the existing data, so no matter how many times FiWr becomes true I only ever have one line in my CSV file. I would like to see a new line written to the CSV file each time FiWr becomes true.
Hi Primative Source,
I have used the following code in a function block,
Put these variables in you variable editor:
Then paste this code into the editor.
Be sure that you feed the TEXT_TO_FILE function block with a line that is already in a csv format for example,
variable1,variable2,variable3,variable4
You will see in the code that there is a xNew_Line variable that has the '$n' token to add the new line.
Hope this helps,
Best Regards
Johan
Hi johan76swart, hi Primitive Source,
I've seen many similar examples of code like yours. For me, I want to have a program which can write and read a csv file in a certain directory. The problem is, that I couldn't figure out until now, where the file is saved/ or if I gave a certain file path, it wouldn't appear... It would mean a great deal to me, if you could enlighten me a bit!
Kind regards,
SpeedyG
Then paste this code into the editor.
Be sure that you feed the TEXT_TO_FILE function block with a line that is already in a csv format for example,
variable1,variable2,variable3,variable4
You will see in the code that there is a xNew_Line variable that has the '$n' token to add the new line.
Hope this helps,
Best Regards
Johan
Hi johan76swart
do you have an exemple of the use of your function block please ?