How to trigger a Function_Block

Viewed 188

Hello to all TwinCAT developers,

I am currently developing function blocks with TwinCAT. I'm trying to find a "standard" way to interact with the outside of the block. The Beckhoff examples always have a bExec signal to start a state machine on the rising edge.

fbRisingEdge(CLK := bExec);
IF fbRisingEdge.Q THEN
 nStep := 1;
END_IF

CASE nStep OF
1:
nStep := nStep + 1;

2:
nStep := nStep + 1;

END_CASE

I find that this principle is heavy to use and requires more code to create the rising edge:

fbFileOpen(sPathName := sPathName, bExecute := FALSE);
fbFileOpen(sPathName := sPathName, bExecute := TRUE);

Would anyone use another alternative to start a state-machine inside a FB?

Thank you, Happy new year!

1 Answers

I use a method defined inside my function block to trigger an action. I Also (sometimes) return a bool from the method which indicates weather the action can be performend or not, depending on the current state.

METHOD M_Open : BOOL
VAR_IN
    sPath : STRING;
END_VAR
VAR_OUTPUT
    bExecutionAllowed : BOOL;
END_VAR
bExecutionAllowed := _ ;    // Calculate depending on the current state

    sPathName := sPath;

IF bExecutionAllowed THEN
    nStep := 1;
END_IF

Then call the method somewhere once

fbFileHandler.M_Open("Path/To/File");

And the fucntion block cyclically

fbFileHandler();

The function block can than handles multiple different actions which may be triggered each by their own method.

Related