从串口上的数字开始的十六进制数

时间:2013-02-01 01:48:05

标签: vb.net serial-port networkstream hex

嘿所有我试图控制我的Onkyo接收器上的主音量。问题在于它一直有效,直到我进入第10卷。卷号0-9工作正常(将音量从0提高到9)。但是,当我转到第10卷时,它会将音量显示为 16 第11卷= 17 第12卷= 18 等。

excel表说:

"00"-"64"   Volume Level 0 - 100 ( In hexadecimal representation)

enter image description here

串口命令是:

Public Function remoteCommands(ByVal theCommand As String) As Boolean
    Dim Stream As NetworkStream
    Dim Client As New TcpClient()
    Dim streamw As StreamWriter
    Dim i As Int32
    On Error GoTo blah

    Client.Connect(main.avIP, 60128)
    Stream = Client.GetStream()
    Dim returndata As String = ""

    If Client.Connected And Stream.CanWrite Then
        streamw = New StreamWriter(Stream)
        Dim sendBytes(theCommand.Length + 18) As Char

        sendBytes(0) = "I"
        sendBytes(1) = "S"
        sendBytes(2) = "C"
        sendBytes(3) = "P"
        sendBytes(4) = Chr(0)
        sendBytes(5) = Chr(0)
        sendBytes(6) = Chr(0)
        sendBytes(7) = Chr(16)
        sendBytes(8) = Chr(0)
        sendBytes(9) = Chr(0)
        sendBytes(10) = Chr(0)
        sendBytes(11) = Chr(theCommand.Length + 3)
        sendBytes(12) = Chr(1)
        sendBytes(13) = Chr(0)
        sendBytes(14) = Chr(0)
        sendBytes(15) = Chr(0)
        sendBytes(16) = "!"
        sendBytes(17) = "1"

        For i = 0 To (theCommand.Length - 1)
            sendBytes(18 + i) = theCommand.Chars(i)
        Next

        sendBytes(theCommand.Length + 18) = Chr(13) '&HD
        streamw.Write(sendBytes)

        Dim bytes(Client.ReceiveBufferSize) As Byte
        Stream.Read(bytes, 0, CInt(Client.ReceiveBufferSize))
        returndata = Encoding.ASCII.GetString(bytes)

        streamw.Flush()
    Else
        MsgBox("error: Stream.Canwrite failed")
    End If
    Client.Close()
End Function

这就是我如何称呼上述功能:

Dim vol As String = Trim(lanSent(1).Replace("AVVOL", ""))

If Len(vol) = 1 Then
   vol = "0" + vol
Else
   Select Case vol
      Case 10
        vol = "a"
      Case 11
        vol = "b"
   End Select
End If

Call avReceiver.remoteCommands("MVL" & vol)

我正在寻找转换器,我注意到 10 代表十六进制的 A 。但即使发送 MVLA 也没有做任何事情。

我会失踪什么?

更新(并已解决)

Dim vol As Integer = Trim(lanSent(1).Replace("AVVOL", ""))
Dim vol2 = vol.ToString("X2")

Call avReceiver.remoteCommands("MVL" & vol2)

enter image description here

1 个答案:

答案 0 :(得分:1)

发送OA(零A)。文本说它正在寻找十六进制00到十六进制64(十进制0到100)。请注意,十六进制值表示为两位数,而不是一位数。

如果您实际上正在使用VB.NET,那么您的标记就是错误的。 :-)你应该使用类似的东西:

newvol = 10;
vol = newvol.ToString("X2")    `Converts to 2-digit hex string
Call avReceiver.remoteCommands("MVL" & vol)