65.9K
CodeProject is changing. Read more.
Home

VB.NET – Socket Communications (Works in HyperTerminal, Won’t Work in .NET)

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (2 votes)

Feb 1, 2013

CPOL
viewsIcon

15568

Socket communications works in HyperTerminal, won’t work in .NET

I recently worked on an application using a Serial to WiFi device (B&B Electronics/Quatech ABDG-SE-IN5410) to collect data. I was able to talk to the device through a HyperTerminal with no problems and I thought it would be a snap. I was wrong. I searched the internet for days and tried every variation of the code I could find and nothing worked. I got it working and this is what I did:

  1. Download SmartSniff.
  2. Open SmartSniff and goto Options->Display Filter and add a filter for your remote device. Example – include:remote:99.99.999.99:9999 (ip address:port).
  3. Click OK and set Display Mode to Hex Dump (F4) and then Start Capture (F5).
  4. Open HyperTerminal and connect to your device and send your command.
  5. SmartSniff will display all of the characters that are passed.

From this, I was able to determine what format the device was looking for. As it turns out, it was sending and receiving each individual character of my command string. The code I used is listed below:

ReadOnly BUFFER_SIZE As Integer = 1024 * 1024
    Const SERVER_ADDR As String = "99.99.9999.99"
    Const SERVER_PORT As Integer = 9999
    ReadOnly buffer As Byte() = New Byte(BUFFER_SIZE - 1) {}

Private Function Send(ByRef socket As Socket, ByVal outString As String, _
ByRef inString As String) As Boolean
    Send = True
    inString = String.Empty
    Try
        Dim err As SocketError = SocketError.Success
        Dim inLength As Integer = 0
        Dim bytes As Byte() = ASCIIEncoding.ASCII.GetBytes(outString)

        socket.Send(bytes, 0, bytes.Length, SocketFlags.None, err)
        rtb.AppendText(String.Format("Error Code:{0}{1}", err, ControlChars.CrLf))

        Dim t As Integer = Environment.TickCount
        Do
            inLength = socket.Receive(buffer, buffer.Length, SocketFlags.None)
            inString &= Encoding.ASCII.GetString(buffer, 0, inLength)
        Loop Until inString.Contains(outString) OrElse Environment.TickCount - t > 1000

        If Not inString.Contains(outString) Then
            Send = False
        End If
    Catch ex As Exception
        Send = False
    End Try
End Function

Private Function SendWord(ByVal word As String, ByRef inString As String) As Boolean
    SendWord = True
    Using socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim ip As IPAddress = IPAddress.Parse(SERVER_ADDR)
        socket.Connect(New IPEndPoint(ip, SERVER_PORT))
        For Each c As Char In word
            Send(socket, c, inString)
            rtb.AppendText(inString & ControlChars.CrLf)
        Next
    End Using
End Function

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    Dim inString As String = String.Empty
    SendWord("COMMAND" & ControlChars.Cr, inString)
End Sub