Click here to Skip to main content
13,353,462 members (65,933 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 1 Feb 2013

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

, 7 Feb 2013
Rate this:
Please Sign up or sign in to vote.
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
        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
            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)
    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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Patrick Cunningham
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionok Pin
CIDev14-Mar-13 6:20
memberCIDev14-Mar-13 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 7 Feb 2013
Article Copyright 2013 by Patrick Cunningham
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid