Bidirectional binding with expression

I need to use a numeric field to show and to modify a tag value.
If this numeric field is directly connected, with tag binding, to a tag and bidirectional is active, all is ok.

When I need to use a binding expression to show the value and a scripting to modify the tag value I have a problem: when I start the client in a tag value I find value -1, and the old value is gone…

How can I solve this problem?

1 Like

I don’t understand why one we need to do this. If you bind the value you can edit the value and have that value pass to third party, say PLC. Why are you using scripting to modify the tag?

I using script to change the value displayed in order to the unit selected by the operator that use the client.

I believe I’ve seen the -1 value indicating an expression not evaluating when I was not entering indirect references correctly into an expression–see my question and the answer to that here:

If that doesn’t help, perhaps someone can help if you post the code you are using in your expression.

This is the binding on Value and the scripting for a Numeric Text Field that have to show a tag value an allow to change this value. I meed to convert dis value in the correct unit for plc and show the value in the unit selected in the client.
The value of the tag is for example 100, when I open the page the walue show is -1 and the value in the tag is -1…so I lose my data in the tag…

I’m not sure if this is relevant but in the thread


there is this post:

[quote=“Kevin.Herron”]Right, so this is a bit unfortunate, but you can’t access script modules from the runScript() expression function on a tag.

Tags are globally scoped (belong to the gateway, basically), whereas a script module belongs to a specific project, so there’s no access.

This is one of the problems that the introduction of a globally-scoped script module area will solve in Ignition 7.7.[/quote]

i.e. it might be that the runScript is not working

It looks like you are trying to bind a string value (coming from the expression) to a float value.

Binding and Sripting are correct.

I understand your goal is to:
[li]1 - display the value via an expression which calls a function to convert the value to the units of measure selected in the client.[/li]
[li]2 - write changes to value back to tag via script to convert the value from the units of measure selected in the client to the base units of measure stored in the tag.[/li][/ul]
I’ve done this often in other software packages, but have not yet done a project in Ignition which required it, so others may have more helpful feedback.

There are a number of possible failure points here. To troubleshoot, I would check the following:
[ul][li]1 - is value being changed in actual tag in PLC (assuming this tag is in a PLC), or is it just showing wrong in Ignition?[/li]
[li]2 - if value is changed in actual tag in PLC, try implementing only one piece of the script that writes to the tag at a time–for instance, try writing a static value to the tag, rather than the app.Units.ToPLCunits function to isolate the whether the problem is the value returned from the function, or something else.[/li]
[li]3 - if value is NOT changed (not -1) in PLC, take a closer look at the expression. Again, break it into parts and implement one piece at a time to figure out which part is not working.[/li][/ul]

When troubleshooting scripts, I use system.gui.messageBox(messageString) to show me what is happening – for instance to display what I’m feeding into a function, and then display what comes back from the function, etc to isolate where the failure is happening.

I hope something here helps you figure it out!

I use this Numeric Text Field to set a value in a tag for the plc, for example the total capacity of a tank. For example I set tag TankCapacity=100; all is right, in the tag I see value=100. The tag value is setted only inside Ignition, plc only read this tag. Now I close Ignition.
When I re-open Ignition client and I go in the page with the Numeric Text Field, I find in the display and in the tag, value = -1.

Assuming app.Units.ToHMIunits does not write to the tag, the changed PLC tag suggests the problem is occurring in the event script.

You could try adding a couple message boxes (or use print statements, if you prefer) to your script like this to confirm what value you are feeding into app.Units.ToPLCunits as well as what you are getting back from it:

try: if event.propertyName == "floatValue": tagPath = "DAST/Mud Tanks/Warn Th Max AMV" system.gui.messageBox("event.newValue = " + event.newValue) system.gui.messageBox("app.Units.ToPLCunits = " + app.Units.ToPLCunits(event.newValue, "Volumes")) system.tag.write(tagPath, app.Units.ToPLCunits(event.newValue, "Volumes")) except: print "Unexpected error"

If you do something like the above (you may need to type cast the values to strings), are you seeing the expected values output for event.newValue and app.Units.ToPLCunits? Or is one or both of them off?

This is almost certainly happening because the values are moving to -1 at some point. All you need to do is filter out when event.newValue is -1 (or whatever value is producing a -1).