How to do a AND

Discussion of application development using iX Developer, including but not limited to getting started, using the functions tab, properties, objects and installation.
Post Reply
KevinA.
Posts: 34
Joined: Wed Oct 24, 2012 1:22 pm

How to do a AND

Post by KevinA. »

I have to enable a light when two controller tags are active so I was wondering if there is any way to do a AND without having to use a script?

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: How to do a AND

Post by Edmund »

Are you running on a PC or a Panel?

If you are running on a PC you could let the first tag operate the Visibility property and the second tag the Opacity property (I believe Opacity is only available on PC-targets).

On a panel you can put a rectangle (in the same color as your background) on top and let one of the tags hiding it and the other one the "light-icon"...

Maybe there is some other and more simpler way, but not that I know of (without using scripts).

Best Regards

Edmund
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

KevinA.
Posts: 34
Joined: Wed Oct 24, 2012 1:22 pm

Re: How to do a AND

Post by KevinA. »

And with a script how would it be? I tried to code it referring myself to a website to learn how to code C # but it downt work at all.

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: How to do a AND

Post by mark.monroe »

Code: Select all

if((Globals.Tags.MyTag1.Value == 1) && (Globals.Tags.MyTag2.Value == 1)){
   //Do Something here
}

http://csharp.net-tutorials.com/basics/if-statement/
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: How to do a AND

Post by Edmund »

Well, easiest way would be.

Go to tags, create a new tag (internal, BOOL) called Tag3.

Hit the script tab in the bottom.

To the left you have all your tags, expand tag 1 and DoubleClick ValueChange.

A new eventhandler is created for you

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
			
}
This function will be called every time the tag changes value.

Now we have to check if the tags are both true and if they are lets set our third tag to true as well.

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
So now we check if both are true then we set Tag3 to true or false.

But right now we only check the status each time Tag1 is changed, we also need to do this when Tag2 is changed.

Code: Select all

void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
This will work but it creates duplicated code, which is BAD!

Let´s create a little function that we can call from each eventhandlers.

Code: Select all

void DetermineStatusForTag3()
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
And call the function from the eventhandlers

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
		
void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
Of course we can make this even better and more flexible by passing the tags as parameters to the function and so on... But that’s the next lesion.

Complete code

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
		
void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}


void DetermineStatusForTag3()
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
Now you can use Tag3 to show or hide your light.

Best Regards
Attachments
Script for Tags
Script for Tags
img1.png (63.75 KiB) Viewed 11363 times
Tags
Tags
img0.png (46.79 KiB) Viewed 11363 times
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

KevinA.
Posts: 34
Joined: Wed Oct 24, 2012 1:22 pm

Re: How to do a AND

Post by KevinA. »

Great!! Thanks a lot Edmund, that really helped me to understand a bit more the way C# works, thank you very much!!

Post Reply