|
<< 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 encoding.
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 usual to represent 2 (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 a 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 each 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 when reading or writing 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 1 (one) byte, the format used by this Driver. The first step is separate the 2 (two) decimal digits that compose this value in their 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 itself, 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 |
•Reading of value 11223344 in BCD format: Notice that the integer 11223344 contains 8 (eight) digits, therefore on the properties window of this Driver, users must create a new Operation, defining the Data field with a BCD data type and the Size field with size 4 (four), that is, the 8 (eight) digits of the BCD value must be sized to bytes, grouping the digits always in pairs, 11 22 33 44, to define how many bytes are required to represent this value
Reading a value with packed BCD encoding
Memory of a Device |
Value of a Tag |
Notes |
|---|---|---|
Register 1 = 0x1122 and Register 2 = 0x3344 |
11223344 |
The value 11223344 requires 4 (four) bytes to be represented in BCD format, and occupies 2 (two) Modbus registers in the memory of a device |