Byte stuffing

To transmit non-character data when using a character orientated data transmission protocol some sort of coding has to be used to avoid confusing the receiver.
This called data transparency and is achieved by using a method called byte stuffing. The character DLE is inserted into the message.

Suppose we are transmitting the following characters and binary digits:
Start and end of message and block characters have been omitted for clarity.

DATA1001110

This would be sent as ASCII as follows:

 1000100  1000001   1010100  1010100  0010000  1001110  0010000
 D  A  T  A  DLE  1001110  DLE

Suppose 0000100 inadvertently turned up in the binary data? The binary data transmission would be prematurely ended and the rest of the transmission would be corrupted.

 0010000  1000110  0000100  0010000  1000100  0010000
 DLE  1000110  0000100  !(DLE)  1000100  DLE

One way of tackling this is to stuff another DLE into the binary stream.

 0000100  1000110  0000100  0010000  0010000  1000100  0000100
 DLE  1000110  0000100  DLE  !(DLE)  1000100  DLE

The receiving device will read the first DLE and then the second. Once this occurs the receiving machine will ignore the first DLE and treat the second one as binary data.

Bit stuffing

A similar problem occurs when bit orientated data transmission protocols are used. Bit orientated protocols look for patterns of data for control purposes.
Suppose that a frame delimiter pattern is 01111110 when we are sending some data. Here the flag is inadvertently repeated in the middle of the frame.

 0111110  00100100  11100010  10110011  11110101  00010101  00100101   01111110
     

  flag

 occurs      

To overcome this problem the transmitting device stuffs a '0' bit into the data stream after each sequence of five binary '1's.

 0111110  00100100  11100010  10110011  111010101  10001010  10010010  10111111 0 
                 

The receiver does the opposite, everytime it recognises a sequence of five '1's there will be a '0' that follows. This is removed and the original data stream is reconstructed.