CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sat Dec 15, 2018 2:26 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
 Post subject: IF NOT AND question
PostPosted: Tue Nov 20, 2018 1:50 am 
Offline

Joined: Mon Oct 12, 2015 5:47 pm
Posts: 31
Could you please explain why the following statement is always FALSE:
Code:
IF PLC^.xInitDone AND NOT PLC^.xError THEN

It runs on RaspberryPi with the latest Codesys Runtime.
PLC is a pointer to ModbusSlaveComPort.
The xInitDone is TRUE, xError is FALSE.

If I rewrite the code in such way it works as expected:
Code:
IF PLC^.xInitDone THEN
   IF NOT PLC^.xError THEN


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Tue Nov 20, 2018 10:01 am 
Offline

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 67
What is the result if you replace dereferenced pointers by only boolean variables ?


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Thu Nov 22, 2018 11:26 am 
Offline

Joined: Thu Aug 30, 2018 8:40 am
Posts: 33
Hello Sir,

ST checks for all the conditions and does not stop early.

AND_THEN does what you describe with nested ifs.

Best regards,
Marcel


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Thu Nov 22, 2018 10:17 pm 
Offline

Joined: Mon Oct 12, 2015 5:47 pm
Posts: 31
dFx wrote:
What is the result if you replace dereferenced pointers by only boolean variables ?

It works as expected.


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Thu Nov 22, 2018 10:17 pm 
Offline

Joined: Mon Oct 12, 2015 5:47 pm
Posts: 31
m.prestel wrote:
Hello Sir,

ST checks for all the conditions and does not stop early.

AND_THEN does what you describe with nested ifs.

Best regards,
Marcel

Sorry Marcel,
I didn't understood your idea.


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Fri Nov 23, 2018 8:49 am 
Offline

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 67
and if you try :
Code:
IF PLC^.xInitDone AND NOT (PLC^.xError) THEN


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Fri Nov 23, 2018 9:03 am 
Offline

Joined: Thu Aug 30, 2018 8:40 am
Posts: 33
Sorry I misunderstood you...

Can you provide a small example application to easyly reproduce?

Best regards,
Marcel


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Mon Nov 26, 2018 1:00 am 
Offline

Joined: Mon Oct 12, 2015 5:47 pm
Posts: 31
dFx wrote:
and if you try :
Code:
IF PLC^.xInitDone AND NOT (PLC^.xError) THEN


Unfortunately it doesn't help. Only nested IFs works fine.
For those who want to reproduce it:
Code:
FUNCTION write_value_bool : BOOL
VAR_INPUT
   PLC         : POINTER TO ModbusSlaveComPort;
END_VAR

IF PLC^.xInitDone AND NOT PLC^.xError THEN
        write_value_bool:= TRUE;
ELSE
        write_value_bool:= FALSE;
END_IF

And yes, you have to wait for xInitDone to become FALSE or xError TRUE. For this purpose you can do xReset:= TRUE (at ModbusSlaveComPort) for one cycle.


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Mon Nov 26, 2018 8:53 am 
Offline

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 67
PhilipLykov wrote:
… xInitDone to become FALSE or xError TRUE...


This does not match written code. Written code says you have to wait xInitDone to TRUE AND xError to FALSE.


Top
   
 Post subject: Re: IF NOT AND question
PostPosted: Mon Nov 26, 2018 9:13 pm 
Offline
User avatar

Joined: Fri Jan 19, 2018 3:38 pm
Posts: 56
Code:
FUNCTION write_value_bool : BOOL
VAR_INPUT
   PLC         : POINTER TO ModbusSlaveComPort;
END_VAR
IF PLC^.xInitDone AND NOT PLC^.xError THEN
        write_value_bool:= TRUE;
ELSE
        write_value_bool:= FALSE;
END_IF


One should avoid the above IF THEN statements. A better solution could be;

Code:
write_value_bool := PLC^.xInitDone AND NOT PLC^.xError;


Which (better) readable is something like;

Code:
FUNCTION xWrtRdy : BOOL
VAR_INPUT
   pMbCom : POINTER TO ModbusSlaveComPort;
END_VAR
xWrtRdy := pMbCom^.xInitDone AND NOT pMbCom^.xError


Now your code is more atomic in nature and "what's in a name". Also, this function can very easily be transformed to a method of some FB.

_________________
https://github.com/Aliazzzz


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

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 5 guests


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:  
cron
Powered by phpBB® Forum Software © phpBB Limited