Eddiie
Eddiie

Reputation: 15

understanding this VB code

Can someone try wrap their brain around this one? I thought it was simply ensuring there were 2 bytes in hex byte and ensuring the values were between 0 -9 and A-F but no.

A snippet of a program that is for an infrared controller/blaster. This subroutine will send the actual signals (or other codes) out the serial port to the controller for it to finish the job.

Sample call:

SendCode ("04241001")

The VB6 code says:

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

The question is based around the second For/Next loop with Step 2 and the embedded IF statements. What is going on inside the loop? numb1 and numb2

What is the purpose of this loop?

Upvotes: 0

Views: 195

Answers (1)

david
david

Reputation: 2638

It converts a hex string into a binary byte string, then sends the binary byte string. It also converts the binary bytes back into hex (strRS) so that you can check the conversion and the output. The check/debug string is not used for anything, but if you put a break point in there you can check the values.

Upvotes: 1

Related Questions