Detect the Byte Order
hard
Works out which byte order a device uses from a value it is known to report.
What it teaches
There is no standard for how a 32-bit value is split across Modbus registers, so commissioning often means trying each order against a value you already know, such as a reference pressure. Try the value as it arrives, then SWPB with REVERSE, WORD and HIGH/LOW, and report the first that matches. Combines REAL From Two Registers and REAL in Network Byte Order.
Parameters
| Tag | Direction | Type | Default | Description |
|---|---|---|---|---|
| In_Raw | Input | DINT | 1342194813 | 32 bits as received from the device |
| In_Expected | Input | REAL | 1013.25 | The value the device is known to be reporting |
| Out_Order | Output | DINT | 0 | 0 as received, 1 REVERSE, 2 WORD, 3 HIGH/LOW, -1 none |
| Out_Value | Output | REAL | 0 | In_Raw read in the order found, or 0.0 |
Required behavior
- While commissioning, the device reports a value you already know, such as a reference pressure. Work out which of the four byte orders turns In_Raw into In_Expected.
- Every scan, try the orders in this sequence and report the first that gives In_Expected exactly: 0 = In_Raw read as it is; 1 = its four bytes reversed (SWPB REVERSE); 2 = its two 16-bit halves swapped (WORD); 3 = the two bytes swapped within each half (HIGH/LOW).
- Out_Value is In_Raw read as a REAL in the order found. When no order gives In_Expected, Out_Order is -1 and Out_Value is 0.0.
- 1013.25 is 16#447D5000. It arrives as 16#447D5000 in order 0, 16#00507D44 in order 1, 16#5000447D in order 2 and 16#7D440050 in order 3.