title: Sending UDP data including hex using VB.NET
body: <p>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 <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" rel="nofollow noreferrer">VB.NET</a> (I am using 2008 currently).</p>
  
  <p>Now the sign is nice enough to have <a href="http://support.favotech.com/protocol.specs.2.4.jetfile.pdf" rel="nofollow noreferrer">a specifications sheet on programming for it</a>.</p>
  
  <p>But an example of a line to send to it (page 3):</p>
  
  <pre><code>&lt;0x01&gt;Z30&lt;0x02&gt;AA&lt;0x06&gt;&lt;0x1B&gt;0b&lt;0x1C&gt;1&lt;0x1A&gt;1This message will show up on the screen&lt;0x04&gt;
  </code></pre>
  
  <p>With codes such as &lt;0x01> representing the hex character.</p>
  
  <p>Now, to send this to the sign I need to use <a href="http://en.wikipedia.org/wiki/User_Datagram_Protocol" rel="nofollow noreferrer">UDP</a>. However, the examples I have all encode the message as <a href="http://en.wikipedia.org/wiki/ASCII" rel="nofollow noreferrer">ASCII</a> before sending, like this one (from <em><a href="http://www.java2s.com/Code/VB/Network-Remote/UDPClientsendspacketstoandreceivespacketsfromaserver.htm" rel="nofollow noreferrer">UDP: Client sends packets to, and receives packets from, a server</a></em>):</p>
  
  <pre><code>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:" &amp; _
              vbCrLf &amp; "Length: " &amp; data.Length &amp; vbCrLf &amp; _
              System.Text.Encoding.ASCII.GetString(data))
  
        End While
  
     End Sub ' WaitForPackets
  
  End Class
  </code></pre>
  
  <p>To output a hexcode in VB.NET, I think the syntax may possibly be &amp;H1A - to send what the specifications would define as &lt;0x1A>.</p>
  
  <p>Could I modify that code, to correctly send a correctly formated packet to this sign?</p>
  
  <p>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.</p>
  
  <p>I have tried and am now able to monitor the packets going through. A working packet example is this (in raw hex): <a href="http://www.brettjamesonline.com/misc/forums/other/working.raw" rel="nofollow noreferrer">http://www.brettjamesonline.com/misc/forums/other/working.raw</a> vs my version: <a href="http://www.brettjamesonline.com/misc/forums/other/failed.raw" rel="nofollow noreferrer">http://www.brettjamesonline.com/misc/forums/other/failed.raw</a>. The difference is my hex codes are still not encoded correctly, seen in this side-by-side image: <a href="http://www.brettjamesonline.com/misc/forums/other/snapshotcoding.png" rel="nofollow noreferrer">http://www.brettjamesonline.com/misc/forums/other/snapshotcoding.png</a>.</p>
  
  <p>I have used this code to generate the packet and send it:</p>
  
  <pre><code>container = &amp;H1 &amp; "Z" &amp; &amp;H30 &amp; &amp;H2 &amp; "temp.nrg" &amp; &amp;H1C &amp; "1Something" &amp; &amp;H4
      '    This did not appear to work neither
      'container = Chr(&amp;H1) &amp; "Z" &amp; Chr(&amp;H30) &amp; Chr(&amp;H2) &amp; Chr(&amp;H1C) &amp; "1Something" &amp; Chr(&amp;H4)
      '&lt;0x01&gt;Z00&lt;0x02&gt;FILENAME&lt;0x1C&gt;1Test to display&lt;0x04&gt;   &lt;- the "official" spec to send
  Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(container)
  </code></pre>
  
  <p>(Full snippet: <a href="http://pastebin.com/f44417743" rel="nofollow noreferrer">http://pastebin.com/f44417743</a>.)</p>