Skip to content

Modbus Swap Operations

The Modbus specification states that the big-endian format is used for 16-bit data. However, this might differ depending on the manufacturer's implementation.

The Modbus device service can carry out a swap operation on the order of data. This handles any implementation differences for the following data types: Int32, Int64, Uint32, Uint64, Float32, Float64.

The following swap operations are supported:

Swap Operation Examples

The following examples show how byte and word swapping can be used when reading or writing data to a Modbus device.

32-bit example with no swapping

Without enabling byte- or word-swapping, the Modbus device service will access the 32-bit hexadecimal reading 41 45 85 1f and convert it to the Float32 value 12.345:

Register Value
40001 41 45
40002 85 1f

64-bit example with no swapping

Without enabling byte- or word-swapping, the Modbus device service will access the 64-bit hexadecimal reading, 40 93 4a 3d 70 a3 d7 0a, and convert it to the Float64 value, 123.456:

Register Value
40001 40 93
40002 4a 3d
40003 70 a3
40004 d7 0a

Byte Swapping

If enabled, byte-swapping carries out a swap operation on two bytes of data, changing the order of the bytes in the reading. As Modbus registers are two bytes in length, byte-swapping will swap the order of each byte within a register. For example, a register with a value of 45 41 will be read as 41 45. Here, the last byte has been placed at the front of the reading.

To enable byte-swapping, set the isByteSwap attribute to true, as shown in the following device profile extract:

attributes:
  { primaryTable: "INPUT_REGISTERS", startingAddress: "4", isByteSwap: "true", isWordSwap: "false" }

32-bit Example with Byte Swapping

Using byte-swapping on the below 32-bit data set, the hexadecimal reading of 45 41 1f 85 will be swapped to 41 45 85 1f. As the reading has been byte-swapped, it will still produce the Float32 value of 12.345.

Register Value
40001 45 41
40002 1f 85

64-bit Example with Byte Swapping

Using byte-swapping on the below 64-bit data set, the hexadecimal reading of 93 40 3d 4a a3 70 0a d7 will be swapped to 40 93 4a 3d 70 a3 d7 0a. As the reading has been byte-swapped, it will still produce the Float64 value, 123.456.

Register Value
40001 93 40
40002 3d 4a
40003 a3 70
40004 0a d7

Word Swapping

If enabled, word-swapping carries out a swap operation on two words of data - each set of two words in a data reading will swap places. As Modbus registers are one word in length, word-swapping will swap the data values read from two registers. For example, a reading with a value of 4a 3d 40 93 will become 40 93 4a 3d. Here, we can see that the second word 40 93 has been placed before 4a 3d in the reading.

Note

Word-swapping operates on two words at a time. If there are more than two words, it will change the order of two words first, before moving on to the next set of two words.

To enable word-swapping, set the isWordSwap attribute to true, as shown in the following device profile extract:

attributes:
  { primaryTable: "INPUT_REGISTERS", startingAddress: "4", isByteSwap: "false", isWordSwap: "true" }

32-bit Example with Word Swapping

Using word-swapping on the below 32-bit data set, the hexadecimal reading of 85 1f 41 45 will be swapped to 41 45 85 1f. As the reading has been word-swapped, it will still produce the Float32 value of 12.345.

Register Value
40001 85 1f
40002 41 45

64-bit Example with Word Swapping

Using word-swapping on the below 64-bit data set, the hexadecimal reading of 4a 3d 40 93 d7 0a 70 a3 will be swapped to 40 93 4a 3d 70 a3 d7 0a. As the reading has been word-swapped, it will still produce the Float64 value, 123.456.

Register Value
40001 4a 3d
40002 40 93
40003 d7 0a
40004 70 a3

Combined Byte and Word Swapping

If both are enabled, byte-swapping will occur before word-swapping. As such, the bytes within a word will be swapped before the words. For example, a reading with the value 1f 85 45 41 will become 41 45 85 1f. Here, we can see that the individual bytes of 1f 85 and 45 41 have swapped first, then the words themselves have swapped.

To enable combined byte- and word-swapping, set both the isByteSwap and isWordSwap attributes to true, as shown in the following device profile extract:

attributes:
  { primaryTable: "INPUT_REGISTERS", startingAddress: "4", isByteSwap: "true", isWordSwap: "true" }

32-bit Example with Combined Byte and Word Swapping

Using byte- and word-swapping on the below 32-bit data set, the hexadecimal reading of 1f 85 45 41 will be swapped to 41 45 85 1f. As the reading has been byte- and word-swapped, it will still produce the Float32 value of 12.345.

Register Value
40001 1f 85
40002 45 41

64-bit Example with Combined Byte and Word Swapping

Using combined byte- and word-swapping on the below 64-bit data set, the hexadecimal reading of 3d 4a 93 40 0a d7 a3 70 will be swapped to 40 93 4a 3d 70 a3 d7 0a. As the reading has been byte- and word-swapped, it will still produce the Float64 value, 123.456.

Register Value
40001 3d 4a
40002 93 40
40003 0a d7
40004 a3 70