I have made a program that works, but it don't work as an Function block.
It takes the input from VAR1_1 og ekstern and takes tha last value and send it to VAR_ut.
I'm going to use it on a modbusdevice. So you can set the setpoint on the local device and from a remote panel.
ROGRAMModbusRWVAR(*Variablerforobjektnr.1*)Â Â VAR1:REAL;Â Â VAR1_1:REAL;Â Â VAR1_OLD:REAL;Â Â VAR1_1_OLD:REAL;Â Â Count1:REAL;Â Â ekstern:REAL;Â Â VAR_ut:REAL;END_VAR
  (*LesinndatafraModbus*)  (*VAR1_1koblesmotrettvariabelimodbusleseblokketterkoverteringtilrettdatatype*)  (*VAR1_1mådeklareressomglobalvariabel*)  (*Sjekkeromdetteerførstescanetterstrømstans*)  (*Hvisdeterførstescanhentesdataframodbusogleggesinnieksternvariabel*)  IFCount1<1THEN    VAR1:=VAR1_1;    ekstern(*eksternvariabel*):=VAR1;    VAR1_OLD:=VAR1;    VAR1_1_OLD:=VAR1_1;    Count1:=1;  END_IF;  (*Leseroppnyverdihvisverdiendrersegpåmodbus*)  IFVAR1_1<>VAR1_1_OLDTHEN    VAR1:=VAR1_1;    ekstern(*eksternvariabel*):=VAR1;    VAR1_1_OLD:=VAR1_1;    VAR1_OLD:=VAR1;  END_IF;  (*hentereksternvariabel*)  VAR1:=ekstern(*eksternvariabel*);  IFVAR1<>VAR1_OLDTHEN    (*PRGnavn*);    VAR1_OLD:=VAR1;    VAR_ut:=VAR1;  END_IF;
When I try to make it as an function block it don't work. Can sombody help me understand way?
VAR_INPUTÂ Â ekstern:REAL;Â Â (*eksterinput*)Â Â VAR1_1:REAL;Â Â (*modbusinput*)END_VARVAR_OUTPUTÂ Â VAR_ut:REAL;END_VARVAR(*Variablerforobjektnr.1*)Â Â VAR1:REAL;Â Â VAR1_OLD:REAL;Â Â VAR1_1_OLD:REAL;Â Â Count1:REAL;END_VAR
  IFCount1<1THEN    VAR1:=VAR1_1;    ekstern(*eksternvariabel*):=VAR1;    VAR1_OLD:=VAR1;    VAR1_1_OLD:=VAR1_1;    Count1:=1;  END_IF;  (*Leseroppnyverdihvisverdiendrersegpåmodbus*)  IFVAR1_1<>VAR1_1_OLDTHEN    VAR1:=VAR1_1;    ekstern(*eksternvariabel*):=VAR1;    VAR1_1_OLD:=VAR1_1;    VAR1_OLD:=VAR1;  END_IF;  (*hentereksternvariabel*)  VAR1:=ekstern(*eksternvariabel*);  IFVAR1<>VAR1_OLDTHEN    (*PRGnavn*);    VAR1_OLD:=VAR1;    VAR_ut:=VAR1;  END_IF;
Thanks
Espen
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
a function block does not save any variables, so if you need to keep any values , you will have to use in/out variables with external vars (global)
so your count is always 0 for example.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2017-02-19
Originally created by: scott_cunningham
Function blocks keep loca variable memory - counter will work. Functions do not - counter will not work. VAR_IN_OUT type are for situations where the variable is both an input and output ("I need it and I may also change it).
Make sure you are calling the Function block instance every PLC scan.
This is not calling the B - it only changes the variable: myFB.ekstern := 5.3;
This calls the FB and runs it: myFB();
Also, VAR_INPUTs cannot be changed from the inside - if you change ekstern in your FB, it does not change the variable you mapped to it. In the FB you change the VAR_INPUTs, but the next call, they will be back to your old values (VAR_INPUT means "I give you this value and you cannot change it"). Programming recommendations suggest never modifying VAR_INPUTs inside the POU.
Try your program again using the variables as defined in the FB (programs can also have VAR_INPUTS VAR_OUTPUTS too). I don't think it will work and you should find your problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello
I have made a program that works, but it don't work as an Function block.
It takes the input from VAR1_1 og ekstern and takes tha last value and send it to VAR_ut.
I'm going to use it on a modbusdevice. So you can set the setpoint on the local device and from a remote panel.
When I try to make it as an function block it don't work. Can sombody help me understand way?
Thanks
Espen
a function block does not save any variables, so if you need to keep any values , you will have to use in/out variables with external vars (global)
so your count is always 0 for example.
Originally created by: scott_cunningham
Function blocks keep loca variable memory - counter will work. Functions do not - counter will not work. VAR_IN_OUT type are for situations where the variable is both an input and output ("I need it and I may also change it).
Make sure you are calling the Function block instance every PLC scan.
This is not calling the B - it only changes the variable: myFB.ekstern := 5.3;
This calls the FB and runs it: myFB();
Also, VAR_INPUTs cannot be changed from the inside - if you change ekstern in your FB, it does not change the variable you mapped to it. In the FB you change the VAR_INPUTs, but the next call, they will be back to your old values (VAR_INPUT means "I give you this value and you cannot change it"). Programming recommendations suggest never modifying VAR_INPUTs inside the POU.
Try your program again using the variables as defined in the FB (programs can also have VAR_INPUTS VAR_OUTPUTS too). I don't think it will work and you should find your problem.
Thank you for your help. I made a PRG and used that.