I am new to Codesys and PLC programming in general (please go easy ha!) I'm not looking for code to be written for me just some help and pointing in the right direction.
I am writing some code to send commands to a relay based on input values (to put it simply). Quite basic stuff.
I have wrote a function block that takes a global variable (Open_command:BOOL) and outputs to another global variable (Opened : BOOL).
The function block is simulating a device so I'll eventually get the globals from that.
I now need to create multiple versions of this function block/ device (lets say 100) but I need each iteration of that function block to reference it's own relevant global variable. I think that the best way of doing this would be to use arrays, although I could be wrong. I am aware that for up to 100 instances I could very well manually assign everything but that seems rather time consuming and I want a fancier way of doing it.
Here is a very basic example of what I am looking to do, please note I have not written this in proper code it's just to show what I mean.
Global Variables
V[0-100] int
Open_command [0-100] Bool
Opened [0-100] Bool
Function Block
var input
x : BOOL
Var output
y : BOOL
if x then
y = TRUE ELSE
y = FALSE
The input to my function block will be Open_command, output will be Opened
Example code.
If V[x] > 10 then
Open_command [x] = TRUE
ELSE
Open_command [x] = FALSE
What I can't seem to figure out is how to tie all this together, I have read through the codesys documentation and if anything it has confused me more! ha.
Apologies for the poorly written post but hopefully you understand what I am trying to get at.
Thanks,
Jack
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think you have to look at a FB in a different way.
A Function block (Class) can contain its own data.
In other words don't define loose data in your GVL, but define a instance of a FB (Object) in your GVL:
Hello,
I am new to Codesys and PLC programming in general (please go easy ha!) I'm not looking for code to be written for me just some help and pointing in the right direction.
I am writing some code to send commands to a relay based on input values (to put it simply). Quite basic stuff.
I have wrote a function block that takes a global variable (Open_command:BOOL) and outputs to another global variable (Opened : BOOL).
The function block is simulating a device so I'll eventually get the globals from that.
I now need to create multiple versions of this function block/ device (lets say 100) but I need each iteration of that function block to reference it's own relevant global variable. I think that the best way of doing this would be to use arrays, although I could be wrong. I am aware that for up to 100 instances I could very well manually assign everything but that seems rather time consuming and I want a fancier way of doing it.
Here is a very basic example of what I am looking to do, please note I have not written this in proper code it's just to show what I mean.
Global Variables
V[0-100] int
Open_command [0-100] Bool
Opened [0-100] Bool
Function Block
var input
x : BOOL
Var output
y : BOOL
if x then
y = TRUE ELSE
y = FALSE
The input to my function block will be Open_command, output will be Opened
Example code.
If V[x] > 10 then
Open_command [x] = TRUE
ELSE
Open_command [x] = FALSE
(So when V1 goes above 10 I need Open_command1 = TRUE therefore initiating FB1 output.
V2 > 10, open_command2 = True > FB2 output
V3 > 10, open_command3 = True > FB3 output
...
...
)
What I can't seem to figure out is how to tie all this together, I have read through the codesys documentation and if anything it has confused me more! ha.
Apologies for the poorly written post but hopefully you understand what I am trying to get at.
Thanks,
Jack
Hi Jack,
I think you have to look at a FB in a different way.
A Function block (Class) can contain its own data.
In other words don't define loose data in your GVL, but define a instance of a FB (Object) in your GVL:
Example:
In your code you can directly acces the data and couple it to the IO:
If you want to take this a step further you are also able to add methods and properties to the FB/Class end thereby creating a OOIP program
Related
Talk.ru: 1
Last edit: alexgooi 2024-02-15
Thanks for the reply. Much appreciated.