Issue
How to decode individual on/off states from a Modbus analogue register (e.g., Holding or Input registers) using bit masking. Each bit represents the status of a coil and needs to be read separately.
Product Line
Satchwell Sigma Software
Environment
Sigma IC Modbus and IC3 Modbus controllers
Cause
Modbus registers are 16-bit values. Each bit (0 or 1) represents the on/off state of a coil. To interpret these states, bit masking is used to isolate and evaluate each bit.
Resolution
Each Modbus register contains 16 bits. For example, a value of 901 in Sigma translates to the binary 0000001110000101, where:
1= ON0= OFF
To decode each bit:
- Create a Sigma analogue input object to read the Modbus value.
- Create a programmable object for each bit (up to 16 per register).
Example Code Snippets
Assuming POINT 0|222 is the Modbus value object, here’s how to decode each bit:
Bit 1 (LSB)
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 1 )
30 IF YINT = 1 THEN RETURN TRUE "Bit 1 = on"
40 RETURN FALSE "Bit 1 = off"
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 2 )
30 IF YINT = 2 THEN RETURN TRUE "Bit 2 = on"
40 RETURN FALSE "Bit 2 = off"
``Repeat the pattern above, updating the mask value as follows:
| Bit | Mask Value |
|---|---|
| 3 | 4 |
| 4 | 8 |
| 5 | 16 |
| 6 | 32 |
| 7 | 64 |
| 8 | 128 |
| 9 | 256 |
| 10 | 512 |
| 11 | 1024 |
| 12 | 2048 |
| 13 | 4096 |
| 14 | 8192 |
| 15 | 16384 |
| 16 (MSB) | 32768 |