Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Raspberry PI-FILE WRITE

burger
2017-01-15
2017-01-16
  • burger - 2017-01-15

    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;

    filestring:     STRING:='uuuHello this is a test';
    fileCR:         STRING:='$r$n';
    endline:        STRING:='$r';
    (*  
    sFileName is file location relative to the CODESYS Runtime directory on the HMI
    examples:
        'test.txt'                          File is saved in 'Flash\QtHMI\RTS' 
        '..\test.txt'                       File is saved in 'Flash\QtHMI' 
        '..\data\test.txt'                  File is saved in 'Flash\QtHMI\data' 
        '..\..\..\USBMemory\test.txt'       File is saved in root of external USB memory
        '..\..\..\Storage Card\test.txt'    File is saved in external SD Card memory
    *)
    
    filemode:       BYTE:=2;
    (*
    Mode in which the file should be opened,
    FILE.MWRITE (0): Write access; File will be overwritten/created
    FILE.MREAD (1): Read access; File will be opened for reading only
    FILE.MRDWR (2): Read/write access; File will be overwritten/created
    FILE.MAPPD (3): Write access; 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

    2: ( write text in the file )

    filestringwrite:=CONCAT(filestring,fileCR);
    filewrite.hFile:=hFile;
    filewrite.pBuffer:=ADR(filestringwrite);
    filesize1:=SIZEOF(filestringwrite);
    filewrite.szSize:=filesize1;
    filewrite.udiTimeOut:=100000;                     (* 100ms Timeout *)
    filewrite(xExecute:=TRUE);
    IF filewrite.xDone THEN
        uiDemoStatus:=uiDemoStatus+2;
    END_IF
    IF filewrite.xError THEN
        uiDemoStatus:=0;
        ;
    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

     
  • Lo5tNet - 2017-01-16

    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.

     

Log in to post a comment.