FreeOpcUa / opcua-asyncio

OPC UA library for python >= 3.7
GNU Lesser General Public License v3.0
1.11k stars 358 forks source link

Cannot set final bit of signed integers using set_attr_bit #1542

Open DavideFigundio opened 9 months ago

DavideFigundio commented 9 months ago

When attempting to set or unset the final bit of signed integer values (sbyte, int16/32/64) I am getting an out of bounds error.

I am connecting to a third party server, getting the node that I require and running the following code:

def set_bit(node: SyncNode, bit: int, value: bool):
    '''Sets the value of the indicated bit for the given node.'''

    if value:
        node.set_attr_bit(asyncua.ua.AttributeIds.Value, bit)
    else:
        node.unset_attr_bit(asyncua.ua.AttributeIds.Value, bit)

This raises an exception for certain values of bit (7 for sbyte, 15 for int16...) giving the reason (int16 example):

struct.error: short format requires -32768 <= number <= 32767

However this does not fail if the function would not actually change the value of the bit, for example the following code would run without issues:

value = asyncua.ua.Variant(-32768, asyncua.ua.VariantType.Int16)
node.set_value(value)
node.set_attr_bit(asyncua.ua.AttributeIds.Value, 15)

Finally, there are no issues when using unsigned variable types (byte, uint16/32/64).

Python-Version: 3.12 opcua-asyncio Version: 1.0.6