向 vb.net 监听器添加发送选项

发布于 2024-11-13 09:31:06 字数 3779 浏览 4 评论 0原文

我有以下代码,用于侦听和评估收到的字符串,但我现在需要添加通过单击按钮将预定义字符串发送回源的选项。

任何帮助或指示我需要研究的地方将不胜感激,我花了几天时间试图完成这项工作,但收效甚微!

我当前收到的错误是,button_click 子中的 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

I have the following code which listens and evaluates strings received, but I now need to add the option of sending pre-defined strings back to the source on a button click.

Any help or a pointer of where I need to reserach would be greatly appreciated, I've spent days trying to get this work but am having very little success!

The error I'm surrently getting is that the line opensock2 = New IO.StreamWriter(client.GetStream) in the button_click sub, is not connected, so I think I need to pass the connection from the background worker, or cause it in the form load but I am at a loss as to how to do this.

All help greatly appreciated.

Cheers,
Chris

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文