FieldServer Gray Code – Reflected Binary Code, and how is it used
Gray Code is one of the most important codes. It is a non-weighted code which belongs to a class of codes called minimum change codes. While traversing from one step to another step, only one bit in the code group changes in these types of codes. In the case of Gray Code, two adjacent code numbers differ from each other by only one bit. You will probably understand it better by looking at the table given below. This code is not applicable in any type of arithmetical operations, but it has some applications in analog to digital converters and in some input/output devices.
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 Codes in FieldServer Gateways
In the configuration below:
Reads 6 Binary inputs from each of 2 BACnet devices, converts to a Binary (Decimal) Number, and then converts those numbers to Gray Codes, which are served using MSTP
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
Sample c++ code
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 = mask >> 1)
{
num = num ^ mask;
}
return num;
}
Online Gray Code Converter