
In the event of no response or incorrect response (mismatched control codes), the master makes some attempts
to interrogate the same slave. The master can easily discriminate the type of response from the length, in fact:
no data implies a positive response, a length equal to 1 identifies an error, otherwise the data relating to the
query are reported.
CRC-16 CALCULATION
The CRC (Cyclic Redundancy Check) is made up of two bytes and is calculated by the transmitting device (both
MASTER and SLAVE) which postpones it to the message. The receiving device recalculates the CRC when
receiving the message and compares this value with the one received. If the two values are different, an error
is reported.
The calculation of the CRC is started by preloading a 16-bit register with all bits at 1. Then the calculation of
the CRC proceeds by inserting the bytes of the message to the current contents of the register. Only the 8 least
significant bits in each character are used for CRC generation. The start (STX) and stop (ETX) characters do not
apply for the CRC calculation.
During the CRC generation, an exclusive OR (XOR) logic operation is performed between each 8-bit character
and the contents of the register.
Then the result is translated (shift operation) in the direction of the least significant bit (LSB) with an insertion
of zero (zero filling) in the position of the most significant bit (MSB). The bit extracted in this way is then
examined. If it is 1 then an XOR operation is performed between the contents of the register and a
predetermined value which depends on the generator polynomial used. If it is 0, no XOR operation is performed.
The process is repeated until 8 translations of the register contents are made. Therefore a new character can be
inserted in the register with an XOR operation between the character itself and the present content of the
register.
The final content of the register, after all the characters of the message have been entered, is the CRC value.
The procedure for generating the CRC-16, used in the protocol, is summarized as follows:
1. Initialize a 16-bit register with the value 0xFFFF. This register is called the CRC register.
2. Execute the XOR operation in the CRC register between the first 8-bits of the message and the least
significant byte of the register itself.
3. Left shift the CRC register by one bit, filling the most significant bit with zero.
4. If the extracted bit is zero, repeat point 3. If the extracted bit is one, execute the XOR operation between
the contents of the register and the value 0xA001 which corresponds to the use of a generator polynomial
x
16
+ x
15
+ x
2
+ 1.
5. Repeat steps 3. and 4. until 8 translations have been performed to complete the CRC of the current byte
of the message.
6. Repeat steps 2. to 5. for all subsequent bytes of the message.
7. The final content of the register is the CRC of the message.