OPC Tag - Clear Value

Hello,

I have OPC Tags (TCP Drivers) that add the incoming data into a MS SQL Table every time the Value Changed Tag Event fires. This works great 99% of the time. Sometimes, the value will get written to the database again though. This definitely happens any/every time I go into the Tag Event Value Changed screen, and end up clicking OK, or Apply (regardless if I made any changes or not). Now I understand why it’s doing it at these times, but what I’m wondering is how can I clear the tag’s value after my code executes? I’m already checking to see if currentValue == None at the very beginning. But how can at the end of my code, I set currentValue to None so that it can’t execute again unless the value truly did change? I’ve tried several methods of writing to the tag itself, and nothing seems to error out, but nothing seems to work either. I’m obviously reading the “Message” field from the OPC Item Path. I need to be able to clear out the “Message” field I’m guessing.

Any help would be greatly appreciated…

Thanks in advance!

-Lenny

There should be a “previousValue” in scope that you can check against.

1 Like

Ugh… I. Feel. So. Stupid.

That seemed to have done the trick! I’ve been looking at this stuff for two weeks. Guess I was looking “too” hard… Thanks again!

-Lenny

I take that back, that isn’t 100% either…

Does anybody know how to clear the value altogether? I think that would be the best fix for this issue.

-Lenny

You might need to make a memory tag that you write to when the OPC tag changes, and you can clear at will when you want it cleared.

You can’t clear the OPC tag because the value comes from the OPC server and I believe in this case it’s read-only.

1 Like

No, it’s really not. In almost all cases, especially when networks are involved, tags and storage registers should only ever be written by one source. In your case, the proper decision on whether the value changed is to compare to what is in the database, not what was in the tag for the previous event.
In such cases, I use a function in a script module to do the comparison, and the first time it is called, it reads the last value from the database. Which it then stores in a module-global dictionary, which is updated after each actual DB insert. This approach works regardless of quality changes, DB connection failures, gateway restarts, and even script module edits.

1 Like

I was thinking the same thing, but was hoping to keep the TAG count down. Ok, thanks! I’ll try that…

-Lenny