开发者

Adding send option to vb.net listener

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)
    开发者_StackOverflow中文版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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜