# Reading from a OPC UA device, synchronization issues

**URL:** <https://forum.inductiveautomation.com/t/reading-from-a-opc-ua-device-synchronization-issues/57223>\
**Category:** General Discussion\
**Created:** [March 15, 2022, 5:42pm UTC](https://forum.inductiveautomation.com/t/reading-from-a-opc-ua-device-synchronization-issues/57223 "2022-03-15T17:42:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGOSTINO\_SCLAUZERO](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AGOSTINO\_SCLAUZERO](https://forum.inductiveautomation.com/u/AGOSTINO_SCLAUZERO)\
**Post date:** [March 15, 2022, 5:42pm UTC](https://forum.inductiveautomation.com/t/reading-from-a-opc-ua-device-synchronization-issues/57223/1 "2022-03-15T17:42:15Z")

</div>

I’ve got a Siemmens PLC connected via OPC UA.  
I want to read a list of records that are stored in a buffer in the device; each record is made available through a collection of tags.  
There is need of a way for the PLC to inform the client that a new record is available and for the client to notify the PLC that the data has been read and it can go on with the next.  
We came up with the following solution, based on a DataAvailable bit that is set by the PLC when data are available and reset to 0 in an Ignition Script when data has been consumed.

```auto
dataAvailable = system.tag.read("[default]FieldData/dataAvailable").value

if dataAvailable:

    whatever = system.tag.read("[default]FieldData/Whatever1").value

    #do whatever with the data

    system.tag.write("[default]FieldData/dataAvailable", False)

```

This code seems to be susceptible to both repeated reads (the same data gets read twice, even three times) and data loss.  
Obviously this simple ‘handshaking’ has to be implemented correctly on the PLC side.  
PLC people say that the implementation is correct on their side, and that there is some sort of problem that is related with the communication delay.  
My questions are:

1. is this schema too simplistic? Should I use a more comples (two-bits) handshake mechanism?
2. is OPC communication protocol connection-based or broadcast-based, that is: when changing a tag’s value, should i relay that tag modification on the other side happens in the same order they are done on the originating side?

I replaced  
system.tag.write("[default]FieldData/dataAvailable", False)  
with:  
system.tag.writeSynchronous("[default]FieldData/dataAvailable", False)

Without success.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 15, 2022, 5:49pm UTC](https://forum.inductiveautomation.com/t/reading-from-a-opc-ua-device-synchronization-issues/57223/2 "2022-03-15T17:49:37Z")

</div>

You should definitely use two tags for the handshaking whenever possible. Race conditions are possible when one tag is written from both sides.

One approach is described here:

> [@Gateway tag change issue](https://forum.inductiveautomation.com/t/gateway-tag-change-issue/21697/8):
>
> To write each tag from only one direction: PLC sets trigger tag Script runs on trigger tag change, does stuff, and sets handshake tag PLC clears trigger tag when it sees handshake tag Script runs on trigger tag change and clears handshake tag This should make a race condition pretty unlikely,

---

<div class="post-metadata">

**Author:** ![AGOSTINO\_SCLAUZERO](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AGOSTINO\_SCLAUZERO](https://forum.inductiveautomation.com/u/AGOSTINO_SCLAUZERO)\
**Post date:** [March 16, 2022, 7:25am UTC](https://forum.inductiveautomation.com/t/reading-from-a-opc-ua-device-synchronization-issues/57223/3 "2022-03-16T07:25:52Z")

</div>

Great, thank you Kevin. If I wanted to study this topic in more depth, can you provide some relevant links where I can find examples or theory about race conditions in the context of network comunication like this? But close to the PLC field, not generic?
