我想使用Visual Basic 6.0将十六进制数据发送到以太网或LAN。十六进制数据将如下:
AB7745683ACB76B34E5FF5E99EBC5F878A6BC8E9DF9876BCEA89
在表单上我有一个文本框,用户将在其中输入此数据。
如何使用Visual Basic 6.0将此十六进制数据发送到LAN或以太网?
答案 0 :(得分:3)
'1 form with:
' 1 textbox control: name=Text1
' 1 winsock control: name=Winsock1
Option Explicit
Private Sub Command1_Click()
SendCmd Text1.Text
End Sub
Private Sub Form_Load()
Text1.Text = "AB7745683ACB76B34E5FF5E99EBC5F878A6BC8E9DF9876BCEA89"
End Sub
Private Sub SendCmd(strHex As String)
Dim strCmd As String
With Winsock1
'make sure we are connected
DoConnect
'convert hex to cmd
strCmd = Hex2Cmd(strHex)
'send cmd
.SendData strCmd
End With 'Winsock1
End Sub
Private Sub DoConnect()
With Winsock1
If .State <> sckConnected Then
.Connect "192.168.98.17", 5001 'Destination IP address and port
Do Until .State = sckConnected
DoEvents
Loop
End If
End With 'Winsock1
End Sub
Private Function Hex2Cmd(strHex As String) As String
Dim intHex As Integer
Dim strCmd As String
strCmd = ""
For intHex = 1 To Len(strHex) Step 2
strCmd = strCmd & Chr$(Val("&H" & Mid$(strHex, intHex, 2)))
Next intHex
Hex2Cmd = strCmd
End Function