Issue
How to use bit masking to decode on/off states from a Modbus analogue registers e.g. Holding or Input registers. Where each bit is required to be read individually to indicate the status of a coil.
Product Line
Satchwell Sigma Software
Environment
- Sigma IC Modbus and IC3 Modbus controllers
Cause
Configuration technique to read 16-bit Modbus registers for on/off states.
Resolution
Each modbus holding or input register is 16 bits, where each bit can be on or off i.e. 0 or 1.
For example the Sigma analogue input value of 901 represents the state of a number of coils where each bit indicates the on or off. Hence, this translates to the 16-bit binary value 0000001110000101 where 1 = on and 0 = off
- Create a Sigma analogue input object to read the Modbus value. Click here for the Sigma Modbus 4.06 release Note
- Create a programmable object for each of the required bits (max of 16 for each register) to be read as below.
(Highlight, copy and paste into each programmable object).
For Example starting with the Least Significant Bit (LSB) and where point 0|222 is the Modbus value created, the programmable objects are;
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"
Bit 2
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"
Bit 3
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 4 )
30 IF YINT = 4 THEN RETURN TRUE "Bit 3 = on"
40 RETURN FALSE "Bit 3 = off"
Bit 4
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 8 )
30 IF YINT = 8 THEN RETURN TRUE "Bit 4 = on"
40 RETURN FALSE "Bit 4 = off"
Bit 5
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 16 )
30 IF YINT = 16 THEN RETURN TRUE "Bit 5 = on"
40 RETURN FALSE "Bit 5 = off"
Bit 6
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 32 )
30 IF YINT = 32 THEN RETURN TRUE "Bit 6 = on"
40 RETURN FALSE "Bit 6 = off"
Bit 7
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 64 )
30 IF YINT = 64 THEN RETURN TRUE "Bit 7 = on"
40 RETURN FALSE "Bit 7 = off"
Bit 8
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 128 )
30 IF YINT = 128 THEN RETURN TRUE "Bit 8 = on"
40 RETURN FALSE "Bit 8 = off"
Bit 9
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 256 )
30 IF YINT = 256 THEN RETURN TRUE "Bit 9 = on"
40 RETURN FALSE "Bit 9 = off"
Bit 10
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 512 )
30 IF YINT = 512 THEN RETURN TRUE "Bit 10 = on"
40 RETURN FALSE "Bit 10 = off"
Bit 11
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 1024 )
30 IF YINT = 1024 THEN RETURN TRUE "Bit 5 = on"
40 RETURN FALSE "Bit 6 = off"
Bit 12
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 2048 )
30 IF YINT = 2048 THEN RETURN TRUE "Bit 12 = on"
40 RETURN FALSE "Bit 12 = off"
Bit 13
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 4096 )
30 IF YINT = 4096 THEN RETURN TRUE "Bit 13 = on"
40 RETURN FALSE "Bit 13 = off"
Bit 14
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 8292 )
30 IF YINT = 8292 THEN RETURN TRUE "Bit 14 = on"
40 RETURN FALSE "Bit 14 = off"
Bit 15
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 16384 )
30 IF YINT = 16384 THEN RETURN TRUE "Bit 15 = on"
40 RETURN FALSE "Bit 15 = off"
Bit 16 (MSB)
10 XINT = INT POINT 0|222
20 YINT = XINT MASK ( 32768 )
30 IF YINT = 32768 THEN RETURN TRUE "Bit 16 = on"
40 RETURN FALSE "Bit 16 = off"