Read bits with getBit

I am trying to use getBit to read bits. In the current case, just for testing purposes, I placed a button on the screen. When I press it, if my bit 0 is equal to true, I would like it to execute the print “it worked.”

In my PLC, I am activating and deactivating the bit to test this.

I saw a function here in the forum, but I don't know how to use it. I know I'm new to this and I may be wrong in Python, but I would still like some help with this, please.

My script looks like this:

def getBit(value, bit):
    return (value & (1 << bit)) >> bit

readValue = event.source.parent.Dados.RUNTIME.WBIOS5_BB
bitWant = 0

if getBit(readValue, bitWant) == True:
	read & (1 << bitWant) >> bitWant
	print 'it worked'

Is there anything preventing you from making an Ignition tag that directly reads the targeted bit of the (I assume) integer from the PLC? Most PLCs and protocols support this, though there are some that do not.

If you can't read individual bits, then I would recommend making a custom property, either on the button or view root container, and using an expression binding to read the target bit, something along the lines of getBit({path.to.tag}, 0). Then in your button script, reference that custom property as one of your checks.

2 Likes

Dear Ryan,

In some supervisory situations, I use the expression and custom properties.

In the one I am testing, it would greatly facilitate my development. After much testing, I managed to make it work. Once again, thank you very much for your attention.

Example that worked below

def getBit(i, n):
	return (i >> n) & 1

test = event.source.parent.Dados.RUNTIME.WBIOS5_BB

if getBit(test, 0):
	system.gui.messageBox(""+str(test)+"", "result")