Program and Function block

espenbo
2017-02-18
2017-02-24
  • espenbo

    espenbo - 2017-02-18

    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.

    ROGRAM ModbusRW
    VAR
    (*Variabler for objekt nr. 1*)
       VAR1: REAL;
       VAR1_1: REAL;
       VAR1_OLD: REAL;
       VAR1_1_OLD: REAL;
       Count1: REAL;
       ekstern: REAL;
       VAR_ut: REAL;
    END_VAR
    
       (*Les inn data fra Modbus*)
       (*VAR1_1 kobles mot rett variabel i modbus lese blokk etter kovertering til rett datatype*)
       (*VAR1_1 må deklareres som global variabel*)
       (*Sjekker om dette er første scan etter strømstans*)
       (*Hvis det er første scan hentes data fra modbus og legges inn i ekstern variabel*)
       IF Count1 < 1 THEN
          VAR1 := VAR1_1;
          ekstern(*ekstern variabel*) := VAR1;
          VAR1_OLD := VAR1;
          VAR1_1_OLD := VAR1_1;
          Count1 := 1;
       END_IF;
       (*Leser opp ny verdi hvis verdi endrer seg på modbus*)
       IF VAR1_1 <> VAR1_1_OLD THEN
          VAR1 := VAR1_1;
          ekstern(*ekstern variabel*) := VAR1;
          VAR1_1_OLD := VAR1_1;
          VAR1_OLD := VAR1;
       END_IF;
       (*henter ekstern variabel*)
       VAR1 := ekstern(*ekstern variabel*);
       IF VAR1 <> VAR1_OLD THEN
          (*PRG navn*);
          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;   (*modbus input*)
    END_VAR
    VAR_OUTPUT
       VAR_ut: REAL;
    END_VAR
    VAR
    (*Variabler for objekt nr. 1*)
       VAR1: REAL;
       VAR1_OLD: REAL;
       VAR1_1_OLD: REAL;
       Count1: REAL;
    END_VAR
    
       IF Count1 < 1 THEN
          VAR1 := VAR1_1;
          ekstern(*ekstern variabel*) := VAR1;
          VAR1_OLD := VAR1;
          VAR1_1_OLD := VAR1_1;
          Count1 := 1;
       END_IF;
       (*Leser opp ny verdi hvis verdi endrer seg på modbus*)
       IF VAR1_1 <> VAR1_1_OLD THEN
          VAR1 := VAR1_1;
          ekstern(*ekstern variabel*) := VAR1;
          VAR1_1_OLD := VAR1_1;
          VAR1_OLD := VAR1;
       END_IF;
       (*henter ekstern variabel*)
       VAR1 := ekstern(*ekstern variabel*);
       IF VAR1 <> VAR1_OLD THEN
          (*PRG navn*);
          VAR1_OLD := VAR1;
          VAR_ut := VAR1;
       END_IF;
    

    Thanks
    Espen

     
  • shooter

    shooter - 2017-02-19

    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.

     
  • 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.

     
  • espenbo

    espenbo - 2017-02-24

    Thank you for your help. I made a PRG and used that.

     

Log in to post a comment.