Setbit function?

I know I am missing something here, but I have a hard time setting individual bits in DINTs in a ControlLogix PLC. I use the getbit function to read individual status bits, but how do I write an individual bit? I have a toggle button that just needs to set a bit to an individual bit in a Dint tag. I do not want to make individual SQLtags for each one of these bits. A setbit function would be nice…

Any reason why not? This would be the method that I would recommend.

True. Do you want a scripting function or an expression function? Typically one will use bit masking and shifting for bit twiddling. This is quite easy - see this page

Of course, you could write your own functions for this. Create a new module under app called util, and paste this in, and there you go!

[code]# Usage: app.util.setBit("Path/To/myTag", 5) to set bit 5
def setBit(tagPath, n):
import fpmi
val = fpmi.tag.getTagValue(tagPath)
val |= (1 << n)
fpmi.tag.writeToTag(tagPath, val)

Usage: app.util.clearBit("Path/To/myTag", 5) to clear bit 5

def clearBit(tagPath, n):
import fpmi
val = fpmi.tag.getTagValue(tagPath)
val &= ~(1 << n)
fpmi.tag.writeToTag(tagPath, val)[/code]

Hope this helps,


Thanks a lot. Just what I needed.


I was thinking of how that could work with a single SQLTag.

For RSLinx you have some OPC tag path. Let’s use a simple SLC integer example, [my_topic]N7:17, and the SQLTag []my_int that points to it.

You would like to change the OPC Item Path of my_int on the fly. Namely add /0 to read the first bit, /1 to read the second, etc. Very cool concept, but I speculate that it’d have negative implications, particularly if you’re using that same SQLTag elsewhere. This request may have spawned new ideas, though.

As Carl suggested, why not use more tags? The typical response I get is that an integrator has too many tags and it needs to be so dynamic. It’s pretty easy to create a large number of such tags with an import/export to CSV, then use Excel. In my experience, a having a large number of SQLTags is negligible on system performance unless they’re changing values rapidly.

Once the “bitwise” SQLTags are set up, you can easily (dynamically) reference them with Indirect Tag Binding.

I wouldn't advise this method! After you change the path of the tag, it will take some small but not insignificant amount of time for FactorySQL to see the change and re-subscribe to the new OPC path. If you change the path and immediately write, the write will probably try to happen before the path was changed - you can see the problem there! Interesting idea, Nathan, but I wouldn't do it.

That's what I figured, that by design using dynamic tag paths in SQLTags won't work. Thanks for confirmation and reasoning.