I am trying to write to file, where I want to write my data to several lines and not only one line, and my code is doing that now.
Also I want to delete all the data from file on rising edge of the tag. Any Ideas how to do that?
VAR
( Set xDemoStart to TRUE to start test )
xDemoStart: BOOL:=FALSE;
uiDemoStatus: UINT:=0;
filestring: STRING:='uuuHello this is a test';fileCR: STRING:='$r$n';endline: STRING:='$r';(*sFileNameisfilelocationrelativetotheCODESYSRuntimedirectoryontheHMIexamples:
'test.txt'Fileissavedin'Flash\QtHMI\RTS''..\test.txt'Fileissavedin'Flash\QtHMI''..\data\test.txt'Fileissavedin'Flash\QtHMI\data''..\..\..\USBMemory\test.txt'FileissavedinrootofexternalUSBmemory'..\..\..\Storage Card\test.txt'FileissavedinexternalSDCardmemory*)filemode: BYTE:=2;(*Modeinwhichthefileshouldbeopened,
FILE.MWRITE(0): Writeaccess; File will be overwritten/createdFILE.MREAD(1): Readaccess; File will be opened for reading onlyFILE.MRDWR(2): Read/writeaccess; File will be overwritten/createdFILE.MAPPD(3): Writeaccess; Data will be appended at the end of the file*)filename: CAA.FILENAME:='/home/pi/DataLogger/INTHISA_KUZMA11.csv';hFile: CAA.HANDLE;filesize1: CAA.SIZE:=0;filesize2: CAA.SIZE:=0;fileopen: FILE.Open;filewrite: FILE.Write;fileread: FILE.Read;fileclose: FILE.Close;filestringwrite: STRING(255);
END_VAR
CASE uiDemoStatus OF
0: ( Start demo )
IF xDemoStart THEN
uiDemoStatus:=1;
END_IF
fileopen(xExecute:=FALSE);
filewrite(xExecute:=FALSE);
fileread(xExecute:=FALSE);
fileclose(xExecute:=FALSE);
1: ( create a new file )
fileopen.sFileName:=filename;
fileopen.eFileMode:= filemode;
fileopen.xExclusive:=TRUE;
fileopen(xExecute:=TRUE);
IF fileopen.xDone THEN
hFile:=fileopen.hFile;
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF fileopen.xError THEN
( error handling)
;
END_IF
3: ( read file)
fileread.hFile:=hFile;
fileread.udiTimeOut:=100000; ( 100ms Timeout )
fileread.pBuffer:=ADR(FileString);
fileread.szBuffer:=255;
fileread(xExecute:=TRUE);
IF fileread.xDone THEN
filesize2:=fileread.szSize;
IF filesize2 = filesize1 THEN
uiDemoStatus:=uiDemoStatus+1;
ELSE
uiDemoStatus:=uiDemoStatus+1;
;
END_IF
END_IF
IF fileread.xError THEN
uiDemoStatus:=uiDemoStatus+2;
;
END_IF
4: ( close file)
fileclose.hFile:=hFile;
fileclose(xExecute:=TRUE);
IF fileclose.xDone THEN
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF fileclose.xError THEN
( error handling)
;
END_IF
5: ( end of example )
xDemoStart:=FALSE;
uiDemoStatus:=0;
END_CASE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You have two options that I know of.
1. Write an empty string to the file using the mwrite mode. This will overwrite and clear the files contents.
2. Delete the file and create a new one.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I am trying to write to file, where I want to write my data to several lines and not only one line, and my code is doing that now.
Also I want to delete all the data from file on rising edge of the tag. Any Ideas how to do that?
VAR
( Set xDemoStart to TRUE to start test )
xDemoStart: BOOL:=FALSE;
uiDemoStatus: UINT:=0;
END_VAR
CASE uiDemoStatus OF
0: ( Start demo )
IF xDemoStart THEN
uiDemoStatus:=1;
END_IF
fileopen(xExecute:=FALSE);
filewrite(xExecute:=FALSE);
fileread(xExecute:=FALSE);
fileclose(xExecute:=FALSE);
1: ( create a new file )
fileopen.sFileName:=filename;
fileopen.eFileMode:= filemode;
fileopen.xExclusive:=TRUE;
fileopen(xExecute:=TRUE);
IF fileopen.xDone THEN
hFile:=fileopen.hFile;
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF fileopen.xError THEN
( error handling)
;
END_IF
2: ( write text in the file )
3: ( read file)
fileread.hFile:=hFile;
fileread.udiTimeOut:=100000; ( 100ms Timeout )
fileread.pBuffer:=ADR(FileString);
fileread.szBuffer:=255;
fileread(xExecute:=TRUE);
IF fileread.xDone THEN
filesize2:=fileread.szSize;
IF filesize2 = filesize1 THEN
uiDemoStatus:=uiDemoStatus+1;
ELSE
uiDemoStatus:=uiDemoStatus+1;
;
END_IF
END_IF
IF fileread.xError THEN
uiDemoStatus:=uiDemoStatus+2;
;
END_IF
4: ( close file)
fileclose.hFile:=hFile;
fileclose(xExecute:=TRUE);
IF fileclose.xDone THEN
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF fileclose.xError THEN
( error handling)
;
END_IF
5: ( end of example )
xDemoStart:=FALSE;
uiDemoStatus:=0;
END_CASE
You have two options that I know of.
1. Write an empty string to the file using the mwrite mode. This will overwrite and clear the files contents.
2. Delete the file and create a new one.