作为一种业余爱好,我很有兴趣编写一个以太网连接的LED标志,以便在屏幕上滚动消息。但是我在VB.NET(我目前正在使用2008)中制作UDP发送者时遇到了麻烦。
现在这个标志非常好,可以a specifications sheet on programming for it。
但是要发送给它的行的示例(第3页):
<0x01>Z30<0x02>AA<0x06><0x1B>0b<0x1C>1<0x1A>1This message will show up on the screen<0x04>
代码如&lt; 0x01&gt;代表十六进制字符。
现在,要将此信息发送到标志,我需要使用UDP。但是,我在发送之前的所有示例都将消息编码为ASCII,就像这个(来自 UDP: Client sends packets to, and receives packets from, a server ):
Imports System.Threading
Imports System.Net.Sockets
Imports System.IO
Imports System.Net
Public Class MainClass
Shared Dim client As UdpClient
Shared Dim receivePoint As IPEndPoint
Public Shared Sub Main()
receivePoint = New IPEndPoint(New IPAddress(0), 0)
client = New UdpClient(8888)
Dim thread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
thread.Start()
Dim packet As String = "client"
Console.WriteLine("Sending packet containing: ")
'
' Note the following line below, would appear to be my problem.
'
Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(packet)
client.Send(data, data.Length, "localhost", 5000)
Console.WriteLine("Packet sent")
End Sub
Shared Public Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("Packet received:" & _
vbCrLf & "Length: " & data.Length & vbCrLf & _
System.Text.Encoding.ASCII.GetString(data))
End While
End Sub ' WaitForPackets
End Class
要在VB.NET中输出十六进制代码,我认为语法可能是&amp; H1A - 将规范定义的内容发送为&lt; 0x1A&gt;。
我可以修改该代码,正确地将正确格式化的数据包发送到此符号吗?
Grant的答案(在发送带有十六进制的数据包之后),Hamish Smith(使用函数获取十六进制值)和Hafthor(硬编码chr()消息进行示例)在尝试时都没有用。所以我会研究看看还有什么可能出错。从理论上讲,如果成功发送此字符串,我应该有一条包含“OK”的消息,这将有助于了解它何时起作用。
我已经尝试过,现在能够监控正在通过的数据包。一个有效的数据包示例(原始十六进制):http://www.brettjamesonline.com/misc/forums/other/working.raw vs我的版本:http://www.brettjamesonline.com/misc/forums/other/failed.raw。区别在于我的十六进制代码仍未正确编码,在此并排图像中可以看到:http://www.brettjamesonline.com/misc/forums/other/snapshotcoding.png。
我已经使用此代码生成数据包并发送它:
container = &H1 & "Z" & &H30 & &H2 & "temp.nrg" & &H1C & "1Something" & &H4
' This did not appear to work neither
'container = Chr(&H1) & "Z" & Chr(&H30) & Chr(&H2) & Chr(&H1C) & "1Something" & Chr(&H4)
'<0x01>Z00<0x02>FILENAME<0x1C>1Test to display<0x04> <- the "official" spec to send
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(container)
(完整摘录:http://pastebin.com/f44417743。)
答案 0 :(得分:2)
你可以把像这样的快速解码器放在一起:
Function HexCodeToHexChar(ByVal m as System.Text.RegularExpressions.Match) As String
Return Chr(Integer.Parse(m.Value.Substring("<0x".Length, 2), _
Globalization.NumberStyles.HexNumber))
End Function
然后使用它来转换:
Dim r As New System.Text.RegularExpressions.Regex("<0x[0-9a-fA-F]{2}>")
Dim s As String = r.Replace("abc<0x44>efg", AddressOf HexCodeToHexChar)
' s should now be "abcDefg"
你也可以制作一个编码器功能来解除这种解码(尽管有点复杂)
Function HexCharToHexCode(ByVal m As Match) As String
If m.Value.StartsWith("<0x") And m.Value.EndsWith(">") And m.Value.Length = "<0x??>".Length Then
Return "<0<0x78>" + m.Value.Substring("<0x".Length)
ElseIf Asc(m.Value) >= 0 And Asc(m.Value) <= &HFF Then
Return "<0x" + Right("0" + Hex(Asc(m.Value)), 2) + ">"
Else
Throw New ArgumentException("Non-SBCS ANSI characters not supported")
End If
End Function
并使用它来转换:
Dim r As New Regex("[^ -~]|<0x[0-9a-fA-F]{2}>")
Dim s As String = r.Replace("abc"+chr(4)+"efg", AddressOf HexCharToHexCode)
' s should now be "abc<0x04>efg"
或者您可以使用其中的特殊字符构建字符串,如下所示:
Dim packet As String = Chr(&H01) + "Z30" + Chr(&H02) + "AA" + Chr(&H06) + _
Chr(&H1B) + "0b" + Chr(&H1C) + "1" + Chr(&H1A) + _
"1This message will show up on the screen" + Chr(&H04)
发送UDP数据包,以下内容就足够了:
Dim i As New IPEndPoint(IPAddress.Parse("192.168.0.5"), 3001) ''//Target IP:port
Dim u As New UdpClient()
Dim b As Byte() = Encoding.UTF8.GetBytes(s) ''//Where s is the decoded string
u.Send(b, b.Length, i)
答案 1 :(得分:0)
这可能会有所帮助。在我的公司,我们必须使用ascii和hex的组合与我们的硬件进行通信。
我使用此功能在将IP地址发送到硬件之前对其进行压缩
Public Function HexFromIP(ByVal sIP As String)
Dim aIP As String()
Dim sHexCode As String = ""
aIP = sIP.Split(".")
For Each IPOct As String In aIP
sHexCode += Hex(Val(IPOct)).PadLeft(2, "0")
Next
Return sHexCode
End Function
而且我也使用hexSomething = Hex(Val(number)).PadLeft(2,"0")
。
我也可以为你提供整个程序的来源,虽然它的目的是与不同的硬件进行通信。
编辑:
您是尝试以十六进制格式发送数据包还是以十六进制格式发送数据包?
答案 2 :(得分:0)
UDP客户端发送一个字节数组 您可以使用内存流并将字节写入数组。
Public Class MainClass
Shared client As UdpClient
Shared receivePoint As IPEndPoint
Public Shared Sub Main()
receivePoint = New IPEndPoint(New IPAddress(0), 0)
client = New UdpClient(8888)
Dim thread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))
thread.Start()
Dim packet As Packet = New Packet("client")
Console.WriteLine("Sending packet containing: ")
Dim data As Byte() = packet.Data
client.Send(data, data.Length, "localhost", 5000)
Console.WriteLine("Packet sent")
End Sub
Public Shared Sub WaitForPackets()
While True
Dim data As Byte() = client.Receive(receivePoint)
Console.WriteLine("Packet received:" & _
vbCrLf & "Length: " & data.Length & vbCrLf & _
System.Text.Encoding.ASCII.GetString(data))
End While
End Sub ' WaitForPackets
End Class
Public Class Packet
Private _message As String
Public Sub New(ByVal message As String)
_message = message
End Sub
Public Function Data() As Byte()
Dim ret(13 + _message.Length) As Byte
Dim ms As New MemoryStream(ret, True)
ms.WriteByte(&H1)
'<0x01>Z30<0x02>AA<0x06><0x1B>0b<0x1C>1<0x1A>1This message will show up on the screen<0x04>
ms.Write(System.Text.Encoding.ASCII.GetBytes("Z30"), 0, 3)
ms.WriteByte(&H2)
ms.Write(System.Text.Encoding.ASCII.GetBytes("AA"), 0, 2)
ms.WriteByte(&H6)
ms.Write(System.Text.Encoding.ASCII.GetBytes("0b"), 0, 2)
ms.WriteByte(&H1C)
ms.Write(System.Text.Encoding.ASCII.GetBytes("1"), 0, 1)
ms.WriteByte(&H1A)
ms.Write(System.Text.Encoding.ASCII.GetBytes(_message), 0, _message.Length)
ms.WriteByte(&H4)
ms.Close()
Data = ret
End Function
End Class
答案 3 :(得分:0)
他们发布了一系列语言的库,包括Visual Basic(在单独的文件中)。我用他们的一个标志测试了演示,然后他们工作了!