vb.net体重秤tcpip未获取数据
我是vb.net 2008的新手,无法使用tcpclient()从磅秤获取数据。下面提到的代码能够与机器连接,但无法获取数据。但是超级终端能够获取数据 我搜索了大部分文章,但可能只存在串行端口连接的编码 输出MsgBox: 收到:{0}vb.net体重秤tcpip未获取数据,vb.net,tcp,tcpclient,Vb.net,Tcp,Tcpclient,我是vb.net 2008的新手,无法使用tcpclient()从磅秤获取数据。下面提到的代码能够与机器连接,但无法获取数据。但是超级终端能够获取数据 我搜索了大部分文章,但可能只存在串行端口连接的编码 输出MsgBox: 收到:{0} Imports System.Net.Sockets Public Sub Connect(ByVal server As [String], ByVal _Ports As Int32, ByVal message As [String]) T
Imports System.Net.Sockets
Public Sub Connect(ByVal server As [String], ByVal _Ports As Int32, ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = _Ports
Dim client As New TcpClient()
'Dim client As New TcpClient(server, port)
client.Connect(server, port)
If client.Client.Connected Then TextBox3.Text = "Connected"
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
Dim Buffer(client.ReceiveBufferSize) As Byte
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
'Console.WriteLine("Sent: {0}", message)
MsgBox("Sent: {0} " & message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](2047) {}
'MsgBox("Response Byte - " & data.Length)
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
'Console.WriteLine("Received: {0}", responseData)
MsgBox("Received: {0} " & responseData)
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
'Console.WriteLine("ArgumentNullException: {0}", e)
MsgBox("ArgumentNullException: {0}" & e.Message)
Catch e As SocketException
'Console.WriteLine("SocketException: {0}", e)
MsgBox("SocketException: {0}" & e.Message)
End Try
MsgBox("Got to this point in code")
'Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
'Console.Read()
End Sub
这:
…不会自动将{0}
替换为responseData
的值
{0}
占位符与String.Format()
函数一起使用。在所有适用的地方将代码更改为此,并检查结果:
' Let's also get rid of the legacy VB6 MsgBox() function:
MessageBox.Show(String.Format("Received: {0} ", responseData))
另外,一些数据类型(例如,[字符串])周围的方括号是什么?不需要它们。您的错误日志中有占位符{0},如果使用MsgBox($“Received:{0}”和responseData),您会得到什么?我得到了空的响应数据作为输出。但机器状态已连接。但唯一的问题是没有收到数据。你需要查阅一些关于体重秤的文件。无法保证数据以纯文本形式发送。它可能包含由字节序列(或两者)表示的控制字符或数字。旁注:
{0}
用作类似函数的占位符。您正在使用常规字符串连接,因此{0}
将成为字符串的一部分,而不是替换为值。您应该将其从字符串中完全删除。谢谢@VisualIncent,我是vb.net新手。我将与体重秤支持人员进行检查,并更新control character.FWIW,虽然这将删除{0}
,但不会更改结果。OP已经在消息框中添加了responseData
,因此尽管未使用String.Format()。但由于只显示Received{0}
,因此未接收数据或数据以空字符开头。
' Let's also get rid of the legacy VB6 MsgBox() function:
MessageBox.Show(String.Format("Received: {0} ", responseData))