Tag.read after tag.write

Hi,
I have a question about system.tag.read and system.tag.write instructions: if I execute a read just after a write on the same tag path, e.g.

system.tag.write(tagPath,someValue) newValue=system.tag.read(tagPath).value
shall the newValue be the value just written to tagPath by the write instruction? Or is the result not deterministic: it could be the old value, i.e. the value stored in tagPath before the write instruction, as well as the new one, i.e. the one written to the tagPath with the write instruction, it depends on network speed, CPU load, Gateway load, etc?

Thanks in advance, regards

Edit
Just found the answer in the help: system.tag.writeSynchronous does what I need, i.e. write and wait for the value to be stored in the PLC.

[quote=“pgmo”]Edit
Just found the answer in the help: system.tag.writeSynchronous does what I need, i.e. write and wait for the value to be stored in the PLC.[/quote]Yes, but remember that writeSynchronous() cannot be used in the foreground, that is, it cannot be used directly in event scripts. You may only use it in a thread spawned by system.util.invokeAsynchronous().

Thanks pturmel, you are right.

So I am struggling with this as well.I write to a dataset tag via a save button. I see the values change in the tag browser, but when I do a tag read too fast but still after I see the change, it doesn’t read the new data. I have to wait about 3 seconds or more.

These are both buttons, so it doesn’t seem that Synchronous is an option for me. Why does it not read the new data as soon as I can see it in the tag browser? How can I get an indication that the data is ready to be read?

Thanks.

You’ll have to do something annoying like use system.util.invokeAsync to invoke a function that does a synchronous write and read on a different thread. Then finish whatever logic your button press is doing there, making sure that if you interact with the UI you do it back on the UI thread using system.util.invokeLater…

So system.util.invokeAsync to do work in another thread, then system.util.invokeLater to toggle back to normal thread? Is this how you’d write code that might take a minute to load and you don’t want the UI to show frozen?

Thank you

Yes, exactly.

And preventing a frozen UI is why writeSynchronous throws an error when you try to use it from the UI thread.

1 Like