0

I am developing a code to get a text file in twincat. I am using the block functions:

  • FB_FileOpen

  • FB_FileGets

  • FB_FileClose

I build on the example on the website https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclibsystem/html/tcplclibsys_fileio_example.htm&id=5958930695166849062

When the program runs my FB_GetLabelFile runs to step 3 and then does not advance

I would like to know where I am making the mistake. I attach the code here below:

FUNCTION_BLOCK FB_GetLabelFile
VAR_INPUT
    bExecute        : BOOL ;
    hFile           : UINT ;          
    sNetId          : STRING ;  
    sFileName       : STRING    := 'src\version.txt';                     
    tTimeout        : TIME      := T#5S;
END_VAR
VAR_OUTPUT
    bBusy           : BOOL ;
    bError          : BOOL ;        (* TRUE => Error, FALSE => No error *)
    nErrId          : UDINT;        (* Error code *)
    sResult         : STRING ;
    tRisingEdge     : R_TRIG;
END_VAR
VAR
    fbFileOpen      : FB_FileOpen;
    fbFileClose     : FB_FileClose;
    fbFileGet       : FB_FileGets;

    hSrcFile        : UINT;
    nState          : INT ;
END_VAR

tRisingEdge(CLK:=bExecute);

CASE nState OF
    0:
    IF tRisingEdge.Q THEN
        bBusy   := TRUE;
        bError  := FALSE;
        nErrId  := 0;
        nState  := 1;
        sResult := '';
        hSrcFile:= 0;
    END_IF

    1: 
    fbFileOpen.bExecute     := FALSE;
    fbFileOpen.sNetId       := sNetID;
    fbFileOpen.sPathName    := sFileName;
    fbFileOpen.nMode        := FOPEN_MODEREAD OR FOPEN_MODETEXT;
    fbFileOpen.ePath        := PATH_GENERIC;
    fbFileOpen.tTimeout     := tTimeout;

    nState := nState + 1;

    2:
    fbFileOpen.bExecute := FALSE;
    IF NOT fbFileOpen.bBusy THEN
        IF fbFileOpen.bError THEN
            nErrId := fbFileOpen.nErrId;
            bError := TRUE;
            nState := 10;
        ELSE
            hSrcFile := fbFileOpen.hFile;
            nState := nState + 1;
        END_IF
    END_IF

    3:
    sResult             := '';
    fbFileGet.bExecute  := FALSE;
    fbFileGet.sNetId    := sNetID;
    fbFileGet.hFile     := hSrcFile;
    fbFileGet.bExecute  := TRUE;
    fbFileGet.tTimeout  := tTimeOut;

    nState := nState + 1;

    4:
    fbFileGet.bExecute  := FALSE;
    IF NOT fbFileGet.bBusy THEN
        IF fbFileGet.bError THEN
            nErrId := fbFileGet.nErrId;
            bError := TRUE;
            nState := 10;
        ELSE
            sResult := fbFileGet.sLine;
            nState := nState + 1;
        END_IF
    END_IF

    5:
    fbFileClose.bExecute    := FALSE;
    fbFileClose.sNetId      := sNetID;
    fbFileClose.hFile       := hSrcFile;
    fbFileClose.bExecute    := TRUE;
    fbFileClose.tTimeout    := tTimeOut;

    nState := nState + 1;

    6: 
    fbFileClose.bExecute := FALSE;
    IF NOT fbFileClose.bBusy THEN
        IF fbFileClose.bError THEN
            nErrId  := fbFileClose.nErrId;
            bError  := TRUE;
        END_IF
        nState   := 10;
        hSrcFile := 0;
    END_IF

    10:
    IF hSrcFile <> 0 THEN
        nState := 5;
    ELSE
        nState := 0;
        bBusy := FALSE;
    END_IF
END_CASE

Thank you very much! I would greatly appreciate your help

hachete14
  • 3
  • 1

3 Answers3

1

You're forgetting to call the function blocks. As it is right now, you're only supplying the input-parameters to the different Beckhoff function blocks, but not actually executing them (just as you execute tRisingEdge, which is also a function block). So do (in the correct places, or all at the end):

fbFileOpen();
fbFileGet();
fbFileClose();
Jakob
  • 1,288
  • 7
  • 13
0

@Jakob Thanks for answering me. I try to do that but the behavior is the same, locks in state 3.Note that when I send it to call in the main code in this way it advances to state 3:

fbGetLabelFile.bExecute: = TRUE;
fbGetLabelFile ();

On the other hand, if I send it to call this way, it advances to state 2

fbGetLabelFile ();
fbGetLabelFile.bExecute: = TRUE;
hachete14
  • 3
  • 1
0

You are setting bExecute to false then true on step 3. The fbFileGet only sees bExecute as true, as it is executed later in the code. It needs to see a false->true transition to trigger. Try putting a breakpoint in and stepping through to see what actually happens.

weirdgyn
  • 886
  • 16
  • 47