Results 1 to 3 of 3

Thread: Wondering if anyone could help?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Wondering if anyone could help?

    Hey, I'm new here.. just having a problem with a program I'm creating to talk to devices connected over the network. The program has to be able to send strings out of a specified ip address and port number...and it needs buttons which are customised by the user to set what they want them to do.

    So basically, I used some code from the Microsoft visual basic site and tweaked it about to make it do what I wanted. It was working perfectly fine when the variables message, port and server were being defined in the button onclick subroutine.. but now I need the variables undefined so that the users input can set the values and also I need them to be public so I can use them in other forms....this is where it stopped working.

    My current code:
    The main form with the button that is to be customised:
    Code:
    Public Class frmMain
        Public message As String 'Defines message as a string
    
        Public server As String 'Defines server as a string
        Public port As Int32
        Private Sub cmdButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton1.Click
            Try
                'Dim message As String = "Message" 'Defines message as a string
    
                'Dim server As String = "192.168.95.231" 'Defines server as a string
                'Dim port As Int32 = "10001"
                Dim client As New Net.Sockets.TcpClient(server, port) 'Defining variable client as a tcp client
    
                Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message(0)) 'Changes message into ASCII to store as a byte array, data.
    
                Dim stream As Net.Sockets.NetworkStream = client.GetStream() 'Defines stream as a Network stream
                MsgBox(message)
    
                stream.Write(data, 0, data.Length)
                'Sending the data into the stream
                'Console.WriteLine("Sent: {0}", message) 'Displays message that was sent
                ' Receive the TcpServer.response.
                ' Buffer to store the response bytes.
                data = New [Byte](256) {}
    
                ' String to store the response ASCII representation.
                'Dim responseData As [String] = [String].Empty
    
    
                ' Read the first batch of the TcpServer response bytes.
                'Dim bytes As Int32 = stream.Read(data, 0, data.Length)
                'responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
                'Console.WriteLine("Received: {0}", responseData)
    
                'If responseData = "%1POWR OK" & vbCr Then
                'lblStatus.Text = "Successful"
                'ElseIf responseData = "%1POWR=ERR1" & vbCr Then
                'lblStatus.Text = "Out of parameter"
                'End If
    
                ' Close everything.
                stream.Close()
                client.Close()
            Catch f As ArgumentNullException
                Console.WriteLine("ArgumentNullException: {0}", f)
            Catch f As System.Net.Sockets.SocketException
                Console.WriteLine("SocketException: {0}", f)
            End Try
            MsgBox(server)
            MsgBox(port)
    
    
            'Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
            ' Console.Read()
        End Sub
    
        Private Sub cmdCustomise_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomise.Click
            Me.Visible = False
            frmCustomise.Visible = True
        End Sub
    End Class
    And the button customisation screen:
    Code:
    Public Class frmCustomise
        Public message As String 'Defines message as a string
    
        Public server As String 'Defines server as a string
        Public port As Int32
    
        Private Sub cmdSetButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetButton1.Click
            Dim MyVariable As String
            If radType.Checked = True Then
                frmMain.message = (comB11.Text + comB12.Text + ComB13.Text + comB14.Text + comB15.Text + comB16.Text + comB17.Text + comB18.Text + comB19.Text)
                frmMain.cmdButton1.Text = txtCommandTitle.Text
            Else
                MyVariable = comButton1.Text
                If MyVariable = "On" Then
                    frmMain.cmdButton1.Text = MyVariable
                    frmMain.message = "%1POWR 1" & vbCr
                Else
                    frmMain.cmdButton1.Text = MyVariable
                    frmMain.message = "%1POWR 0" & vbCr
                End If
            End If
    
            If txtPort.Text = "" Then
                MsgBox("Please enter a port number")
            ElseIf txtServer.Text = "" Then
                MsgBox("Please enter an IP address")
            Else
                frmMain.port = txtPort.Text
                frmMain.server = txtServer.Text
            End If
            Me.Visible = False
            frmMain.Visible = True
            'MsgBox(frmMain.message(0))
            'MsgBox(frmMain.port)
            'MsgBox(frmMain.server)
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radType.CheckedChanged
            comB11.Enabled = True
            comB12.Enabled = True
            ComB13.Enabled = True
            comB14.Enabled = True
            comB15.Enabled = True
            comB16.Enabled = True
            comB17.Enabled = True
            comB18.Enabled = True
            comB19.Enabled = True
            txtCommandtitle.Enabled = True
            lblType.Enabled = True
            lblTitle.Enabled = True
            comButton1.Enabled = False
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radChoose.CheckedChanged
            comB11.Enabled = False
            comB12.Enabled = False
            ComB13.Enabled = False
            comB14.Enabled = False
            comB15.Enabled = False
            comB16.Enabled = False
            comB17.Enabled = False
            comB18.Enabled = False
            comB19.Enabled = False
            txtCommandtitle.Enabled = False
            lblType.Enabled = False
            lblTitle.Enabled = False
            comButton1.Enabled = True
        End Sub
    No doubt I have other errors, but I'd like to try and sort out sending the strings right now so any help would be great
    Thanks
    Emily

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Wondering if anyone could help?

    You say that it 'stopped' working. How isnt it working anymore? Are you getting any errors?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Wondering if anyone could help?

    I would suggest a change in the general design, but feel free to disregard. My thinking is that you really want to be communicating based on data coming from various places. It makes plenty of sense to have all of the communication in a single class with variables that are set by outside, but does it really make sense to have this class be a form? It's certainly convenient, but I would guess that you will end up with a more extensible solution if you move all your TCP code out of the form into a class.

    Then the port and other member variables could be exposed via properties, and there could be a SendMessage method that would accept a string and send stuff off.

    The real advantage to this is that once you got the class working once, you wouldn't be tied to that form, but could instantiate the class anywhere you wanted to, though it sounds alot like you would only want a single instance of this class in any program, so there would be a reason to make this a Singleton class (private constructor with a single shared public instance member).

    Other than that, tell us about the errors.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width