2008-10-04 187 views
3

作爲一種愛好,我很感興趣的是編程連接以太網的LED指示燈以在屏幕上滾動消息。但我在製作VB.NET(我目前使用2008)的UDP發件人時遇到了麻煩。使用VB.NET發送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> 

隨着碼,諸如< 0×01>表示十六進制字符。

現在,要發送此標誌我需要使用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一個十六進制編碼,我想的語法可能是& H1A - 發送規範定義爲< 0x1A>。

我可否修改該代碼,將正確格式的數據包正確發送至此標誌?

當嘗試所有都不起作用時,從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

好吧,我已經恢復尋找這個。我發現使用wireshark可以查看正在發送的原始udp數據包。它與我用過的示例代碼一起出現,HEX文字記錄器在實際數據包中出現空白(00)字符。 – SuperRoach 2008-12-07 10:12:04

+0

你是否曾經在這次冒險中取得過成功?我有這些標誌之一有一個串口發送它的命令,我還沒有任何運氣,但發送它的命令。 – NexAddo 2012-03-13 21:07:28

回答

0

這可能會有幫助。在我的公司,我們必須使用ascii和十六進制的組合來與我們的硬件進行通信。

我用這個功能將它們發送到硬件

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")以及前hexify IP地址。

我也可以給你整個程序的源碼,雖然它的目的是與不同的硬件進行通信。

編輯:

你們是不是要在十六進制發送數據包,或十六進制得到數據包?

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" 

你也可以做出撤銷該解碼(雖然有點編碼器功能更復雜)

​​

並用它來轉換:

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) 
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 
0

他們爲包括Visual Basic在內的一些語言(在單獨的文件中)發佈了庫。我用他們的一個標誌測試了這些演示,並且他們工作了!

http://support.favotech.com