Toggle Individual Bit in DINT OPC Tag

Hi all,

I can see there are a few old posts about this topic but I’m struggling to decipher the answer I’m after.

I have a ControlLogix and have these tags in my tag browser as OPC tags. I also have a button that I want to toggle a bit in a DINT tag in my PLC. I can see that ignition does not recognise this as individual bits in my tag browser.

Am I correct in saying I cannot achieve this using a 2 state toggle button, but have to use a script on a normal button? Is there an easy way to reference the bit such as tagpath.bit in my script, or is the only way bypass the tag and try writing directly to OPC address please?

Thank you

1 Like

Thanks for linking this - the problem is my project is large and I have a lot of tags that would require me creating 32 tags for each and addressing them so was hoping there would be a way to apply dynamically from my button script.

# Parameters
tagPath = "[default]MyDevice/MyDINTTag"  # OPC tag path
bitToSet = 3                             # bit index (0-31)

# Read the current value
currentValue = system.tag.readBlocking([tagPath])[0].value

# Set the specific bit
newValue = currentValue | (1 << bitToSet)

# Reset the specific bit
newValue = currentValue & ~(1 << bitToSet)

#Toggle the specific bit
newValue = currentValue ^ (1 << bitToSet)

# Write the new value back
system.tag.writeBlocking([tagPath], [newValue])

Something like that should do the job. I didn’t test it to be honest.

2 Likes

In places where you would use an OPC Item Path you can dynamically address, e.g. use [MyDevice]MyDINT.3 in a system.opc.readValues or writeValues scripting call.

But there's no syntax to dynamically get/set a bit when you're just referencing an Ignition tag by its tag path.

3 Likes