Click here to Skip to main content
Click here to Skip to main content

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

License

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
Follow on   Google+

Comments and Discussions

 
Questionok PinmemberCIDev14-Mar-13 5:20 
There is some interesting and useful information here. But, it is a short article without source code or in depth analysis, therefore it would be more appropriate for it to be a tip rather than an article.
Just because the code works, it doesn't mean that it is good code.

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 7 Feb 2013
Article Copyright 2013 by Patrick Cunningham
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid