BCD Encoding

Driver Modicon Modbus

BCD Encoding

Top  Previous  Next

BCD Encoding (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 (values in binary).

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 four bits, or a half byte, for its representation.

Such representation with 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 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 values that are read or written.

 

Example

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 two decimal digits that compose this value in its decimal representation:

·Digit 1: 8

·Digit 2: 4

 

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 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 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

 
Has this section of the documentation helped you configure this Driver?
Yes No
Comments (optional):