Board logo

标题: CRC???é?÷??ר?ù-1 VB????±à??CRC???é???ò [打印本页]

作者: zona    时间: 2004-5-21 16:36     标题: CRC???é?÷??ר?ù-1 VB????±à??CRC???é???ò

zona??×??ü?è????CRC???é?????????ò??CRC????·?±????????é?????????????????????±?ò??????×??????????é·????÷?????í??????????·?????????

VB????±à??CRC???é???ò  

??×??????ú??????????·??????????ú?¤?????????????ú?????????¨?????¤??????????????????????·????????????à????????×????????à???????????ò???°?ì???????ú???????è±????????¨?????????á·??ú??·¨?¤?????í?ó??????·????í?ó?ù???????°?ì????°????¨???±???????????é??°ì·¨???????·???à?????é??×??????????é·?·¨??????



???? ???·???à?????é???í


???????·???à?????é??????????Cyclical Redundancy Check???ò??CRC???ü????????·¨?°?à???????í??×÷?í?ó?ì???¨Error Detecting???????????????±??·???×°????????CRC??????????????·?????????×°????????×°??????????????????????CRC??????????CRC?à±???????????CRC?????????ò???÷?????¨???????í?ó??

?ù???????·?????°??????????CRC????·???????????±ê×???

??????CRC-12????

??????CRC-16????

??????CRC-CCITT????

??????CRC-32????

????CRC-12???¨??????????6-bit×?·?????CRC-16?°CRC-CCITT???ò??????????8-bit×?·???????CRC-16?????ú????????CRC-CCITT???·???ú???ù??????CRC-32???ó??±???????????????Point-to-Point??????????????

??????×???????CRC-16?????????÷???ú????????

????CRC-16????????×????????????????±CRC?????÷???????????¤????1?????ó°?CRC?????÷??8-bit???????????ì?ò?????ó??CRC?????÷????????????????????×??????¨MSB????????????????×??????¨LSB???????ó????±?????CRC?????÷????????1???ò°??????÷???¤?¨?????à???????????ì?ò??·??ò????LSB???????ò???è?????ì?ò??????????????????????????8??????????8-bit???????í?ê±????????±CRC?????÷????????????8-bit?????ì?ò?????????°????????????8?????????ù????×?·????í?ê???óCRC?????÷??????????×?????CRC????

??????????CRC????????????

????1???è??CRC?????÷????????????FFFF(hex)??

????2????????????????8-bit×?·???16??CRC?????÷????8???????ì?ò????°??á??????CRC?????÷??

????3??CRC?????÷?ò??????????MSB?????????????ì?éLSB??

????4??????LSB??0????????????????LSB??1??CRC?????÷???à???????à?ì?ò??

????5????????3????4???±??8???????????ê???????±????8-bit???????í?ê±???

????6????????2????5???±???ù?????????????í?ê????

????7??×???CRC?????÷??????????CRC????

???? ???·???à?????é???ò??±à??


?????÷°×??CRC???é?????ú?ú??????±à???????ò????·??????×????????Visual Basic????·????°???°?????????¨????????????????????????VB??????±à??CRC???ú?????ò?????ü???????è??×???????????

????±à??CRC???é???ò??????°ì·¨????????????·¨?????????é±í·¨????????????·?·¨·?±???????


????1??????·¨

????????·¨????????CRC???é?????ú?ú???í???è?????ò?????????????é?ú???????????é???????????????????±???????????ó??????±????í???????????¨???????????????à????????A001(hex)??

???????°????·??????ü??°???Command1?????í???????ú????


????Private Sub Command1_Click()

??????Dim CRC() As Byte

??????Dim d() As Byte ????????????

??????ReDim d(2) As Byte

??????d(0) = 123

??????d(1) = 112

??????d(2) = 135

??????CRC = CRC16(d) ???÷??CRC16????????

????????CRC(0)??????

????????CRC(1)??????

????End Sub

????×????????????????±CRC?????????????°???????????ó??


????Function CRC16(data() As Byte) As String

??????Dim CRC16Lo As Byte, CRC16Hi As Byte????????CRC?????÷

??????Dim CL As Byte, CH As Byte???????????????????à??????&HA001

??????Dim SaveHi As Byte, SaveLo As Byte

??????Dim i As Integer

??????Dim Flag As Integer

??????CRC16Lo = &HFF

??????CRC16Hi = &HFF

??????CL = &H1

??????CH = &HA0

??????For i = 0 To UBound(data)

????????CRC16Lo = CRC16Lo Xor data(i) ??????????????CRC?????÷?????ì?ò

????????For Flag = 0 To 7

??????????SaveHi = CRC16Hi

??????????SaveLo = CRC16Lo

??????????CRC16Hi = CRC16Hi  2??????????????????????????

??????????CRC16Lo = CRC16Lo  2??????????????????????????

??????????If ((SaveHi And &H1) = &H1) Then ??????????×???×??ó??????1

????????????CRC16Lo = CRC16Lo Or &H80?????????ò????×????????ó?°????1

??????????End If?????????????????????????? ??·??ò×?????0

??????????If ((SaveLo And &H1) = &H1) Then ??????LSB??1???ò???à???????????ì?ò

????????????CRC16Hi = CRC16Hi Xor CH

????????????CRC16Lo = CRC16Lo Xor CL

??????????End If

????????Next Flag

??????Next i

??????Dim ReturnData(1) As Byte

??????ReturnData(0) = CRC16Hi????????????????CRC????

??????ReturnData(1) = CRC16Lo????????????????CRC????

??????CRC16 = ReturnData

????End Function

????2???é±í·¨

?????é±í·¨?????±????????·¨???????à·???????±???±??????????ù???????¨??????·¨???ê???à???????????°????·???????Command1??°????????ú????·????????????ê???????????????é??CRC??????±à?????ú????


????Private Function CRC16(data() As Byte) As String

??????Dim CRC16Hi As Byte

??????Dim CRC16Lo As Byte

??????CRC16Hi = &HFF

??????CRC16Lo = &HFF

??????Dim i As Integer

??????Dim iIndex As Long

??????For i = 0 To UBound(data)

????????iIndex = CRC16Lo Xor data(i)

????????CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex)?????????????????í

????????CRC16Hi = GetCRCHi(iIndex)?????????????????????????????í

??????Next i

??????Dim ReturnData(1) As Byte

??????ReturnData(0) = CRC16Hi??????????CRC????

??????ReturnData(1) = CRC16Lo??????????CRC????

??????CRC16 = ReturnData

????End Function


??????CRC????×?????±í

????Function GetCRCLo(Ind As Long) As Byte

??????GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _

&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40)

????End Function


??????CRC????×?????±í

????Function GetCRCHi(Ind As Long) As Byte

??????GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB, &H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _

&H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83, &H41, &H81, &H80, &H40)

????End Function


???????????ò??Win98??VB6???÷???¨????

[???ù±?zona??2004-05-21 5:27 PM????±à??]
[???ù±?zona??2004-05-21 10:56 PM????±à??]




欢迎光临 我要委托设计网论坛 (http://51odm.com/fourms/) Powered by Discuz! 5.5.0