I have been using the following routines to set bits of PLC command words, but found an problem. If the PLC word is cast as a “Short”, which corresponds to a regular word it will work on all bits but bit 15. If the target of the command is an Int setting bit 15 is fine.
Scripts in shared.bit:
def setBit(value, position, newValue): newValue = (newValue & 1) << position mask = 1 << position return (value & ~mask) | newValue def writeBit(tagPath, position, value): import system currentValue = system.tag.getTagValue(tagPath) system.tag.writeToTag(tagPath, shared.bit.setBit(currentValue, position, value))
Script is in a change script function:
#Use Custom Routine writeBit(tagPath, position, value) if(currentValue.getValue() == 1): shared.bit.writeBit("[.]../../../CMD/WR_HR29952",15,1) #write bit to Register Tag system.tag.write(tagPath, 0) #clear this tag
Again if this is used on bit 15 as shown above, the target word remains 0 (MSB of Short not set). If any other bit is set it works.
There must be something I am not seeing, as I would expect that bit 15 of the short can be set as easily as any other bit?