我有以下代码来监听和评估收到的字符串,但我现在需要添加在按钮点击时将预定义字符串发送回源代码的选项。
我非常感谢任何帮助或指示我需要研究的地方,我花了好几天努力完成这项工作,但收效甚微!
我正在得到的错误是button_click sub中的行opensock2 = New IO.StreamWriter(client.GetStream)
没有连接,所以我想我需要从后台工作者传递连接,或者在表单加载时导致它但是我对如何做到这一点感到茫然。
所有人都非常感谢。
干杯, 克里斯
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Private client As New System.Net.Sockets.TcpClient '("10.0.0.25", 4000)
Dim ipaddress(3) As Byte
Dim swon As String = "A55A6B0550000000FFFBDE0030C8" 'switch on
Dim swoff As String = "A55A6B0570000000FFFBDE0030E8" 'switch off
Dim but0 As String = "A55A6B0500000000FFFBDE002066" 'button release
Dim msg As String = "0000000000000000000000000000"
Dim t As Integer = "00"
Dim returndata As String = msg
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Listener.RunWorkerAsync()
Button2.Enabled = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Listener.DoWork
ipaddress(0) = 10
ipaddress(1) = 0
ipaddress(2) = 0
ipaddress(3) = 14
Dim opensock As New Net.Sockets.TcpListener(New Net.IPAddress(ipaddress), 4000)
Try
opensock.Start()
Catch ex As System.Net.Sockets.SocketException
mess.Text = "cannot start connection"
End Try
Dim tcpClient As TcpClient = opensock.AcceptTcpClient()
While True
If Listener.CancellationPending Then
e.Cancel = True
opensock.Stop()
Exit While
End If
Listener.ReportProgress(1)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
Listener.ReportProgress(2)
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Try
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Catch ex As Exception
Listener.ReportProgress(3)
End Try
returndata = Encoding.ASCII.GetString(bytes)
Listener.ReportProgress(4)
Else
'returndata = msg
End If
End While
End Sub
Private Sub Listener_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Listener.RunWorkerCompleted
t = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
temp.Text = t
comm.Text = returndata
If e.Cancelled Then
mess.AppendText("cancelled" + vbCrLf)
End If
End Sub
Private Sub Listener_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Listener.ProgressChanged
Select Case e.ProgressPercentage
Case 1
mess.AppendText("connected" + vbCrLf)
'comm.Text = msg
Case 2
mess.AppendText("Can read and write" + vbCrLf)
mess.AppendText("data received" + vbCrLf)
Case 3
mess.AppendText("error" + vbCrLf)
Case 4
t = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
temp.Text = t
comm.Text = returndata
mess.AppendText("Last temp" + vbCrLf)
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim opensock2 As IO.StreamWriter
Try
opensock2 = New IO.StreamWriter(client.GetStream)
opensock2.WriteLine(swon)
opensock2.WriteLine(but0)
opensock2.Flush()
Catch ex As Exception
mess.AppendText(ex.ToString + vbCrLf)
End Try
End Sub
End Class