TCP/IP Winsock Communications可在超级终端中工作,但我无法在VB.Net中使它们工作. [英] TCP/IP Winsock Communications work in Hyperterminal but I cannot get them to work in VB.Net.

查看:194
本文介绍了TCP/IP Winsock Communications可在超级终端中工作,但我无法在VB.Net中使它们工作.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以使用超级终端连接到的设备,可以发送字符串并接收所需的数据,但是我无法在VB.Net中使用它.在VB中,我可以连接并发送字符串,但是返回的数据是垃圾.我尝试了所有可能的方式编写代码,但是下面是我目前编写的方式.

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



我要设置超级终端的所有工作就是IP地址,端口号和TCP/IP(Winsock).任何帮助将不胜感激.



All I did to setup HyperTerminal was IP Address, Port Number and TCP/IP (Winsock). Any help would be greatly appreciated.

推荐答案

我知道了.

http://jocoder.org /2012/09/28/vb-net-socket-communications-works-in-hyperterminal-wont-work-in-net/ [
I figured it out.

http://jocoder.org/2012/09/28/vb-net-socket-communications-works-in-hyperterminal-wont-work-in-net/[^]


这篇关于TCP/IP Winsock Communications可在超级终端中工作,但我无法在VB.Net中使它们工作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆