CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sat Jul 22, 2017 1:56 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat Feb 18, 2017 12:28 pm 
Offline

Joined: Wed Apr 22, 2015 1:45 pm
Posts: 11
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.

Code:
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

Code:
   (*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?
Code:
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


Code:
   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


Top
   
PostPosted: Sun Feb 19, 2017 12:08 pm 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 974
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.

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
PostPosted: Sun Feb 19, 2017 1:56 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 230
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.


Top
   
PostPosted: Fri Feb 24, 2017 7:32 am 
Offline

Joined: Wed Apr 22, 2015 1:45 pm
Posts: 11
Thank you for your help. I made a PRG and used that.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited