CoDeSys ST Function Block Call

bopol

Member
Join Date
Feb 2008
Location
Singapore
Posts
46
Hi guys! need your help for proper syntax in calling a function block using structured text since i am a total noob in ST! this is kinda basic for those using ST but iv been trying to figure how to do this for 2 days and i just can't seem to get it so im seeking enlightenment again from the gurus =D

so here's what i want to do:

1. I have a function block, ill call it "Block" with "Start" and "Stop" Inputs and 1 output "Out"

2. i want to call the function block 10 times using the FOR instruction (so i have 10 different "start" and "stop" inputs, and 10 "out" outputs... i can have them named as start1, start2...start10, etc.)
*i use an integer variable "i" to correspond to the block # called

3. My expected output is to have 10 different outputs corresponding to their inputs by using just this one call function.

* I can do this in ladder by calling the function block and assigning the variables individually but it would be very tedious to do it if i have 100 blocks!

Here's what i did so far:

declare:
i as integer

Block1 as Block
Block2 as Block
.
.
.
Block10 as Block

---
Start1 as In_Var for Block
Start2 as InVar for Block
.
.
.
Start10 as InVar for Block

Stop1 as In_Var for Block
Stop2 as InVar for Block
.
.
.
Stop10 as InVar for Block

Out1 as Out_Var for Block
Out2 as Out_Var for Block
.
.
.
Out10 as Out_Var for Block
---

FOR i:=1 TO 10 BY 1 DO
Block(
in_start:= Start,
in_stop:= Stop);
out:=Block.out
END_FOR

*** im still stuck with Ladder programming so its kinda hard for me to figure out how to do this properly... im still confused whether "i" should be integer or array or if the "block" and inputs and outputs should be array! as i said, total noob to ST programming! but i can see the benefits of learning it so i have to try =) hope to be enlightened on this =))
 
Declare fb's as array.

Code:
VAR
   MyFBs : Array[1..10] OF MyFB;
END_VAR

ps. use code tags for code.

i see... thanks for the info! will try later today! hopefully i can get the program to function already! (y)

:site:

finally got it to work! thanks a lot TurpoUrpo!

here's my code:

FOR i:=1 TO 10 BY 1 DO
Blocked(
in_start:= Start,
in_stop:= Stop);
out:=Blocked.out;
END_FOR

IF i>10 THEN i:=0;
ELSE i:=i;
END_IF
Start[1]:=pb_start_01; Stop[1]:=pb_stop_01;
Start[2]:=pb_start_02; Stop[2]:=pb_stop_02;
Start[3]:=pb_start_03; Stop[3]:=pb_stop_03;
Start[4]:=pb_start_04; Stop[4]:=pb_stop_04;
Start[5]:=pb_start_05; Stop[5]:=pb_stop_05;
Start[6]:=pb_start_06; Stop[6]:=pb_stop_06;
Start[7]:=pb_start_07; Stop[7]:=pb_stop_07;
Start[8]:=pb_start_08; Stop[8]:=pb_stop_08;
Start[9]:=pb_start_09; Stop[9]:=pb_stop_09;
Start[10]:=pb_start_10; Stop[10]:=pb_stop_10;

Variables:

PROGRAM PLC_PRG
VAR
i: INT;
Start: ARRAY [1..10] OF BOOL;
Stop: ARRAY [1..10] OF BOOL;
out: ARRAY [1..10] OF BOOL;
blocked: ARRAY [1..10] OF Block;

pb_start_01: BOOL;
pb_start_02: BOOL;
pb_start_03: BOOL;
pb_start_04: BOOL;
pb_start_05: BOOL;
pb_start_06: BOOL;
pb_start_07: BOOL;
pb_start_08: BOOL;
pb_start_09: BOOL;
pb_start_10: BOOL;

pb_stop_01: BOOL;
pb_stop_02: BOOL;
pb_stop_03: BOOL;
pb_stop_04: BOOL;
pb_stop_05: BOOL;
pb_stop_06: BOOL;
pb_stop_07: BOOL;
pb_stop_08: BOOL;
pb_stop_09: BOOL;
pb_stop_10: BOOL;

END_VAR

please feel free to comment if there are better/more efficient ways to do this =) thanks again!
 
Last edited:
you have a name of a variable (blocked) and the function the same name. this is not allowed.
where is definition of the block (probably a type)
why this IF question i: is a loopvariable and you are not in the loop.
so no need for this.
you will need to define this outblock same as inputs so
coilout_01:= out[1]

ther is an easier way for this
look at www.oscat.de
 
you have a name of a variable (blocked) and the function the same name. this is not allowed.
where is definition of the block (probably a type)
why this IF question i: is a loopvariable and you are not in the loop.
so no need for this.
you will need to define this outblock same as inputs so
coilout_01:= out[1]

ther is an easier way for this
look at www.oscat.de

thanks for the tips shooter. i did try to refine this one and took out the "if" statement. i was just making sure it would not end in an endless loop (like if i used "if/else" statement). the function block name is blocked of type block so its a little confusing but is allowed. the block i posted was just for testing purposes so i can understand the structure/syntax of ST =). and thanks for the link to the library also! =D
 

Similar Topics

As I'm just in the flow of typing along (have to get a new backspace key soon)... I tried to write a function block in CoDeSys that what somewhat...
Replies
0
Views
5,060
Hi. I'm writing an CODESYS program with ST. In diffrent parts of my main program i need my program to pause for a while and continue running...
Replies
3
Views
10,180
"Function block call requires a function instance" when calling action in Codesys Using Codesys 2.3, I'm getting the following error: "A...
Replies
9
Views
9,854
I want to extract tag/variable values from a Mitsubishi PLC using MODBUS RTU protocol through the serial port. I am using Codesys for programming...
Replies
1
Views
2,511
To Codesys and TwinCAT experts, if you could spare some time to help this newbie, I will be so very grateful, just please promise not to laugh too...
Replies
2
Views
2,057
Back
Top Bottom