收藏 分享(赏)

CRC 计算.doc

上传人:j35w19 文档编号:6622119 上传时间:2019-04-18 格式:DOC 页数:5 大小:35.50KB
下载 相关 举报
CRC 计算.doc_第1页
第1页 / 共5页
CRC 计算.doc_第2页
第2页 / 共5页
CRC 计算.doc_第3页
第3页 / 共5页
CRC 计算.doc_第4页
第4页 / 共5页
CRC 计算.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、Private Function CRC16(data() As Byte) As StringDim CRC16Lo As Byte, CRC16Hi As Byte CRC 寄存器Dim CL As Byte, CH As Byte 多项式码&HA001Dim CRCLo As String, CRCHi As StringDim SaveHi As Byte, SaveLo As ByteDim i As IntegerDim Flag As IntegerCRC16Lo = &HFFCRC16Hi = &HFFCL = &H1CH = &HA0For i = 0 To UBound(d

2、ata)CRC16Lo = CRC16Lo Xor data(i) 每一个数据与 CRC 寄存器进行异或For Flag = 0 To 7SaveHi = CRC16HiSaveLo = CRC16LoCRC16Hi = CRC16Hi 2 高位右移一位CRC16Lo = CRC16Lo 2 低位右移一位If (SaveHi And &H1) = &H1) Then 如果高位字节最后一位为 1CRC16Lo = CRC16Lo Or &H80 否则低位字节右移后前面补 1End If 否则自动补 0If (SaveLo And &H1) = &H1) Then 如果 LSB 为 1,则与多项式

3、码进行异或CRC16Hi = CRC16Hi Xor CHCRC16Lo = CRC16Lo Xor CLEnd IfNext FlagNext iIf Len(Hex(CRC16Hi) = 1 ThenCRCHi = “0“ + Hex(CRC16Hi)ElseCRCHi = Hex(CRC16Hi)End IfIf Len(Hex(CRC16Lo) = 1 ThenCRCLo = “0“ + Hex(CRC16Lo)ElseCRCLo = Hex(CRC16Lo)End IfCRC16 = CRCLo + CRCHiEnd Function 0| 评论2009-6-12 11:19vlao

4、da | 十级Private Sub Command1_Click()Dim 数据() As Byte Dim 校验码 As IntegerDim 字节数 As LongDim 低位 As Boolean校验码 = -1数据 = StrConv(Text1, vbFromUnicode)字节数 = UBound(数据)For i = 0 To 字节数低位 = tureFor j = 1 To 8If 低位 Then 校验码 = 校验码 Xor 数据(i)低位 = 数据(i) And 1数据(i) = 数据(i) 2Next jNext iText2 = Hex(校验码)End Sub算法大概就

5、是这样了方法 2form1-form8,8 个窗体,一个模块form6Private Sub Command1_Click()Dim CRC As ByteDim s1 As StringDim lens1 As IntegerDim d() As Byte 待传输数据Dim sd() As String 显示的字符Text2.Text = “s1 = Replace(Text1.Text, “ “, “)lens1 = Len(s1)If lens1 Mod 2 0 Or lens1 47 And sd(i) 64 And sd(i) 96 And sd(i) 103) ThenElseMs

6、gBox “数据输入有误!“, 64, “信息提示! “Text1.SetFocusExit SubEnd IfNext iReDim d(lens1 2) As ByteFor i = 0 To (lens1 2) - 1 Step 2d(i) = “&H“ & Mid(s1, i * 2 + 1, 2)Next iCRC = d(3)For i = 4 To (lens1 2 - 1)CRC = CRC Xor d(i)Next id(lens1 2) = CRCReDim sd(lens1 2) As StringFor i = 0 To (lens1 2)If Val(d(i) 16

7、Thensd(i) = “0“ & Hex(d(i)Elsesd(i) = Hex(d(i)End IfIf Text2.Text = “ ThenText2.Text = sd(i)ElseText2.Text = Text2.Text & “ “ & sd(i)End IfNext iLabel5.Caption = “ “ & lens1 2Label7.Caption = “ “ & lens1 2 + 1End SubPrivate Sub Command2_Click()Text1.Text = “End SubPrivate Sub Command3_Click()Label5.

8、Caption = “ “ & 0Label7.Caption = “ “ & 0End SubPrivate Sub Command7_Click()Unload MeForm1.ShowEnd SubPrivate Sub Command8_Click()EndEnd SubPrivate Sub Form_Load()Text1.Text = “Text2.Text = “Label5.Caption = “ “ & 0Label7.Caption = “ “ & 0Text3.Text = “AA BB 05 FA 00 00 0C 52 A4“ & Chr(13) + Chr(10)

9、 & _“AA BB 07 F8 00 01 0C 00 04 00 F1“ & Chr(13) + Chr(10) & _“AA BB 05 FA 00 01 0C EC 1B“Text4.Text = “AA BB 04 FB 00 00 0D F6“ & Chr(13) + Chr(10) & _“AA BB 09 F6 00 01 0D 00 XX XX XX XX CRC“Text5.Text = “AA BB 08 F7 00 00 0E XX XX XX XX CRC“ & Chr(13) + Chr(10) & _“AA BB 06 F9 00 01 0E 00 08 FE“T

10、ext6.Text = “AA BB 0C F3 00 00 12 60 01 29 03 29 03 29 03 AA“ & Chr(13) + Chr(10) & _“AA BB 0C F3 00 00 12 60 01 00 21 64 36 62 17 86“ & Chr(13) + Chr(10) & _“AA BB 05 FA 00 01 12 E7 0E“ & Chr(13) + Chr(10) & _“AA BB 05 FA 00 01 12 00 E9“Text7.Text = “AA BB 05 FA 00 00 16 01 ED“ & Chr(13) + Chr(10) & _“AA BB 09 F6 00 01 16 00 XX XX XX XX CRC“Text8.Text = “AA BB 09 F6 00 00 17 01 XX XX XX XX CRC“ & Chr(13) + Chr(10) & _“AA BB 05 FA 00 01 17 00 EC“End Sub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报