VB和串口服务器的通信
我不懂,抄了段TCP/IP发送和接收数据的程序,请大家帮我解释一下:
Option Explicit
Private Sub Form_Load()
Dim strPrompt As String
strPrompt = "Enter the IP address of the server"
txtSend.Enabled = False
txtOutput.Enabled = False
tcpClient.RemoteHost = InputBox(strPrompt, "IP Address", "127.0.0.1")
If tcpClient.RemoteHost = "" Then
tcpClient.RemoteHost = "localhost"
End If
tcpClient.RemotePort = 3456
tcpClient.Connect
End Sub
Private Sub Form_Unload(Cancel As Integer)
tcpClient.Close
End Sub
Private Sub tcpClient_Close()
txtSend.Enabled = False
txtOutput.Enabled = False
tcpClient.Close
txtOutput.Text = txtOutput.Text & "Server closed connection" & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Connect()
txtSend.Enabled = True
txtOutput.Enabled = True
txtOutput.Text = tcpClient.RemoteHostIP & _
":" & tcpClient.RemotePort & _
" connected" & vbCrLf & vbCrLf
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim msg As String
tcpClient.GetData msg
txtOutput.Text = txtOutput.Text & msg & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Error(ByVal Number As Integer, Description As String, _
ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, _
ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox Source & ": " & Description, vbExclamation, "TCP/IP Error"
End Sub
Private Sub txtOutput_GotFocus()
If txtOutput.Enabled = True Then
txtSend.SetFocus
End If
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
tcpClient.SendData "CLIENT: " & txtSend.Text
txtOutput.Text = txtOutput.Text & "CLIENT: " & txtSend.Text & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
txtSend.Text = ""
End If
End Sub
谢!
[解决办法]
LZ:MSDN中有完整的代码后解释.