📄 crc_1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "CRC校验"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "FORM1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text8
Height = 285
Left = 3480
TabIndex = 13
Top = 1200
Width = 615
End
Begin VB.TextBox Text7
Height = 285
Left = 2520
TabIndex = 12
Top = 1200
Width = 615
End
Begin VB.TextBox Text6
Height = 285
Left = 1560
TabIndex = 11
Top = 1200
Width = 735
End
Begin VB.TextBox Text5
Height = 285
Left = 3480
TabIndex = 10
Top = 360
Width = 615
End
Begin VB.TextBox Text4
Height = 285
Left = 2520
TabIndex = 9
Top = 360
Width = 615
End
Begin VB.TextBox Text3
Height = 285
Left = 1560
TabIndex = 6
Top = 360
Width = 735
End
Begin VB.TextBox Text2
Height = 495
Left = 2280
TabIndex = 4
Top = 2400
Width = 1815
End
Begin VB.TextBox Text1
Height = 495
Left = 2280
TabIndex = 1
Top = 1680
Width = 1815
End
Begin VB.CommandButton Command1
Caption = "开始计算"
Height = 1215
Left = 120
TabIndex = 0
Top = 1680
Width = 615
End
Begin VB.Label Label9
Caption = "数据6"
Height = 255
Left = 3480
TabIndex = 17
Top = 840
Width = 615
End
Begin VB.Label Label8
Caption = "数据5"
Height = 255
Left = 2520
TabIndex = 16
Top = 840
Width = 615
End
Begin VB.Label Label7
Caption = "数据4"
Height = 255
Left = 1560
TabIndex = 15
Top = 840
Width = 615
End
Begin VB.Label Label6
Caption = "数据3"
Height = 255
Left = 3480
TabIndex = 14
Top = 0
Width = 615
End
Begin VB.Label Label5
Caption = "数据2"
Height = 255
Left = 2520
TabIndex = 8
Top = 0
Width = 615
End
Begin VB.Label Label4
Caption = "数据1"
Height = 255
Left = 1560
TabIndex = 7
Top = 0
Width = 615
End
Begin VB.Label Label3
Caption = "顺序输入16进制数据"
Height = 495
Left = 240
TabIndex = 5
Top = 360
Width = 1095
End
Begin VB.Label Label2
Caption = "高字节"
Height = 375
Left = 960
TabIndex = 3
Top = 2400
Width = 1095
End
Begin VB.Label Label1
Caption = "低字节"
Height = 375
Left = 960
TabIndex = 2
Top = 1800
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
On Error GoTo err
If Text3.Text = "" Or Text4.Text = "" Or Text5.Text = "" Or Text6.Text = "" Or Text7.Text = "" Then 'Or Text8.Text = "" Then
MsgBox "您漏填了某个数据,请从新输入"
Exit Sub
Else
Dim CRC() As Byte
Dim d() As Byte '待传输数据
ReDim d(5) As Byte
d(0) = t16_t10(Text3.Text)
d(1) = t16_t10(Text4.Text)
d(2) = t16_t10(Text5.Text)
d(3) = t16_t10(Text6.Text)
d(4) = t16_t10(Text7.Text)
d(5) = t16_t10(Text8.Text)
CRC = CRC16(d) '调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
Text1.Text = Hex$(CRC(0))
Text2.Text = Hex$(CRC(1))
Exit Sub
err:
MsgBox "数据长度超长,请从新输入"
End If
End Sub
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
Function t16_t10(temp As String) As Integer
Dim cun() As String '存储提取出的字符串的动态数组
Dim i As Integer '循环变量
Dim sum As Integer '返回值
ReDim cun(0 To Len(temp)) '定义动态数组
For i = 1 To Len(temp)
cun(i) = Mid$(temp, i, 1) '提取字符存入数组
Select Case cun(i)
Case "0" '当字符为0时
sum = sum + 0 * 16 ^ (Len(temp) - i)
Case "1" '当字符为1时
sum = sum + 1 * 16 ^ (Len(temp) - i)
Case "2" '当字符为2时
sum = sum + 2 * 16 ^ (Len(temp) - i)
Case "3" '当字符为3时
sum = sum + 3 * 16 ^ (Len(temp) - i)
Case "4" '当字符为4时
sum = sum + 4 * 16 ^ (Len(temp) - i)
Case "5" '当字符为5时
sum = sum + 5 * 16 ^ (Len(temp) - i)
Case "6" '当字符为6时
sum = sum + 6 * 16 ^ (Len(temp) - i)
Case "7" '当字符为7时
sum = sum + 7 * 16 ^ (Len(temp) - i)
Case "8" '当字符为8时
sum = sum + 8 * 16 ^ (Len(temp) - i)
Case "9" '当字符为9时
sum = sum + 9 * 16 ^ (Len(temp) - i)
Case "a" '当字符为10时
sum = sum + 10 * 16 ^ (Len(temp) - i)
Case "b" '当字符为11时
sum = sum + 11 * 16 ^ (Len(temp) - i)
Case "c" '当字符为12时
sum = sum + 12 * 16 ^ (Len(temp) - i)
Case "d" '当字符为13时
sum = sum + 13 * 16 ^ (Len(temp) - i)
Case "e" '当字符为14时
sum = sum + 14 * 16 ^ (Len(temp) - i)
Case "f" '当字符为15时
sum = sum + 15 * 16 ^ (Len(temp) - i)
Case Else
MsgBox "您输入的的进制不对,请从新输入!"
End Select
Next
t16_t10 = sum
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -