计算CRC-16循环冗余校验码的程序开发
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
4ǃྲ᷌Ѫ1ˈࡉCRCᇴᆈಘоཊ亩ᔿA001ᔲᡆ˗ྲ᷌Ѫ0ˈࡉ䟽༽ㅜ3↕Ⲵࣘ
5ǃ䟽༽3઼4ⴤࡠᆼᡀҶ8⅑〫սDŽ䘉ṧᆼᮤⲴ8սᆇ㢲ሶᆼᡀ༴⨶ҶDŽ
6ǃሩҾлањ8սᆇ㢲Ⲵ༴⨶ቡᱟ䟽༽ㅜ2↕ࡠㅜ5↕Ҷ
7ǃᢺᡰᴹⲴⅢਁ䘱ᡆ᧕᭦⎸䘉ṧ༴⨶ਾˈCRCᇴᆈಘ䟼Ⲵ٬ቡᱟᡁԜᴰ㓸䴰㾱ᗇࡠⲴCRCṑ傼⸱DŽ
ⴻݸањֻᆀ:
OutByte(1) = Val("&H" & txtOutByte(1).Text)
OutByte(2) = Val("&H" & txtOutByte(2).Text)
OutByte(3) = Val("&H" & txtOutByte(3).Text)
OutByte(4) = Val("&H" & txtOutByte(4).Text)
7ǃThe final content of the CRC register is the CRC value.
ѝ᮷䀓䟺ྲл˖
1ǃᇊѹањࡍ٬ѪFFFFⲴ16սⲴਈ䟿ˈ䈕ਈ䟿〠ѪCRCᇴᆈಘ.
2ǃᢺⅢਁ䘱ᡆ᧕᭦⎸Ⲵ儈8ս઼CRCᇴᆈಘⲴᓅ8սᔲᡆ䘀㇇ˈᒦᢺ㔃᷌൘䍻ࡠCRCᇴᆈಘDŽ
3ǃCRCᇴᆈಘਣ〫1ս˄ᵍᴰվս˅ˈ਼ᰦᴰ儈ս䴦DŽਆࠪᒦỰḕᴰվսᱟѪ1DŽ
Next i
'(6)Repeat step 2 through 5 for the next 8-bit byte of them message.
OutByte(6) = CRC16LO
'(7)The final contents of the CRC register is the CRC value
3ǃShift the CRC register one bit to the right (toward the LSB), zero-filling the MSB. Extract and Examine.
4ǃIf the LSB was 1: Exclusive OR the CRC register with polynomial value A001 hex. If the LSB was 0: Repeat step 3 (another shift).
䇑㇇00HⲴṑ傼⸱:
䴰㾱Ր䘱(᧕ਇ)Ⲵᮠᦞ0000 0000(Ҽ䘋ࡦ) ᇴᆈಘ1111 1111 1111 1111 ਆՐ䘱ᮠᦞⲴ儈ޛսоᇴᆈಘⲴվޛսᔲᡆ
0000 0000 1111 1111 1111 1111 ᆈޕᇴᆈಘ ᇴᆈಘ1111 1111 1111 1111 аǃᇴᆈಘਣ〫аս
For j = 0 To 7
TempHI = CRC16HI
TempLO = CRC16LO
CRC16HI = CRC16HI \ 2 '(3)Shift the CRC register one bit to the right(toward the LSB),zero-filling CRC16LO = CRC16LO \ 2 'the MSB.Extract and Examine
ঊᇒ
ḕⴻ᮷ㄐ 䇑㇇
ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁ
䇑㇇CRC-16ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁ
Ǐᇩ᪈㾱ǐCRC-16ᱟỰ⍻ᮠᦞ൘ਁ䘱䗷〻ѝਁ⭏䭉䈟Ⲵᑨ⭘ṑ傼ᯩ⌅ˈᵜ᮷䙊䗷Ӿᐕ〻ᓄ⭘Ⲵ䀂ᓖˈ䇢䘠ྲօᇎ⧠CRC-16Ⲵ〻ᒿᔰਁˈᒦ㔉ࠪҶ Visual Basic઼Visual C++〻ᒿԓ⸱ˈ㔉Ӿһᐕъ᧗ࡦⲴӪઈ൘߉䙊ؑ〻ᒿⲴᰦىᨀаӋᴹԧ٬Ⲵ৲㘳DŽ
If ((TempHI And &H1) = &H1) Then CRC16LO = CRC16LO Or &H80 '〫սᰦˈྲ᷌儈վսᱟ1ˈࡉվ儈ս࣐1
End If
If ((TempLO And &H1) = &H1) Then '(4)if the LSB was 1:
CRC16HI = CRC16HI Xor &HA0 'Exclusive OR the CRC register with polynomial value A001 hex.
5ǃRepeat step 3 and 4 until 8 shifts have been performed. When this is done, a complete 8-bit byte will have been processed.
6ǃRepeat step 2 through 5 for the next 8-bit byte of them message.
CRC16LO = CRC16LO Xor OutByte (i) '(2)Exclusive OR the first 8-bit byte of the message
'with the low-order byte of the 16-bit CRC,
'putting the result in the CRC register
OutByte(5) = Val("&H" & txtOutByte(5).Text)
CRC16LO = &HFF
'(1)Load a 16-bit register with FFFF hex.call this the CRC register.
CRC16HI = &HFF
For i = 0 To UBound (OutByte) - 2
ᴰ䘁䈫㘵˖
㖁৻䇴䇪˖
ਁ㺘䇴䇪˖
{
UpdateData(TRUE);
CString strTemp;
strTemp=m_strCRC;
WORD CRC; BYTE btData[4096]; int nLength=StrToHex(strTemp,btData);
CRC=ANSI_CRC_16(btData,nLength); //ANSI-16 CString str; str.Format(" %02X %02X",(BYTE)(CRC>>8),(BYTE)CRC); AfxMessageBox(str);
〻ᒿ⭼䶒
〻ᒿԓ⸱˖
Private Sub cmdGenerate_Click()
Dim OutByte(7) As Byte, CRC16() As Byte, CRC16LO As Byte, CRC16HI As Byte, TempHI As Byte, TempLO As Byte
Dim i As Integer, j As Integer
ҼǃCRC-16䇑㇇↕僔
1ǃLoad a 16-bit register with FFFF hex. Call this the CRC register.
2ǃExclusive OR the first 8-bit byte of the message with the low-order byte of the 16-bit CRC, putting the result in the CRC register.
CRC^=btData[i]; for (j=0;j<8;j++) {
Tmp=CRC&0x0001; CRC=CRC>>1; if(Tmp)CRC=(CRC^0xA001); } } return ((CRC>>8)+(CRC<<8)); /* ᓄ⭘ᰦ儈൘*ݸ/ } int CCRC_16Dlg::StrToHex(CString str, BYTE *btData) { int nLength=str.GetLength(); if(nLength<2) return 0; for(int i=0;i<nLength;i+=3) _stscanf(str.Mid(i,2),"%02X",&btData[i/3]); return (i+1)/3; }
CDialog::OnOK(); } WORD CCRC_16Dlg::ANSI_CRC_16(BYTE* btData,int nLength) //ANSI CRC-16,x16 + x15 + x2 + 1 {
CHECKVALUE(btData,nLength); WORD CRC=0xFFFF; BYTE j,Tmp=0; int i; for(i=0;i<nLength;i++) {
аǃࡽ䀰 CRCⲴ䇑㇇ᯩᔿᱟሶⅢՐ䗃Ⲵᮠᦞඇ㿶Ѫаึ䘎㔝սᡰᶴᡀⲴᮤњᮠ٬ˈሶ↔ᮠ٬䲔ԕањ⢩ᇊⲴ䲔ᮠˈ䙊ᑨԕҼ䘋ࡦ㺘皮ˈ↔䲔ᮠ〠Ѫ⭏ᡀཊ亩ᔿ ˄ANSI CRC-16Ⲵ⭏ᡀཊ亩ᔿѪ˖x16 + x15 + x2 + 1˅DŽⴞࡽ䖳ᑨ⭘ⲴCRCսᮠⴞᴹ8઼16ԕ৺32ˈ൘䘉䟼ਚ䇢䘠ྲօ߉CRCսᮠⴞѪ16Ⲵ〻ᒿDŽCRC սᮠⴞ䎺བྷˈᮠᦞⲴ䭉䈟Ự⍻⦷ࡉ䎺儈ˈնᗵ享ཊ㣡аӋᰦ䰤䘋㹼ᮠᦞⲴ䇑㇇DŽ
txtOutByte(6) = Hex(OutByte(6))
OutByte(7) = CRC1源自文库HI
txtOutByte(7) = Hex(OutByte(7))
End Sub
ഋǃVisual C++〻ᒿᇎ⧠
〻ᒿ⭼䶒
ѫ㾱ԓ⸱˖
#define CHECKVALUE(pt,nl) if((btData==NULL)||(nLength<=0)){AfxMessageBox("ᰐ᭸ᮠ㓴ʽ");return -1;}
ᇴᆈಘ0111 1111 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0111 1111 1111 1111 1010 0000 0000 0001 1101 1111 1111 1110 Ҽǃᇴᆈಘਣ〫аս 0110 1111 1111 1111 〫ࠪⲴᱟ0 йǃᇴᆈಘਣ〫аս 0011 0111 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0011 0111 1111 1111 1010 0000 0000 0001 1001 0111 1111 1110 ഋǃᇴᆈಘਣ〫аս 0100 1011 1111 1111 〫ࠪⲴᱟ0 ӄǃᇴᆈಘਣ〫аս 0010 0101 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0010 0101 1111 1111 1010 0000 0000 0001 1000 0101 1111 1110 ޝǃᇴᆈಘਣ〫аս 0100 0010 1111 1111 〫ࠪⲴᱟ0 гǃᇴᆈಘਣ〫аս 0010 0001 0111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0010 0001 0111 1111 1010 0000 0000 0001 1000 0001 0111 1110 ޛǃᇴᆈಘਣ〫аս 0100 0000 1011 1111 ᡰԕˈ00H˄Ҽ䘋ࡦⲴ0000 0000˅Ⲵṑ傼⸱Ѫ40BF
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OutByte(0) = Val("&H" & txtOutByte(0).Text)
BOOL CCRC_16Dlg::OnInitDialog()
{
ĂĂ
m_strCRC="01 04 00 5C 00 04";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCRC_16Dlg::OnOK()
CRC16LO = CRC16LO Xor &H1
End If
'if the LSB was 0: Repeat step 3 (another shift)
Next j
'(5)Repeat step 3 and 4 until 8 shifts have been performed.
'when this is done, a complete 8-bit byte will have been processed.
5ǃ䟽༽3઼4ⴤࡠᆼᡀҶ8⅑〫սDŽ䘉ṧᆼᮤⲴ8սᆇ㢲ሶᆼᡀ༴⨶ҶDŽ
6ǃሩҾлањ8սᆇ㢲Ⲵ༴⨶ቡᱟ䟽༽ㅜ2↕ࡠㅜ5↕Ҷ
7ǃᢺᡰᴹⲴⅢਁ䘱ᡆ᧕᭦⎸䘉ṧ༴⨶ਾˈCRCᇴᆈಘ䟼Ⲵ٬ቡᱟᡁԜᴰ㓸䴰㾱ᗇࡠⲴCRCṑ傼⸱DŽ
ⴻݸањֻᆀ:
OutByte(1) = Val("&H" & txtOutByte(1).Text)
OutByte(2) = Val("&H" & txtOutByte(2).Text)
OutByte(3) = Val("&H" & txtOutByte(3).Text)
OutByte(4) = Val("&H" & txtOutByte(4).Text)
7ǃThe final content of the CRC register is the CRC value.
ѝ᮷䀓䟺ྲл˖
1ǃᇊѹањࡍ٬ѪFFFFⲴ16սⲴਈ䟿ˈ䈕ਈ䟿〠ѪCRCᇴᆈಘ.
2ǃᢺⅢਁ䘱ᡆ᧕᭦⎸Ⲵ儈8ս઼CRCᇴᆈಘⲴᓅ8սᔲᡆ䘀㇇ˈᒦᢺ㔃᷌൘䍻ࡠCRCᇴᆈಘDŽ
3ǃCRCᇴᆈಘਣ〫1ս˄ᵍᴰվս˅ˈ਼ᰦᴰ儈ս䴦DŽਆࠪᒦỰḕᴰվսᱟѪ1DŽ
Next i
'(6)Repeat step 2 through 5 for the next 8-bit byte of them message.
OutByte(6) = CRC16LO
'(7)The final contents of the CRC register is the CRC value
3ǃShift the CRC register one bit to the right (toward the LSB), zero-filling the MSB. Extract and Examine.
4ǃIf the LSB was 1: Exclusive OR the CRC register with polynomial value A001 hex. If the LSB was 0: Repeat step 3 (another shift).
䇑㇇00HⲴṑ傼⸱:
䴰㾱Ր䘱(᧕ਇ)Ⲵᮠᦞ0000 0000(Ҽ䘋ࡦ) ᇴᆈಘ1111 1111 1111 1111 ਆՐ䘱ᮠᦞⲴ儈ޛսоᇴᆈಘⲴվޛսᔲᡆ
0000 0000 1111 1111 1111 1111 ᆈޕᇴᆈಘ ᇴᆈಘ1111 1111 1111 1111 аǃᇴᆈಘਣ〫аս
For j = 0 To 7
TempHI = CRC16HI
TempLO = CRC16LO
CRC16HI = CRC16HI \ 2 '(3)Shift the CRC register one bit to the right(toward the LSB),zero-filling CRC16LO = CRC16LO \ 2 'the MSB.Extract and Examine
ঊᇒ
ḕⴻ᮷ㄐ 䇑㇇
ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁ
䇑㇇CRC-16ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁ
Ǐᇩ᪈㾱ǐCRC-16ᱟỰ⍻ᮠᦞ൘ਁ䘱䗷〻ѝਁ⭏䭉䈟Ⲵᑨ⭘ṑ傼ᯩ⌅ˈᵜ᮷䙊䗷Ӿᐕ〻ᓄ⭘Ⲵ䀂ᓖˈ䇢䘠ྲօᇎ⧠CRC-16Ⲵ〻ᒿᔰਁˈᒦ㔉ࠪҶ Visual Basic઼Visual C++〻ᒿԓ⸱ˈ㔉Ӿһᐕъ᧗ࡦⲴӪઈ൘߉䙊ؑ〻ᒿⲴᰦىᨀаӋᴹԧ٬Ⲵ৲㘳DŽ
If ((TempHI And &H1) = &H1) Then CRC16LO = CRC16LO Or &H80 '〫սᰦˈྲ᷌儈վսᱟ1ˈࡉվ儈ս࣐1
End If
If ((TempLO And &H1) = &H1) Then '(4)if the LSB was 1:
CRC16HI = CRC16HI Xor &HA0 'Exclusive OR the CRC register with polynomial value A001 hex.
5ǃRepeat step 3 and 4 until 8 shifts have been performed. When this is done, a complete 8-bit byte will have been processed.
6ǃRepeat step 2 through 5 for the next 8-bit byte of them message.
CRC16LO = CRC16LO Xor OutByte (i) '(2)Exclusive OR the first 8-bit byte of the message
'with the low-order byte of the 16-bit CRC,
'putting the result in the CRC register
OutByte(5) = Val("&H" & txtOutByte(5).Text)
CRC16LO = &HFF
'(1)Load a 16-bit register with FFFF hex.call this the CRC register.
CRC16HI = &HFF
For i = 0 To UBound (OutByte) - 2
ᴰ䘁䈫㘵˖
㖁৻䇴䇪˖
ਁ㺘䇴䇪˖
{
UpdateData(TRUE);
CString strTemp;
strTemp=m_strCRC;
WORD CRC; BYTE btData[4096]; int nLength=StrToHex(strTemp,btData);
CRC=ANSI_CRC_16(btData,nLength); //ANSI-16 CString str; str.Format(" %02X %02X",(BYTE)(CRC>>8),(BYTE)CRC); AfxMessageBox(str);
〻ᒿ⭼䶒
〻ᒿԓ⸱˖
Private Sub cmdGenerate_Click()
Dim OutByte(7) As Byte, CRC16() As Byte, CRC16LO As Byte, CRC16HI As Byte, TempHI As Byte, TempLO As Byte
Dim i As Integer, j As Integer
ҼǃCRC-16䇑㇇↕僔
1ǃLoad a 16-bit register with FFFF hex. Call this the CRC register.
2ǃExclusive OR the first 8-bit byte of the message with the low-order byte of the 16-bit CRC, putting the result in the CRC register.
CRC^=btData[i]; for (j=0;j<8;j++) {
Tmp=CRC&0x0001; CRC=CRC>>1; if(Tmp)CRC=(CRC^0xA001); } } return ((CRC>>8)+(CRC<<8)); /* ᓄ⭘ᰦ儈൘*ݸ/ } int CCRC_16Dlg::StrToHex(CString str, BYTE *btData) { int nLength=str.GetLength(); if(nLength<2) return 0; for(int i=0;i<nLength;i+=3) _stscanf(str.Mid(i,2),"%02X",&btData[i/3]); return (i+1)/3; }
CDialog::OnOK(); } WORD CCRC_16Dlg::ANSI_CRC_16(BYTE* btData,int nLength) //ANSI CRC-16,x16 + x15 + x2 + 1 {
CHECKVALUE(btData,nLength); WORD CRC=0xFFFF; BYTE j,Tmp=0; int i; for(i=0;i<nLength;i++) {
аǃࡽ䀰 CRCⲴ䇑㇇ᯩᔿᱟሶⅢՐ䗃Ⲵᮠᦞඇ㿶Ѫаึ䘎㔝սᡰᶴᡀⲴᮤњᮠ٬ˈሶ↔ᮠ٬䲔ԕањ⢩ᇊⲴ䲔ᮠˈ䙊ᑨԕҼ䘋ࡦ㺘皮ˈ↔䲔ᮠ〠Ѫ⭏ᡀཊ亩ᔿ ˄ANSI CRC-16Ⲵ⭏ᡀཊ亩ᔿѪ˖x16 + x15 + x2 + 1˅DŽⴞࡽ䖳ᑨ⭘ⲴCRCսᮠⴞᴹ8઼16ԕ৺32ˈ൘䘉䟼ਚ䇢䘠ྲօ߉CRCսᮠⴞѪ16Ⲵ〻ᒿDŽCRC սᮠⴞ䎺བྷˈᮠᦞⲴ䭉䈟Ự⍻⦷ࡉ䎺儈ˈնᗵ享ཊ㣡аӋᰦ䰤䘋㹼ᮠᦞⲴ䇑㇇DŽ
txtOutByte(6) = Hex(OutByte(6))
OutByte(7) = CRC1源自文库HI
txtOutByte(7) = Hex(OutByte(7))
End Sub
ഋǃVisual C++〻ᒿᇎ⧠
〻ᒿ⭼䶒
ѫ㾱ԓ⸱˖
#define CHECKVALUE(pt,nl) if((btData==NULL)||(nLength<=0)){AfxMessageBox("ᰐ᭸ᮠ㓴ʽ");return -1;}
ᇴᆈಘ0111 1111 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0111 1111 1111 1111 1010 0000 0000 0001 1101 1111 1111 1110 Ҽǃᇴᆈಘਣ〫аս 0110 1111 1111 1111 〫ࠪⲴᱟ0 йǃᇴᆈಘਣ〫аս 0011 0111 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0011 0111 1111 1111 1010 0000 0000 0001 1001 0111 1111 1110 ഋǃᇴᆈಘਣ〫аս 0100 1011 1111 1111 〫ࠪⲴᱟ0 ӄǃᇴᆈಘਣ〫аս 0010 0101 1111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0010 0101 1111 1111 1010 0000 0000 0001 1000 0101 1111 1110 ޝǃᇴᆈಘਣ〫аս 0100 0010 1111 1111 〫ࠪⲴᱟ0 гǃᇴᆈಘਣ〫аս 0010 0001 0111 1111 〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010 0000 0000 0001˅ᔲᡆ 0010 0001 0111 1111 1010 0000 0000 0001 1000 0001 0111 1110 ޛǃᇴᆈಘਣ〫аս 0100 0000 1011 1111 ᡰԕˈ00H˄Ҽ䘋ࡦⲴ0000 0000˅Ⲵṑ傼⸱Ѫ40BF
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OutByte(0) = Val("&H" & txtOutByte(0).Text)
BOOL CCRC_16Dlg::OnInitDialog()
{
ĂĂ
m_strCRC="01 04 00 5C 00 04";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCRC_16Dlg::OnOK()
CRC16LO = CRC16LO Xor &H1
End If
'if the LSB was 0: Repeat step 3 (another shift)
Next j
'(5)Repeat step 3 and 4 until 8 shifts have been performed.
'when this is done, a complete 8-bit byte will have been processed.