理解这个VB代码

时间:2013-08-20 02:42:50

标签: vb6

有人可以尝试围绕这个吗?我认为这只是确保在十六进制字节中有2个字节并确保值介于0 -9和A-F之间但不是。

用于红外控制器/冲击波的程序片段。该子程序将实际信号(或其他代码)从串口发送到控制器,以便完成工作。

示例电话:

SendCode ("04241001")

VB6代码说:

Public Sub SendCode(ByVal strOut As String)
' ****************************
' This sub sends the hex codes
' ****************************


Dim numb1 As Integer, numb2 As Integer
Dim strRS As String
Dim i As Long
Dim newline(200) As String, outline(200) As String

Debug.Print "Sending IR - " & strOut

    strRS = vbNullString

    For i = 1 To Len(strOut)
        newline(i) = Mid(strOut, i, 1)
    Next

    For i = 1 To Len(strOut) Step 2
        If Asc(newline(i)) < 64 Then
            numb1 = (Asc(newline(i)) - 48) * 16
            strRS = strRS + Format(Hex(numb1 / 16), "0")
        Else
            numb1 = (Asc(newline(i)) - 55) * 16
            strRS = strRS + Format(Hex(numb1 / 16), "0")
        End If
        If Asc(newline(i + 1)) < 64 Then
            numb2 = (Asc(newline(i + 1)) - 48)
            strRS = strRS + Format(Hex(numb2), "0")
        Else
            numb2 = (Asc(newline(i + 1)) - 55)
            strRS = strRS + Format(Hex(numb2), "0")
        End If
        numb1 = numb1 + numb2
        outline((i + 1) \ 2) = CByte(numb1)
        strRS = strRS + " "
    Next

    With MSComm1
        .RTSEnable = True
        Sleep (20)
        .OutBufferCount = 0
        For i = 1 To (Len(strOut) / 2)
            .Output = Chr(outline(i))
        Next
        Sleep (20)
        .RTSEnable = False
    End With

End Sub

问题是基于第二个For / Next循环和第2步以及嵌入式IF语句。循环内部发生了什么? numb1和numb2

这个循环的目的是什么?

1 个答案:

答案 0 :(得分:1)

它将十六进制字符串转换为二进制字节字符串,然后发送二进制字节字符串。 它还将二进制字节转换回十六进制(strRS),以便您可以检查转换和输出。检查/调试字符串不用于任何内容,但是如果你在那里放置一个断点,你可以检查这些值。