<< Click to Display Table of Contents >>
BCD Encoding |
BCD Encoding, or Binary-Coded Decimal, was originally created to circumvent limitations regarding the maximum number of digits that can be represented in traditional formats for storing values. Formats such as the representation of real numbers in floating point are usually acceptable for math and scientific calculations. However, rounding errors caused by numbers that cannot be represented due to overflow or underflow problems may not be acceptable in certain applications, such as financial procedures. To overcome that limitation, a BCD encoding was developed to allow representing numbers up to the last digit.
In this representation, each decimal digit is represented only in a binary format, without limitations regarding its number of digits.
The next table shows decimal digits and their corresponding values in BCD.
Decimal digits in BCD encoding
Decimal |
BCD |
Decimal |
BCD |
---|---|---|---|
0 |
0000b |
5 |
0101b |
1 |
0001b |
6 |
0110b |
2 |
0010b |
7 |
0111b |
3 |
0011b |
8 |
1000b |
4 |
0100b |
9 |
1001b |
To improve efficiency of this encoding, it is common to represent two digits per byte. Notice that, on the previous table, each decimal digit requires only 4 (four) bits, or a half byte, for its representation.
Such representation with 2 (two) digits in each byte is called Packed BCD, and this the representation used by this Driver, that is, packets sent by this Driver with BCD values use a data byte for every 2 (two) digits of the represented decimal value. That is why the Size field, for BCD data types, must be defined as half the maximum number of digits represented in read or written values.
As an example, suppose that users want to send the value 84 in decimal, 0x54 in hexadecimal format, using a packed BCD encoding in one byte, the format used by this Driver.
The first step is separate the 2 (two) decimal digits that compose this value in its decimal representation:
•Digit 1: 8 (eight)
•Digit 2: 4 (four)
If users want to send this value to a device without a BCD encoding, then the value sent to the protocol is 84, which is represented in hexadecimal format by 0x54, or else 01010100b in binary format.
By using a packed BCD format, however, users represent these 2 (two) decimal digits separately in each half, or nibble, of the byte to send:
•BCD: 0x84 or 10000100b
Notice that, if this value 0x84 is mistakenly interpreted in BCD format as a value in hexadecimal format without this encoding, and this value is then converted to decimal, users get the value 132, which is meaningless.
The next table presents a few more examples of decimal values between 0 (zero) and 99 and their respective representations in Packed BCD format in 1 (one) byte, presented in hexadecimal and binary formats.
Decimal digits in Packed BCD encoding
Decimal |
Hexadecimal |
BCD (Hexadecimal) |
BCD (Binary) |
---|---|---|---|
10 |
0x0A |
0x10 |
00010000b |
0 |
0x00 |
0x00 |
00000000b |
99 |
0x63 |
0x99 |
10011001b |
81 |
0x51 |
0x81 |
10000001b |
45 |
0x2D |
0x45 |
01000101b |
1) Reading of value 11223344 in BCD format
Note that the integer 11223344 contains 8 (eight) digits, so in the properties dialog of this Driver, users must create a new Operation, defining the Data field as type BCD and the Size field with size 4 (four), in other words, the 8 (eight) digits of the BCD value must be sized to bytes, grouping the digits always in pairs, 11 22 33 44, to know how many bytes are required to represent it.
Reading a value with Packed BCD encoding
device memory |
tag value |
Notes |
---|---|---|
Register 1 = 0x1122 and Register 2 = 0x3344 |
11223344 |
The value 11223344 requires 4 (four) bytes to be represented in BCD format, and occupy 2 (two) Modbus registers in the device's memory |