CNAOB-Communications-Protocol-Notes: Difference between revisions
Jump to navigation
Jump to search
Embeddedrf (talk | contribs) No edit summary |
No edit summary |
||
| (15 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
== | == Comm Port Binary Mode == | ||
Is necessary to use binary mode. Serial port in ASCII mode chokes on null = chr(0) chars | |||
*[http://www.codenewsgroups.net/group/microsoft.public.vb.general.discussion/topic6374.aspx Serial port return handling of binary data into array] | |||
*[http://www.di-mgt.com.au/bytearrays.html extensive binary byte array in lieu of string functions handling page] | |||
16-bit CRC verification code | == Links for CRC CheckSum Algorithm == | ||
*[http://iatips.com/crc_vb.txt CRC calculation source code in VB using array lookup] - [[ModBus-CRC-Local-Version-routine-v1]] | |||
*[http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6329&lngWId=10 CRC calculation source code in VB using all calculation] - [[ModBus-CRC-Local-Version-routine-2]] | |||
== 16-bit CRC verification code Algorithm == | |||
Only 8 data bits are involved in CRC calculation, with the exclusion of start bit and end bit. | Only 8 data bits are involved in CRC calculation, with the exclusion of start bit and end bit. | ||
Algorithm of CRC code: | Algorithm of CRC code: | ||
*1) Presetting a 16-bit register to hex FFFF | *1) Presetting a 16-bit register to hex FFFF. The register is called CRC register | ||
*2) XORing | *2) XORing the first byte of the communication message frame with the low 8-bit of 16-bit CRC register.....then storing the result in CRC register | ||
then storing the result in CRC register | |||
*3) Right-shifting the register data by one bit | *3) Right-shifting the register data by one bit and filling the highest bit with 0, then checking the shift-out bit | ||
*4) If the shift-out bit is 0, repeat step 3 (right-shifting one more bit); | *4) If the shift-out bit is 0, repeat step 3 (right-shifting one more bit); | ||
If the shift-out bit | If the shift-out bit is 1, XOR the CRC register data with polynomial A001 (1010 0000 0000 0001) = A001 | ||
*5) Repeating step 3 and step 4 until all of the 8-bit data have been processed after 8 right-shift operations; | *5) Repeating step 3 and step 4 until all of the 8-bit data have been processed after 8 right-shift operations; | ||
| Line 31: | Line 37: | ||
*7) When calculation procedures of the first 5 bytes in the communication message frame are completed, the 16-bit CRC verification | *7) When calculation procedures of the first 5 bytes in the communication message frame are completed, the 16-bit CRC verification | ||
code will be generated in the 16-bit CRC register. | code will be generated in the 16-bit CRC register. | ||
== CRC Example Calculations: 1, 06, 40, 03,E8, 18, 22 Hex == | |||
*1111 1111 1111 1111 CRC | |||
*0000 0000 0000 0001 Data 1 | |||
*----------------------------- XOR | |||
*1111 1111 1111 1110 CRC | |||
*0111 1111 1111 1111 CRC 32767 after right shift / shifted bit = 0 | |||
*0011 1111 1111 1111 CRC after right shift / shifted bit = 1 | |||
*1010 0000 0000 0001 polynomial = A001 | |||
*----------------------------- XOR | |||
*1001111111111110 CRC | |||
keep doing till 8 shifts accomplished | |||
Latest revision as of 14:25, 30 July 2008
Comm Port Binary Mode[edit]
Is necessary to use binary mode. Serial port in ASCII mode chokes on null = chr(0) chars
- Serial port return handling of binary data into array
- extensive binary byte array in lieu of string functions handling page
Links for CRC CheckSum Algorithm[edit]
- CRC calculation source code in VB using array lookup - ModBus-CRC-Local-Version-routine-v1
- CRC calculation source code in VB using all calculation - ModBus-CRC-Local-Version-routine-2
16-bit CRC verification code Algorithm[edit]
Only 8 data bits are involved in CRC calculation, with the exclusion of start bit and end bit. Algorithm of CRC code:
- 1) Presetting a 16-bit register to hex FFFF. The register is called CRC register
- 2) XORing the first byte of the communication message frame with the low 8-bit of 16-bit CRC register.....then storing the result in CRC register
- 3) Right-shifting the register data by one bit and filling the highest bit with 0, then checking the shift-out bit
- 4) If the shift-out bit is 0, repeat step 3 (right-shifting one more bit);
If the shift-out bit is 1, XOR the CRC register data with polynomial A001 (1010 0000 0000 0001) = A001
- 5) Repeating step 3 and step 4 until all of the 8-bit data have been processed after 8 right-shift operations;
- 6) Repeating step 2 to step 5 to process the next byte of the communication message frame;
- 7) When calculation procedures of the first 5 bytes in the communication message frame are completed, the 16-bit CRC verification
code will be generated in the 16-bit CRC register.
CRC Example Calculations: 1, 06, 40, 03,E8, 18, 22 Hex[edit]
- 1111 1111 1111 1111 CRC
- 0000 0000 0000 0001 Data 1
- ----------------------------- XOR
- 1111 1111 1111 1110 CRC
- 0111 1111 1111 1111 CRC 32767 after right shift / shifted bit = 0
- 0011 1111 1111 1111 CRC after right shift / shifted bit = 1
- 1010 0000 0000 0001 polynomial = A001
- ----------------------------- XOR
- 1001111111111110 CRC
keep doing till 8 shifts accomplished