注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
a26409310的个人空间 https://home.eeworld.com.cn/space-uid-263238.html [收藏] [复制] [分享] [RSS]
日志

vb计算crc form5

已有 263 次阅读2010-4-12 18:27

'form1--form8,8个窗体,一个模块

’form5

Private Sub Command1_Click()

Dim CRC() As Byte

Dim s1 As String

Dim lens1 As Integer

Dim d() As Byte '待传输数据

Dim sd() As String '显示的字符

Text2.Text = ""

s1 = Replace(Text1.Text, " ", "")

lens1 = Len(s1)

If lens1 Mod 2 <> 0 Or s1 = ""

Then

MsgBox

"数据长度有误!", 64, "信息提示!! "

Text1.SetFocus

Exit

Sub

End If

ReDim sd(lens1)

For i = 0 To lens1 - 1

sd(i) =

Asc(Mid(s1, i + 1, 1))

If (sd(i)

> 47 And sd(i) < 58) Or (sd(i)

> 64 And sd(i) < 71) Or (sd(i)

> 96 And sd(i) < 103) Then

Else

MsgBox "数据输入有误!", 64, "信息提示!! "

Text1.SetFocus

Exit Sub

End If

Next i

ReDim d(lens1 \ 2 + 1) As Byte

For i = 0 To (lens1 \ 2) - 1 ' Step 2

d(i) =

"&H" & Mid(s1, i * 2 + 1, 2)

Next i

CRC = modbusCRC16(d) '调用CRC16计算函数

d(lens1 \ 2) =

CRC(1)

'CRC(0)为高位

d((lens1 \ 2) + 1) = CRC(0) 'CRC(1)为低位

ReDim sd(lens1 \ 2 + 1) As String

For i = 0 To (lens1 \ 2) + 1

If Val(d(i))

< 16 Then

sd(i) = "0" & Hex(d(i))

Else

sd(i) = Hex(d(i))

End If

If

Text2.Text = "" Then

Text2.Text = sd(i)

Else

Text2.Text = Text2.Text & " " &

sd(i)

End If

Next i

Label5.Caption = " " & lens1 \ 2

Label7.Caption = " " & lens1 \ 2 + 2

End Sub

Private Sub Command2_Click()

Text1.Text = ""

Text1.SetFocus

End Sub

Private Sub Command3_Click()

Label5.Caption = " " & 0

Label7.Caption = " " & 0

End Sub

Private Sub Command4_Click()

Dim CRC() As Byte

Dim s1 As String

Dim lens1 As Integer

Dim d() As Byte '待传输数据

Dim sd() As String '显示的字符

Text4.Text = ""

s1 = Replace(Text3.Text, " ", "") '去掉所有空格

lens1 = Len(s1)

If lens1 Mod 2 <> 0 Or s1 = ""

Then

MsgBox

"数据长度有误!", 64, "信息提示!! "

Text3.SetFocus

Exit

Sub

End If

ReDim sd(lens1)

For i = 0 To lens1 - 1

sd(i) =

Asc(Mid(s1, i + 1, 1)) '判断是否为十六进制字符

If (sd(i)

> 47 And sd(i) < 58) Or (sd(i)

> 64 And sd(i) < 71) Or (sd(i)

> 96 And sd(i) < 103) Then

Else

MsgBox "数据输入有误!", 64, "信息提示!! "

Text3.SetFocus

Exit Sub

End If

Next i

ReDim d(lens1 \ 2) As Byte

d(lens1 \ 2) = 0

For i = 0 To (lens1 \ 2) - 1 ' Step 2

d(i) =

"&H" & Mid(s1, i * 2 + 1, 2)

'd(lens1 \

2) = Val(d(lens1 \ 2)) + Val(d(i)) And &HFF

Next i

d(lens1 \ 2) = sum(d)

ReDim sd(lens1 \ 2) As String

For i = 0 To lens1 \ 2

If Val(d(i))

< 16 Then

sd(i) = "0" & Hex(d(i))

Else

sd(i) = Hex(d(i))

End If

If

Text4.Text = "" Then

Text4.Text = sd(i)

Else

Text4.Text = Text4.Text & " " &

sd(i)

End If

Next i

Label11.Caption = " " & lens1 \ 2

Label13.Caption = " " & lens1 \ 2 + 1

End Sub

Private Sub Command5_Click()

Text3.Text = ""

Text3.SetFocus

End Sub

Private Sub Command6_Click()

Label11.Caption = " " & 0

Label13.Caption = " " & 0

End Sub

Private Sub Command7_Click()

Unload Me

Form1.Show

End Sub

Private Sub Command8_Click()

End

End Sub

Private Sub Form_Load()

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

Label5.Caption = " " & 0

Label7.Caption = " " & 0

Label11.Caption = " " & 0

Label13.Caption = " " & 0

End Sub

分享

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章