FieldServer Gray Code (Reflected Binary Code) Explained
Gray Code, also known as Reflected Binary Code, is a non-weighted digital code in which only a single bit changes between consecutive values. This characteristic makes Gray Code especially reliable in industrial automation systems where signal integrity is critical.
Unlike standard binary counting, Gray Code prevents multiple bits from changing simultaneously. This significantly reduces the risk of transient errors caused by timing delays, electrical noise, or mechanical switching.
Gray Code is not intended for arithmetic operations. Instead, it is commonly used in analog-to-digital converters, rotary encoders, selector switches, and industrial input/output devices.
Learn more about how FieldServer devices fit into larger building automation systems by visiting our BACnet integration gateways and routers
Binary vs Gray Code Example
| Decimal | Binary | Gray Code |
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Using Gray Code in FieldServer Gateways
FieldServer gateways can convert binary input values into Gray Code and expose those values over BACnet networks. This is particularly useful when integrating rotary switches, selector knobs, or legacy panels into modern building automation systems.
In the example configuration, binary inputs are read from two BACnet/IP devices. The individual bits are packed into numeric values and then converted into Gray Code. The converted values are served as BACnet Analog Inputs over BACnet MSTP using Data Arrays.
Data_Array_Name , Data_Format , Data_Array_Length
DA_BitsA , Bit , 100
DA_BitsB , Bit , 100
DA_Float , Float , 100
//================================================================================
// Server Side Connections
Adapters
Adapter , Protocol
N1 , Bacnet_IP
//================================================================================
// Server Side Nodes
Nodes
Node_Name , Node_ID , Protocol ,Node_Option ,Adapter
SouceNodeA , 37306 , Bacnet_IP ,COV_disable ,N1
SouceNodeB , 37307 , Bacnet_IP ,COV_disable ,N1
//================================================================================
// Server Side Map Descriptors
Map_Descriptors
Data_Type , Object_ID , Map_Descriptor_Name , Units , Data_Array_Name , Data_Array_Offset , Node_Name , Property , Function
BI , 0 , Bit 0 , No_Units , DA_BitsA , 0 , SouceNodeA , Present_Value , rdbc
BI , 1 , Bit 1 , No_Units , DA_BitsA , 1 , SouceNodeA , Present_Value , rdbc
BI , 2 , Bit 2 , No_Units , DA_BitsA , 2 , SouceNodeA , Present_Value , rdbc
BI , 3 , Bit 3 , No_Units , DA_BitsA , 3 , SouceNodeA , Present_Value , rdbc
BI , 4 , Bit 4 , No_Units , DA_BitsA , 4 , SouceNodeA , Present_Value , rdbc
BI , 5 , Bit 5 , No_Units , DA_BitsA , 5 , SouceNodeA , Present_Value , rdbc
BI , 6 , Bit 6 , No_Units , DA_BitsA , 6 , SouceNodeA , Present_Value , rdbc
BI , 7 , Bit 7 , No_Units , DA_BitsA , 7 , SouceNodeA , Present_Value , rdbc
Map_Descriptors
Data_Type , Object_ID , Map_Descriptor_Name , Units , Data_Array_Name , Data_Array_Offset , Node_Name , Property , Function
BI , 0 , Bit 0 , No_Units , DA_BitsB , 0 , SouceNodeB , Present_Value , rdbc
BI , 1 , Bit 1 , No_Units , DA_BitsB , 1 , SouceNodeB , Present_Value , rdbc
BI , 2 , Bit 2 , No_Units , DA_BitsB , 2 , SouceNodeB , Present_Value , rdbc
BI , 3 , Bit 3 , No_Units , DA_BitsB , 3 , SouceNodeB , Present_Value , rdbc
BI , 4 , Bit 4 , No_Units , DA_BitsB , 4 , SouceNodeB , Present_Value , rdbc
BI , 5 , Bit 5 , No_Units , DA_BitsB , 5 , SouceNodeB , Present_Value , rdbc
BI , 6 , Bit 6 , No_Units , DA_BitsB , 6 , SouceNodeB , Present_Value , rdbc
BI , 7 , Bit 7 , No_Units , DA_BitsB , 7 , SouceNodeB , Present_Value , rdbc
Moves
Source_Data_Array , Source_offset , Target_Data_Array , Target_Offset , Length ,Function
DA_BitsA , 0 , DA_Float , 0 , 8 ,Bit_pack
DA_BitsB , 0 , DA_Float , 1 , 8 ,Bit_pack
//================================================================================
// COnnection
adapters,
Adapter, Protocol ,
N1 , CAS_TOOLN1 ,
//================================================================================
// Client Side Nodes
Nodes,
Node_Name, Node , Protocol ,Adapter
CASUTIL , 1 , CAS_TOOLN1 ,N1
//================================================================================
// Client Side Map Descriptors
Map_Descriptors
Map_Descriptor_Name ,Scan_Interval ,Data_Array_Name ,Data_Array_Offset ,CAS_TOOLS_Array_Name ,CAS_TOOLS_Array_Offset ,Function ,Node_Name ,CAS_TOOLS_Function_Name ,Length ,Address
toGray ,1.0s ,DA_FLOAT ,0 ,DA_FLOAT ,10 ,wrbc ,CASUTIL ,TO_GRAYCODE ,1 ,00
toGray ,1.0s ,DA_FLOAT ,1 ,DA_FLOAT ,11 ,wrbc ,CASUTIL ,TO_GRAYCODE ,1 ,00
//fromGray ,1.0s ,DA_GRAY ,0 ,DA_BIN ,1 ,wrbc ,CASUTIL ,FROM_GRAYCODE ,1 ,00
//================================================================================
// Server Side Connections
connections
Port, Baud , Parity, Data_Bits, Stop_Bits, Protocol , Timeout, Connection_Type ,
R2 , 38400, None , 8 , 1 , Bacnet_MSTP, 30 , MSTP_Master_Node,
//===============================================================================
// Server Side Nodes
Nodes
Node_Name , Node_ID , Protocol , Node_Option
ServerNode , 31 , Bacnet_MSTP , COV_Disable
Map_Descriptors
Data_Type , Object_ID , Map_Descriptor_Name , Units , Data_Array_Name , Data_Array_Offset , Node_Name , Property , Function , Data_Array_Low_Scale , Data_Array_High_Scale , Node_Low_Scale , Node_High_Scale
AI , 1 , AnalogValue_converted_A , No_Units , DA_Float , 0 , ServerNode , Present_Value , Server , 0 , 100 , 0 , 100
AI , 2 , AnalogValue_converted_B , No_Units , DA_Float , 1 , ServerNode , Present_Value , Server , 0 , 100 , 0 , 100
Configuration Overview
- Read binary inputs from two BACnet/IP source devices
- Store binary values in FieldServer Data Arrays
- Pack binary bits into numeric values
- Convert numeric values to Gray Code using CAS tools
- Expose converted values as BACnet Analog Inputs over MSTP
Gray Code Conversion Logic (C++ Example)
unsigned int bin2Gray(unsigned int num)
{
return (num >> 1) ^ num;
}
unsigned int gray2Bin(unsigned int num)
{
unsigned int mask;
for (mask = num >> 1; mask != 0; mask >>= 1)
{
num ^= mask;
}
return num;
}
Online Gray Code Converter