Simple property enumeration help

I wish to add 1 to the value of an LED display each time a tag value is equal to a specific value. My java expereince is limited

when i create the expression I get a syntax error on token: BOUND CONST

this is my expression

if({ToteLoop/L_2_2MPE1} = 65536)
{Root Container.LED Display.value} ++ 1;

any help most appreciated

The expression language doesn’t do assignments – it’s not java. The closest to what you showed would be something like (bound to the LED display’s value):if({ToteLoop/L_2_2MPE1} = 65536, {Root Container.LED Display.value}+1, {Root Container.LED Display.value})Note that it returns a value – either the original value plus one, or the original value unchanged.
But this won’t be very reliable – it doesn’t restrict itself to value changing to 65536 from some other value. Opening the window with value already 65536 will give you an extra increment.
Consider using a memory tag for the display value, with a tag change event on the original tag to do the increment. Then you can check for startup conditions and verify that the new value != the old value.

2 Likes

thanks for the help phil i’ll let you know how I get on

I don’t think incrementing a value on a window object is the best approach. You would be better to use the tag’s ‘Value Changed’ event to increment another tag. The script will look something likeif currentValue.value == 65536: system.tag.write("[~]counter", system.tag.read("[~]counter").value + 1)This may mean that you have to reset the counter when appropriate.

1 Like

yes you were right this isnt very reliable

so i’ve created a memory tag, would i put Al’s code as an expression in that tag and use that value to display in the led

my memory tag is called recric.

what i’m doing is counting product past a PEC on a conveyor during a shift

i’ve gone into tag events and gone in to value changed,

i can work out enough that this is what i need to chnage but agian my coding lets me down

this is the line
def valueChnaged(tagPath, previousValue, currentValue, initialChange, missedEvents):

do i need to add my code to this? how does it point to the new momeory tag i have created called \root container\recirc

I’m not sure why your tag would be called ‘/root container/recirc’. You should be creating the new tag using the SQL TAG Browser under the ‘Tags’ folder, not the ‘Client’ folder - this will ensure it is a SQLTag (which exists on the server) and not a Client tag (which exists on each client independently).

Here’s the complete valueChanged script for a tag called ‘recirc’ under a folder called ‘Test’:[code]def valueChanged(tagPath, previousValue, currentValue, initialChange, missedEvents):
“”"
Fired whenever the current value changes in value or quality.

Arguments:
	tagPath: The full path to the tag (String)
	previousValue: The previous value. This is a "qualified value", so it
	               has value, quality, and timestamp properties.
	currentValue: The current value. This is a "qualified value", so it has
	              value, quality, and timestamp properties.
	initialChange: A boolean flag indicating whether this event is due to
	               the first execution or initial subscription. 
	missedEvents: A flag indicating that some events have been skipped due
	              to event overflow.
"""
if currentValue.value == 65536:
	system.tag.write("[~]Test/recirc", system.tag.read("[~]Test/recirc").value + 1)

[/code]Make sure you indent thelines of code using tabs, not spaces.

1 Like

thanks Al, thats really helpful, yes I have created my tags in the tags folder. so all ok there.

i’ve created the code as follows for the tag of the photo eye (see attached code.jpg)

but i receive an error next to the tag in the tag browser (see error.jpg)

in this instance any change in value of my tag will indicate a product has passed by, so it may be simpler to change the code to enumerate for any chage of value



This script should run for any change so you can take whatever action you want when the tag value changes. You might also want to use the ‘initialChange’ flag to ensure it is not triggered on first run.

From your second screenshot, I can see that you’re missing a bracket after ‘system.tag.read’. I would also use the tag browser (the small icon to the upper right of the script editor) to insert tag paths into the script to make sure they are correct.

Once your script is correct the error flag should hopefully disappear.

1 Like

all works, thanks for your help and perservering with me Al :smiley: :smiley:

al, one last thing, i want to use a button to reset a tag value to 0 when its pressed

i tried this but it doesnt work

if currentValue.value == 1 :
system.tag.write("[.]Pic_Tower_Count".value 0)

any thoughts?

Hi!

Did you put a comma between the tag name and the zero?

Veering off topic for a bit: Welcome to the forums! :smiley:

1 Like

The tag is just represented by a string so you don’t need the value property:if currentValue.value == 1 : system.tag.write("[.]Pic_Tower_Count", 0)Have a look here for examples.

Remember to indent your code under the ‘if’, either with spaces or a tab (but don’t mix both in a script!).

1 Like

This post is 8 years old, but I wanted to check if we still can't increment very well in expression language.

I am using an event script for uptime per machine on a production line.
Gateway is processing many scripts constantly, and are not as fast as expressions I think.

Incrementing is assignment, so no. You can call a script with runScript in a binding, where the script has such a side effect, but it's a hack. (So too with my objectScript().) Including the target property in an expression directly makes a circular reference, and the expression editor tries to stop you (because it blows up the CPU).

1 Like

Is the hour meter transaction group bi-direction writing with DB wins better than uptime from event script in terms of performance?

I think I wrestled with this many times when I create uptime for a new machine, and keep coming back to event scripts.