Anonymous - 2017-05-10

Originally created by: Primitive Source

Hi, I am having issues with txt file logging using raspberry pi and can't work out where I am going wrong. I am creating a STRING called FINSTR, it contains data from a temperature sensor, humidity sensor etc. My process is basically as follows a tank is emptied and then it is filled, each time the tank is filled I have a BOOL FiWr that becomes true for a few seconds the stops. FINSTR is written to the the txt file each time FiWr becomes true. The issue I have is that rather than adding the FINSTR on a new line/append at the end of the file it just overwrites the existing data. I want the txt file to end up like this after a few cycles;

23, 199, 267,
28, 194, 267,
28, 193, 267,

But I am getting

23, 199, 267,

and then it is over written and the file reads

28, 194, 267,

FINSTR is a CONCAT of (SEMFINSTR, '$R$L'). I am using an altered version of the CAA File Standard example as per below.

xFileStdInit:= RUN;

IF NOT xFileStdInit THEN
filop(xExecute:=FALSE);
filcl(xExecute:=FALSE);
filwr(xExecute:=FALSE);
filrd(xExecute:=FALSE);
xFileStdInit:=TRUE;
uiFileStdState:=0;
ELSE
CASE uiFileStdState OF
0:( create a new file )
filop.sFileName:=sFileName;
filop.eFileMode:=FILE.MODE.MRDWR;
filop.xExclusive:=TRUE;
filop( xExecute:=TRUE);
IF filop.xDone THEN
hFile:=filop.hFile;
uiFileStdState:=1;
END_IF
IF filop.xError THEN
( error handling)
;
END_IF
1:( close file - TestFile.txt )
filcl.hFile:=hFile;
filcl( xExecute:=TRUE);
IF filcl.xDone THEN
uiFileStdState:=2;
END_IF
IF filcl.xError THEN
( error handling)
;
END_IF

    2:(* end of example *)
        ;
END_CASE

END_IF

IF NOT FiWr THEN
filop(xExecute:=FALSE);
filcl(xExecute:=FALSE);
filwr(xExecute:=FALSE);
filrd(xExecute:=FALSE);
FiWr:= TRUE;
csvwrite:=0;
ELSE
CASE csvwrite OF
0:( create a new file )
filop.sFileName:=sFileName;
filop.eFileMode:=FILE.MODE.MAPPD;
filop.xExclusive:=TRUE;
filop( xExecute:=TRUE);
IF filop.xDone THEN
hFile:=filop.hFile;
csvwrite:=1;
END_IF
IF filop.xError THEN
( error handling)
;
END_IF

    1:(* 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);
        IF filwr.xDone THEN
            csvwrite:=2;
        END_IF
        IF filwr.xError THEN
            (* error handling*)
            ;
        END_IF

    2:(* close file  - TestFile.txt *)
        filcl.hFile:=hFile;
        filcl( xExecute:=TRUE);
        IF filcl.xDone THEN
            csvwrite:=4;
        END_IF
        IF filcl.xError THEN
            (* error handling*)
            ;
        END_IF

    3:(* end of example *)
        ;
END_CASE

END_IF

For some reason if I open the file in MAPPD mode initially the file isn't created? This is why I have 2 cases, one that creates the file when the programme is set to RUN and the second case writes the data to the file each time FiWr is true.

I hope you can help me as it is driving me crazy?!