2

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!

Felix
  • 1,066
  • 1
  • 5
  • 22
Aurel
  • 45
  • 3
  • 1
    You can implement your operation as a method rather than in the body of you function block, but that just shifts the question, as then you need to determine when this method is called. Everything I do is structured text, and I have no intention for my function blocks to be used in visual languages, so my function blocks talk to each other by having pointers/reference to each other and issuing method calls or directly accessing variables, basically just like I would to in any object-oriented language. How you intend to use your function block will influence its design. – Fred Jan 12 '22 at 12:32
  • Thank you for your answer. I also try to program in a OOP way. Do you know OOP example from Beckhoff? Do you program in this way? https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/100978955.html&id= – Aurel Jan 12 '22 at 15:39
  • OO modelling/programming is much too wide of a topic to answer here. OO has little to do with using a rising edge or not. If you want to develop OO skills, I would not seek guidance from Beckhoff. Though their tools support some constructs typical of OO languages, I would not expect them to be a fountain of wisdom when it comes to teaching how best to use it. – Fred Jan 12 '22 at 17:40
  • @Fred Structured Text, also known as ST, is part of IEC 61131-3, which is an international standard and not something Beckhoff has invented. ST is a statically typed object-oriented language designed to program complex machine software. – Filippo Boido Jan 12 '22 at 19:31
  • @Aurel If you are interested in a reference how to program machines in an object oriented way I suggest you read the PLCOPEN guidelines:https://plcopen.org/system/files/downloads/plcopen_oop_guidelines_version_1.0.pdf – Filippo Boido Jan 12 '22 at 19:32
  • @FilippoBoido I know what IEC-61131-3 is (more or less, not an expert, mind you) and that Beckhoff did not invent it. I use ST heavily, mostly in CODESYS. The OP was asking for examples from Beckhoff about OOP, and I was steering them away from Beckhoff as a source of good general material about OOP. – Fred Jan 12 '22 at 21:42

1 Answers1

1

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.

Felix
  • 1,066
  • 1
  • 5
  • 22