You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old February 21st, 2021, 06:22 AM   #1
Gragtor
Member
Netherlands

Gragtor is offline
 
Join Date: Feb 2021
Location: ---
Posts: 2
Beginner's question: Why does a variable stays true in Codesys V3

Hi all,


I'm just starting out in the plc world. For school we had some introduction into codesys v3.5, basically we get given visualizations and have to provide the code either in ladder or in function block. I decided to also try to code the assignments in structured text as an exercise.

Thanks for bearing with me. On to the question:
In the attached screenshot you can see both the ladder and the structured text program. The ladder works fine. I can switch pumps on and off, and when the thermal overload kicks in (via button) the corresponding visual alerts turn on and p1 switches to p2.
I've got the same thing working in ST, but for one thing. the second visual alert (H4) stays true, and I cannot work out why.
Does anyone have an idea?
Attached Images
File Type: jpg Capture.JPG (240.4 KB, 88 views)
  Reply With Quote
Old February 21st, 2021, 06:52 AM   #2
drbitboy
Lifetime Supporting Member
United States

drbitboy is online now
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,322
Short answer: the two programs behave differently because the two programs are not equivalent.


The first thing to learn about programming is this: the only thing worse than a program not doing what you want it to do, is when it does exactly what you told it to do. Anyone who keeps that in mind has a chance to be successful at programming. Anyone who does not fully embrace that mindset will fail.



TL;DR


Change all of the [ELSIF ...] clauses to [IF ... END_IF];


In the ladder program, every rung, along with its IF-expression-equivalent logic, is performed on every scan.


With the [IF ... ELSIF ... ELSIF ...] construct, only statements in the first TRUE IF or ELSIF expression's clause are executed; in fact, all following ELSIFs' expressions are not even evaluated (or should not be) once one IF's or ELSIF's expression is TRUE. Changing all to IFs makes the ST more similar to the ladder.


Just to be clear on jargon :


Code:
IF expression1 THEN
  clause1statements;   (* this will only be executed if expression1 is TRUE)
ELSIF expression2 THEN  (* this will not even be evaluated if expression1 is TRUE *)
  clause2statements;    (* this will only be executed if expression1 is FALSE AND expression2 is TRUE)
ELSIF expression3 THEN  (* this will not even be evaluated if expression1 is TRUE OR expression2 is TRUE *)
  clause3statements;    (* this will only be executed if expression1 is FALSE AND expression2 is FALSE AND expression3 is TRUE *)
...                     (* et cetera *)
END_IF;
I have not evaluated the code in detail, but if you make the change from [ELSIF ...] to [IF ... END_IF] and the ST then works, then that was the problem, and doing so takes less time than trying to figure out exactly which ELSIF blocked the later one.


Update: I looked again and now understand which earlier ELSIF is blocking the final ELSIF expression from even being ever evaluated, so it never even gets close to Resetting .H4. However, I leave it to you to find it for yourself, because when you do that for yourself then will either be forced to forever take the mindset recommended above, or give up programming as a career.

Last edited by drbitboy; February 21st, 2021 at 07:11 AM.
  Reply With Quote
Old February 21st, 2021, 07:09 AM   #3
L D[AR2,P#0.0]
Lifetime Supporting Member
United Kingdom

L D[AR2,P#0.0] is offline
 
Join Date: Nov 2006
Location: UK
Posts: 6,193
Code each rung of ladder with ST, e.g. for rung 1


Code:
gv1.P1:=(gv1.S1 OR gv1.P1) AND NOT gv1.Stop AND NOT gv1.S2;
gv1.H1:=gv1.P1;
__________________
S7-300 to 1500 conversions done - email to s7conversions@hotmail.com
  Reply With Quote
Old February 21st, 2021, 09:28 AM   #4
Gragtor
Member
Netherlands

Gragtor is offline
 
Join Date: Feb 2021
Location: ---
Posts: 2
-

Thanks so much, the both of you. You made this into a productive sunday.

Somewhere I had gotten the idea that you cannot simply declare in ST, but that you had to invoke using loops or conditionals. Works perfectly now that I followed the rungs on the ladder.

Also the realization that an elsif is part of the if statement and that only one of these can be executed each scan. Makes perfect sense. Hindsight is 20/20
  Reply With Quote
Old February 21st, 2021, 09:39 AM   #5
drbitboy
Lifetime Supporting Member
United States

drbitboy is online now
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,322
Quote:
Originally Posted by Gragtor View Post
Somewhere I had gotten the idea that you cannot simply declare in ST, but that you had to invoke using loops or conditionals. Works perfectly now that I followed the rungs on the ladder.

Thanks for the feedback.



@LD's solution is the right one, of course, and much cleaner. That said, it's good you now understand where it went wrong; the making mistakes is important i.e. so we don't repeat them (hopefully ;-)).



IFs/ELSIF/ELSE clauses in ST for discrete logic implement the behavior of Set/Reset (from ladder).
  Reply With Quote
Old February 21st, 2021, 09:53 AM   #6
Steve Bailey
Lifetime Supporting Member + Moderator
United States

Steve Bailey is online now
 
Steve Bailey's Avatar
 
Join Date: Apr 2002
Location: The boondocks of Western Massachusetts USA
Posts: 7,425
Quote:
IFs/ELSIF/ELSE clauses in ST for discrete logic implement the behavior of Set/Reset (from ladder).
I would argue that IF (without ELSE) implements SET/RESET while IF/ELSE implements the AB XOR instruction.

X Y
-] [------( )-



is the equivalent of

IF X
Y := 1
ELSE
Y := 0
ENDIF


X Y
-] [------(Set)-



is the equivalent of

IF X
Y := 1
ENDIF
  Reply With Quote
Old February 21st, 2021, 12:13 PM   #7
drbitboy
Lifetime Supporting Member
United States

drbitboy is online now
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,322
Quote:
Originally Posted by Steve Bailey View Post
I would argue that IF (without ELSE) implements SET/RESET while IF/ELSE implements the AB XOR instruction.

X Y
-] [------( )-



is the equivalent of

IF X
Y := 1
ELSE
Y := 0
ENDIF

and it is exactly the same as


Code:
   X     Y
--] [---(S)---

   X     Y
--] [---(R)---
  Reply With Quote
Old February 21st, 2021, 03:26 PM   #8
drbitboy
Lifetime Supporting Member
United States

drbitboy is online now
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 2,322
Quote:
Originally Posted by drbitboy View Post
and it is exactly the same as


Code:
   X     Y
--] [---(S)---

   X     Y
--]/[---(R)---

Fixed typo above.
  Reply With Quote
Old February 22nd, 2021, 06:36 AM   #9
parky
Member
United Kingdom

parky is offline
 
parky's Avatar
 
Join Date: Oct 2004
Location: Midlands
Posts: 2,956
Due to different platforms it is possible it may work in different ways,
Here is some ST for if/else & using the ST SET/RESET functions, you can see they all do what is expected but the code is actually different.
IF My_X THEN
My_Y:=TRUE;
ELSE
My_Y:= FALSE;
END_IF;

IF My_X THEN
My_Y:= TRUE;
END_IF;

SET(My_X,My_Y);
RST(NOT My_X, My_Y);
Note: that using the ST Function SET & RESET is the simplest code wise (Step 17 to 20)
Attached Images
File Type: png SET_Reset.png (38.2 KB, 46 views)
  Reply With Quote
Old February 23rd, 2021, 12:53 AM   #10
(8{)} ( .)
Lifetime Supporting Member
Israel

(8{)} ( .) is offline
 
(8{)} ( .)'s Avatar
 
Join Date: Apr 2004
Location: Israel
Posts: 606
Quote:
Originally Posted by Steve Bailey View Post
I would argue that IF (without ELSE) implements SET/RESET while IF/ELSE implements the AB XOR instruction.

X Y
-] [------( )-



is the equivalent of

IF X
Y := 1
ELSE
Y := 0
ENDIF


X Y
-] [------(Set)-



is the equivalent of

IF X
Y := 1
ENDIF
Actually it's the equivalent of y := x;
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Question: Variable Tags in Cicode charleschia1986 LIVE PLC Questions And Answers 0 August 18th, 2010 11:16 AM
beginner's question plc-learner LIVE PLC Questions And Answers 5 July 20th, 2009 07:42 AM
S7 question. Clearing Variable Dec. Table Johnny T LIVE PLC Questions And Answers 9 October 24th, 2005 01:13 PM
Modicon Concept V26 variable question jimdi4 LIVE PLC Questions And Answers 5 September 7th, 2004 10:14 PM
Beginners Question SysApp LIVE PLC Questions And Answers 4 October 26th, 2002 08:46 AM


All times are GMT -5. The time now is 09:26 PM.


.