TwinCAT FB_FileOpen

karlek

Member
Join Date
Jul 2014
Location
Varazdin, Croatia
Posts
68
There's already been some discussion about writing to a file in TwinCAT, but I didn't see that someone encountered problem similar to mine.

My code here is written for testing purposes, task cycle time is 500 ms so every 6 seconds I write a string (consisting of date, time, voltage and current in one phase) into a file. Embedded PC is CX8090 and I write the file onto a C disk on my laptop.

Code:
VAR

    adresa            :T_AmsNetId:='169.254.113.214.1.1';
    otvoriDatoteku    :FB_FileOpen;
    zatvoriDatoteku   :FB_FileClose;
    fPuts             :FB_FilePuts;

    hFile             :UINT;

    bFileOpen         :BOOL;
    bFileOpenBusy     :BOOL;
    bFileOpenError    :BOOL;
    nFileOpenErrId    :UDINT;

    bFileCloseBusy    :BOOL;
    bFileCloseError   :BOOL;
    nFileCloseErrId   :UDINT;

    bFilePuts         :BOOL;
    bFilePutsBusy     :BOOL;
    bFilePutsError    :BOOL;
    nFilePutsErrId    :UDINT;

    BState            :UINT;

END_VAR

CASE BState OF
    0:
    otvoriDatoteku(bExecute:=FALSE);
    BState := BState + 1;

    1:
    otvoriDatoteku(
        sNetId:=adresa,
        sPathName:='c:\testfile6.txt',
        nMode:=FOPEN_MODEAPPEND,
        ePath:=PATH_GENERIC,
        bExecute:=TRUE,
        bBusy=>bFileOpenBusy,
        bError=>bFileOpenError,
        nErrId=>nFileOpenErrId);

    BState := BState + 1;

    2:
    otvoriDatoteku();
    IF (NOT otvoriDatoteku.bError AND NOT otvoriDatoteku.bBusy) THEN
        hFile:=otvoriDatoteku.hFile;
        fPuts(bExecute:=FALSE);
        BState := BState + 1;
    ELSIF otvoriDatoteku.bError THEN
        BState := 0;
    END_IF

    3:
    fPuts(
        sNetId:=adresa,
        hFile:=hFile,
        sLine:=stringDat,
        bExecute:=TRUE,
        bBusy=>bFilePutsBusy,
        bError=>bFilePutsError,
        nErrId=>nFilePutsErrId);
    BState := BState + 1;

    4:
    fPuts();
    IF(NOT fPuts.bError AND NOT fPuts.bBusy) THEN
        zatvoriDatoteku(bExecute:=FALSE);
        BState := BState + 1;
    END_IF

    5:
    zatvoriDatoteku(
        sNetId:=adresa,
        hFile:=hFile,
        bExecute:=TRUE);
    BState := 0;

END_CASE
Problem I am facing is that approximately every ~50 minutes (when my text file size gets somewhere around 24-25 kb) my program gets stuck at step 2 - opening of a file. Error HEX code is 0x70C, which means "File not found. Invalid file name or file path." (as stated in Beckhoff documentation - https://infosys.beckhoff.com/englis...ibsystem/html/tcplclibsys_fb_fileopen.htm&id=).

When this happens, I have to reboot my CX to be able to write again to a file. Even if I stop the PLC program, change the name of the file in which I want to log (so basically I try to log into a completely new file), download new program and run the program it doesn't help.

Has anybody got any clues what is bugging me here? If you need any more piece of information or if I forgot something, I'll be glad to give more details.
 
Last edited:
It seems that I post a little too fast, solved problem quickly after posting :)

The problem was in the last state (closing the file), I didn't wait for the FB_FileClose to finish its job before going to state 0 so there was an error while FB_FileOpen was trying to open the file (apparently this started happening when the file was bigger and not right at the start).

So, the rearranged code:

Code:
CASE BState OF

    0:
    otvoriDatoteku(bExecute:=FALSE);
    otvoriDatoteku(
        sNetId:=adresa,
        sPathName:='c:\testfile7.txt',
        nMode:=FOPEN_MODEAPPEND,
        ePath:=PATH_GENERIC,
        bExecute:=TRUE,
        bBusy=>bFileOpenBusy,
        bError=>bFileOpenError,
        nErrId=>nFileOpenErrId);

    BState := BState + 1;

    1:
    otvoriDatoteku(bExecute:=FALSE);
    IF (NOT otvoriDatoteku.bError AND NOT otvoriDatoteku.bBusy) THEN
        hFile:=otvoriDatoteku.hFile;
        BState := BState + 1;
    ELSIF otvoriDatoteku.bError THEN
        BState := BState - 1;
    END_IF

    2:
    fPuts(bExecute:=FALSE);
    fPuts(
        sNetId:=adresa,
        hFile:=hFile,
        sLine:=stringDat,
        bExecute:=TRUE,
        bBusy=>bFilePutsBusy,
        bError=>bFilePutsError,
        nErrId=>nFilePutsErrId);
    BState := BState + 1;

    3:
    fPuts(bExecute:=FALSE);
    IF (NOT fPuts.bError AND NOT fPuts.bBusy) THEN
        BState := BState + 1;
    ELSIF fPuts.bError THEN
        BState := 5;
    END_IF

    4:
    zatvoriDatoteku(bExecute:=FALSE);
    zatvoriDatoteku(
        sNetId:=adresa,
        hFile:=hFile,
        bExecute:=TRUE );
    BState := BState + 1;

    5:
    zatvoriDatoteku(bExecute:=FALSE);
    IF NOT zatvoriDatoteku.bBusy THEN
        BState := 0;
    END_IF

END_CASE
 
Last edited:

Similar Topics

Hi I want to create, open, write and close a .txt file. And it works fine. BUT I can't do it again. I have to reactive the hole system before I...
Replies
2
Views
4,007
Hi I'm doing a simple writing to .CSV file. I want to write the headers, but I have hard time indentifying when the file is empty. One idea was...
Replies
1
Views
1,976
Hi, I want to write a string into a file. This has to happen within a funcion (FUN, no FB) since I want to use it to log stuff to a file...
Replies
3
Views
6,561
Hi guys, I get an 'Identifier expected' error on this line fbFileOpen(' ','Hard Disk\My_Projects\Tilt6_1.txt',FOPEN_MODEREAD OR...
Replies
16
Views
4,659
Sorry if this has been asked before, and apologies if this seems like a trivial issue, but I am new to Beckhoff and have been banging my head...
Replies
1
Views
60
Back
Top Bottom