如何使用以太网连接从秤上读取重量
How to read weight from scale using ethernet connection
我有一台 Mettler Toledo XS32000L 秤,我只是想通过以太网连接和 VB.net (2010) 应用程序读取重量。我发现很多 information/code 用于串行(COM)连接,但没有用于以太网连接。
通讯方式:Client & Server
远程主机地址:192.168.0.2。
远程主机端口号:8001
本地服务器端口号:8000(秤IP:192.168.0.1)
我可以通过回复 ping 体重秤。我曾尝试使用超级终端,但没有成功。任何帮助将不胜感激,希望这个 post 可以帮助许多其他人。谢谢!
其他资源
具有串行连接的 VB 应用程序的代码:http://control.com/thread/1240210560
另一个有用的 link(但不适合我):
http://vb.net-informations.com/communications/vb.net_socket_programming.htm
好的 - 尽管有批评,但经过数小时的研究,我将 post 找到解决方案。我创建了一个 TCP 客户端并修改了设置(IP、端口等)希望这对未来的程序员有所帮助!
Imports System.Net.Sockets
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Connect("192.168.0.1", "S" & vbCrLf)
End Sub
Shared Sub Connect(ByVal server As [String], 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 = 8000
Dim client As New TcpClient(server, port)
' 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()
' 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](256) {}
' 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 'Connect
End Class
我有一台 Mettler Toledo XS32000L 秤,我只是想通过以太网连接和 VB.net (2010) 应用程序读取重量。我发现很多 information/code 用于串行(COM)连接,但没有用于以太网连接。
通讯方式:Client & Server 远程主机地址:192.168.0.2。 远程主机端口号:8001 本地服务器端口号:8000(秤IP:192.168.0.1)
我可以通过回复 ping 体重秤。我曾尝试使用超级终端,但没有成功。任何帮助将不胜感激,希望这个 post 可以帮助许多其他人。谢谢!
其他资源
具有串行连接的 VB 应用程序的代码:http://control.com/thread/1240210560
另一个有用的 link(但不适合我): http://vb.net-informations.com/communications/vb.net_socket_programming.htm
好的 - 尽管有批评,但经过数小时的研究,我将 post 找到解决方案。我创建了一个 TCP 客户端并修改了设置(IP、端口等)希望这对未来的程序员有所帮助!
Imports System.Net.Sockets
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Connect("192.168.0.1", "S" & vbCrLf)
End Sub
Shared Sub Connect(ByVal server As [String], 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 = 8000
Dim client As New TcpClient(server, port)
' 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()
' 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](256) {}
' 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 'Connect
End Class