Writing to Modbus tag with swapped bytes

In a follow up to my other post about reading a Modbus address whose format is swapped bytes. That is a tag whose value I know to be -32768 or 00000000 00000001 in binary but Ignition is reading it as 128 or 00000001 00000000 in binary since there is currently no option to swap the bytes of a 16 bit integer in the Modbus settings.

I also need to write back to a Modbus register whose format is swapped bytes. At this point the only way I can figure out how to accomplish the byte swapping is to have 3 tags.

  1. A Raw value Tag- This is the tag with the byte in the wrong order as read from the device
  2. The corrected Expression tag - This is the tag that uses the expression Phil recommended in the other tread that swaps the high and low bytes using bit shifting
  3. A memory tag for writing new values to - This tag would have an script in the value changed option that shifts the high low bytes and then writes back to tag 1

Is there a better method for doing this? I plant to create a datatype that handles the tag creation in one go, it just seems like Iā€™m going about this inefficiently.

If you can do your bit-shifting entirely with expressions, you might be able to use a derived tag for this.

Thanks Paul, I was able to accomplish it using this in the Read Expression

((toInt({source}) & 0xff00) >> 8) | ((toInt({source}) & 0xff) << 8)

and this in the Write Expression

((toInt({value}) & 0xff00) >> 8) | ((toInt({value}) & 0xff) << 8)

Without the "toInt" cast it complains that I'm trying to do a bitwise AND to an object.
It'd be really cool if I could do this kind of operation without having to have a base tag to derive off of, but this is at least a more efficient than the 3 tag setup I was thinking of!

1 Like