米客-显示驱动专家

VB.NET 16进制发送

0
阅读(2706)

 Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click ' 直接发送文本框的十六进制字符串

        Try

            '    Dim Send_Flag As Boolean

            Dim A As String

            Dim C As String

            Dim B() As String

            C = TextBox1.Text

            A = "00," & C

            B = A.Split(",")

            Dim i As Integer

            For i = 0 To B.Length - 1

                Send_Hex(B(i))

            Next

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub


  Private Sub Send_Hex(hexString As String)  '16进制字符串转16进制数据

        '*********************** 文本string 转 hex   ********************

        ' SerialPort.Write(TextBox1.Text)

        Dim TestArray() As String = Split(hexString)

        Dim hexBytes() As Byte

        ReDim hexBytes(TestArray.Length - 1)

        Dim i As Integer

        For i = 0 To TestArray.Length - 1

            hexBytes(i) = Val("&h" & TestArray(i))

        Next

        SerialPort.Write(hexBytes, 0, hexBytes.Length)


        '******************     End     *******************************

    End Sub