Link a checkbox to a precise bit inside a word

Hi,

I’m trying to link a checkbox to a word I have inside my Siemens PLC 1500 via the Siemens Enhanced Driver.

Reading the bit works fine if I use the GetBit() expression function, but like this it’s not really linked it just keeps reading the value of said bit and if I click on the checkbox the value on my PLC stays the same.

I found a script in python to do so on internet and it works on a button, but in my case a checkbox would be better to use.

Here’s the script for the button:

	# Read the current word value
	tagPath = "tagPath"
	currentValue = system.tag.readBlocking(tagPath)[0].value
	
	# Toggle bit 4 (for example) using bitwise XOR
	bitToToggle = 4
	newValue = currentValue ^ (1 << bitToToggle)
	
	# Write the new value back
	system.tag.writeBlocking(tagPath, newValue)

Consider using a derived tag. The read expression would simply be getBit(...). The write expression would use bitwise AND and OR to inject the new bit into the existing word.

Then your UI would bidirectionally bind to the derived boolean as usual.

Be aware that this kind of read-modify-write has an unfixable race with simultaneous modifications by the PLC. Ignition can stomp on PLC changes.

Are you sure the new Siemens driver cannot write directly to the bit? (Though it wouldn't surprise me if Siemens is too lame to do it via symbols.)

1 Like

I had a similar problem to solve.
The way that I handled it was create derived tags off of the “word” tag and then you can put a read expression and write expression.
Source Tag Path is self expanatory.
The read expression would be exactly this:
TOINT({source})>>4 & 1
The write expression would be exactly this:
IF({value},TOINT({source}) | (1 << 4),TOINT({source}) & ~(1 << 4))
Then you could put a bidirectional binding to the checkbox

1 Like

I tried your solution but the write function doesn’t work, while the read one does.

I don’t know if I’m doing something wrong with the checkbox also because after I linked it to the derived tag, if I click on it it changes the value to a “true” or “false” but after a couple of seconds it becomes “1” or “0” as the bit it’s reading, and so the checkbox gives me an error.

Here’s the image of the error, it says “expected bool or null, found integer”

image

Maybe try making sure the data type for the derived tag is boolean, or if it is, then try wrapping the whole read expression in a toBoolean.
Also make sure your gateway is in read write mode too in designer if that is where your are trying this from.