Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
7 Feb 2013CPOL 15.2K   6   1
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:

VB.NET
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionok Pin
BillW3314-Mar-13 5:20
professionalBillW3314-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    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.