使用 VB.NET 发送包括十六进制的 UDP 数据
作为一种爱好,我对编程一个连接以太网的 LED 标牌以在屏幕上滚动消息很感兴趣。 但我在 VB.NET 中创建 UDP 发送器时遇到问题(我正在使用目前为 2008 年)。
现在这个标志已经足够好了,有关于它的编程规范表。
但是要发送给它的行的示例(第 3 页):
<0x01>Z30<0x02>AA<0x06><0x1B>0b<0x1C>1<0x1A>1This message will show up on the screen<0x04>
使用诸如 之类的代 代表十六进制字符。
现在,要将其发送到标志,我需要使用 UDP。 然而,我的例子在发送之前都将消息编码为 ASCII ,就像这个(来自UDP:客户端向服务器发送数据包并从服务器接收数据包服务器):
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 中输出十六进制代码,我认为语法可能是 &H1A - 发送规范定义为 的内容
我可以修改该代码,以正确地将格式正确的数据包发送到该标志吗?
Grant(发送包含十六进制的数据包后)、Hamish Smith(使用函数获取十六进制值)和 Hafthor(将 chr() 消息硬编码到示例中)的答案在尝试时均无效。 所以我会研究看看还有什么可能出错。 理论上,如果该字符串发送成功,我应该会收到一条包含“OK”的消息,这将有助于了解它何时起作用。
我已经尝试过,现在能够监视正在通过的数据包。 工作数据包示例如下(原始十六进制): http://www. brettjamesonline.com/misc/forums/other/working.raw 与我的版本: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)
As a hobby I'm interesting in programming an Ethernet-connected LED sign to scroll messages across a screen. But I'm having trouble making a UDP sender in VB.NET (I am using 2008 currently).
Now the sign is nice enough to have a specifications sheet on programming for it.
But an example of a line to send to it (page 3):
<0x01>Z30<0x02>AA<0x06><0x1B>0b<0x1C>1<0x1A>1This message will show up on the screen<0x04>
With codes such as <0x01> representing the hex character.
Now, to send this to the sign I need to use UDP. However, the examples I have all encode the message as ASCII before sending, like this one (from 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
To output a hexcode in VB.NET, I think the syntax may possibly be &H1A - to send what the specifications would define as <0x1A>.
Could I modify that code, to correctly send a correctly formated packet to this sign?
The answers from Grant (after sending a packet with hex in it), Hamish Smith (using a function to get hex values), and Hafthor (hardcoded chr() message into example) when attempted all did not work. So I'll research to see what else could go wrong. In theory, if this string is sent successfully, I should have a message containing "OK" back, which will help to know when it works.
I have tried and am now able to monitor the packets going through. A working packet example is this (in raw hex): http://www.brettjamesonline.com/misc/forums/other/working.raw vs my version: http://www.brettjamesonline.com/misc/forums/other/failed.raw. The difference is my hex codes are still not encoded correctly, seen in this side-by-side image: http://www.brettjamesonline.com/misc/forums/other/snapshotcoding.png.
I have used this code to generate the packet and send it:
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)
(Full snippet: http://pastebin.com/f44417743.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以组合一个像这样的快速解码器:
然后使用它来转换:
您还可以创建一个编码器函数来撤消此解码(尽管稍微复杂一点)
并使用它来转换:
或者您可以使用以下内容构建字符串:其中的特殊字符如下所示:
对于发送 UDP 数据包,以下内容应该足够了:
You could put together a quickie decoder like this one:
then use this to transform:
you could also make an encoder function that undoes this decoding (although a little more complicated)
and use this to transform:
or you could just build the string with the special characters in it to begin with like this:
for sending a UDP packet, the following should suffice:
这可能会有所帮助。 在我的公司,我们必须使用 ASCII 和十六进制的组合来与我们的硬件进行通信。
我使用此函数在将 IP 地址发送到硬件之前对其进行十六进制化
,有时我也会使用 hexSomething = Hex(Val(number)).PadLeft(2,"0") 。
我也可以为您提供整个程序的源代码,尽管它旨在与不同的硬件进行通信。
编辑:
您是否尝试以十六进制发送数据包或以十六进制获取数据包?
This might help. At my company we have to communicate with our hardware using sort of a combination of ascii and hex.
I use this function to hexify ip addresses before sending them to the hardware
And occationally I use
hexSomething = Hex(Val(number)).PadLeft(2,"0")
as well.I can give you the source for the whole program too, though it's designed to talk to different hardware.
EDIT:
Are you trying to send packets in hex, or get packets in hex?
UDP 客户端发送字节数组。
您可以使用内存流并将字节写入数组。
The UDP client sends an array of bytes.
You could use a memory stream and write bytes to an array.
他们发布了包括 Visual Basic 在内的多种语言的库(在单独的文件中)。 我用他们的一个标志测试了演示,它们工作正常!
http://support.favotech.com
They posted libraries for a bunch of languages including Visual Basic (in the separate file). I tested the demos out with one of their signs and they work!
http://support.favotech.com