Sending UDP data, including hex, using VB.NET

As a hobby, I'm interested in programming an LED sign connected to Ethernet to scroll through messages on the screen. But I have problems sending UDP sender to VB.NET (I am using 2008 now).

Now the mark is good enough to the specification for programming for him .

But an example of a line for sending 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 a hexadecimal character.

Now, to send this to the character, I need to use UDP . However, in the examples that I encoded everything as ASCII before sending, like this one (from UDP: the client sends packets and receives packets from the 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 (hardcoded chr() ) . , . , ​​, , "" , , .

. - ( ): 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)

( : http://pastebin.com/f44417743.)

+3
4

, :

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)
+2

. , 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").

, .

EDIT:

?

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
source

They hosted libraries for several languages, including Visual Basic (in a separate file). I tested the demo with one of my signs and they work!

http://support.favotech.com

0
source

Source: https://habr.com/ru/post/1697890/


All Articles