Skip to content

VB.Net – Socket Communications (Works in HyperTerminal, Won’t work in .Net)

September 28, 2012

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 HyperTerminal with no problem 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

From → .Net, CodeProject, Misc

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: