Reading and Writing to Multiple Bits on a Register

Hi All,

I was wondering if there was a way to read grouped bits from a register. One of the devices I'm reading/writing from has registers that require you to read and write to grouped bits for different functions.

For example:
[Device_1]HRUS108 gives you an operating status at Bit 2 and Bit 3. 0=off, 1=,on, 3= fault, 4 = Error.
On the same device, Bit 14 and Bit 15 would be something like 0=Error, 1=Disconnected, 2 = Connected, 3= Alarm.

So there's a bunch of data shoved into one register. Different Registers require me to write to grouped bits as well, some of them spanning 3 total bits instead of 2 like the above example.

Here's another example of a sample register:

Bit 8 and 9 you could write to, however I'm unsure how to write to the grouped bits0-2 and bits10-15
Is this possible to do?

You would have to convert the bit fields to groups of booleans, and write the booleans in a single operation (single list for system.tag.writeBlocking() or system.opc.writeValues()). Do note that it won't work at all if the device doesn't implement the modbus function code for masked writes to holding registers (Schneider is notorious for this).

FWIW, my alternate Modbus driver does implement bit fields in this fashion for you, so you get an unsigned integer to work with.

How would you group the booleans in the bitfield? I could only find a resource where you can read individual bits of the register.

When reading, just read the whole register and apply bitwise logical AND and a shift to get just the field.