Click here to Skip to main content
11,705,610 members (52,441 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET TCP/IP
I have a device that I can connect to using HyperTerminal and send strings and receive back the data I need but I cannot get it to work in VB.Net. In VB I can connect and send a string but the data a get back is garbage. I have tried every possible way to write the code but below is the way I have it currently written.

Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect(SERVER_ADDR, SERVER_PORT)
Using networkStream As NetworkStream = tcpClient.GetStream()
    If networkStream.CanWrite And networkStream.CanRead Then
        ' Do a simple write.
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("?LEAK")
        networkStream.Write(sendBytes, 0, sendBytes.Length)
        ' Read the NetworkStream into a byte buffer.
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        ' Output the data received from the host to the console.
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        Console.WriteLine(("Host returned: " + returndata))
    Else
        If Not networkStream.CanRead Then
            Console.WriteLine("cannot not write data to this stream")
            tcpClient.Close()
        Else
            If Not networkStream.CanWrite Then
                Console.WriteLine("cannot read data from this stream")
                tcpClient.Close()
            End If
        End If
    End If
End Using
Console.ReadLine()

All I did to setup HyperTerminal was IP Address, Port Number and TCP/IP (Winsock). Any help would be greatly appreciated.
Posted 28-Sep-12 3:26am

1 solution

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

  Print Answers RSS
0 OriginalGriff 229
1 Sergey Alexandrovich Kryukov 100
2 Inimicos 60
3 ProgramFOX 50
4 Richard MacCutchan 45
0 OriginalGriff 9,053
1 Sergey Alexandrovich Kryukov 8,347
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100