Results 1 to 11 of 11

Thread: [RESOLVED] How to use the same SerialPort instance on a second form?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Resolved [RESOLVED] How to use the same SerialPort instance on a second form?

    Hi guys, quick question, in dealing with IO.Ports.Serialport

    I have this class,

    Code:
    ''' <summary>
    ''' Routines for finding and accessing COM ports.
    ''' </summary>
    
    Public Class ComPorts
    '....this class contains different properties and subs that handle the serial port communication. for example:
       Friend Shared Sub FindComPorts()
    '.....omitted
        End Sub
    Friend Function OpenComPort() As Boolean
    '...omitted
    End Function
    'and such..
    (this came from a serial communication example from lvr.com)
    I'm using this class on my first form , like such

    Code:
     Public UserPort1 As ComPorts
    '...
    UserPort1.OpenComPort()
    My question arises here: now that the port is open on the first form(UserPort1 is declared:Public UserPort1 As ComPorts on the first form), how do I keep the port open, when I
    close/hide the first form and give focus/show the second form?? I'm thinking there must be some way like inherit or something that I can keep using the UserPort1 I declared on form1
    Please help--------------------thx

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,432

    Re: How to use the same SerialPort instance on a second form?

    If you close the first form, there isn't anything you can do, as the object will go out of scope and eventually be destroyed. If you are just hiding the form, then it is available to any form that has the first form in scope. It would be best to expose the ComPorts object via a Public property rather than a Public variable, but that has more to do with design philosophy, and nothing to do with performance.

    There is another option to consider, though. As a general rule, global variables are discouraged, since ANYBODY can tamper with a global variable (or at least any code in the project can). However, it sounds like the object in question might make more sense as a global variable than it does as a form variable. Therefore, you might consider adding a module to the project and making UserPort1 a public variable in that module. That would make it available to ALL forms (and any other class in the project).
    My usual boring signature: Nothing

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: How to use the same SerialPort instance on a second form?

    You can declare UserPort1 in a Module or if you want to keep it in from1, declare it as shared field in form1.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: How to use the same SerialPort instance on a second form?

    Add a module to the project and move your declaration there. This makes the variable available to every part of the program.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Re: How to use the same SerialPort instance on a second form?

    well..i tried a few things and actually on form2.vb, this works

    Code:
    form1.UserPort1.WriteToPort("xxxx")
    , where WriteToPort() is another sub defined in the ComPorts class.
    thank you guys for bearing with noob

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,432

    Re: How to use the same SerialPort instance on a second form?

    There's a potential problem with that. By writing form1., you are almost certainly using the default instance of Form1. That may be sufficient, but default instances tend to cause confusion. If you ever create your own instance of form1, and stop using the default instance, then the code will fail, since it is using one specific instance of the form: The default instance.

    I like the public variable in a module better than that. Alternatively, you could make the UserPort1 variable Shared on Form1, which would also work (and would be really similar to the public variable in a module, except that you'd have to decorate it with the form name each time).
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Re: How to use the same SerialPort instance on a second form?

    Thank you sir. I added a module
    Code:
    Module PublicSerialPortModuel
        Public UserPort1 As ComPorts
    
    End Module
    and now I can use UserPort1 in form2 without the "form1.xxxx" prefix.

    I run into another problem though, which got me really nonplussed(well..confused)
    I used a even handler on form1 to handle DataReceived event
    Code:
     AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler
    and that sub is something like
    Code:
        Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            COMportReadBuffer1 = UserPort1.SelectedPort.ReadExisting
            While UserPort1.SelectedPort.BytesToRead > 0
                COMportReadBuffer1 &= UserPort1.SelectedPort.ReadExisting
            End While
      End Sub
    which works all fine on the first form. But when I do the same thing on form2,
    Code:
     AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler2
    when DataReceived event is triggered and I get into DataReceivedHandler2(), the read buffer is blank and its length is 0

    Can you think of any possible reasons for this? I can't ..with UserPort1 now being defined public in the public module

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: How to use the same SerialPort instance on a second form?

    Yup. Handler needs to go in the module as well. You can't have two.

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: How to use the same SerialPort instance on a second form?

    The read data is removed from the receive buffer and that's why you don't get any data in DataReceivedHandler2 (since it has been already read in DataRecivedHandler earlier).
    But why do you need to handlers for the same event? Can't you simply pass the read data to the other form as you read in in one form?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Re: How to use the same SerialPort instance on a second form?

    Thank you sir. I've moved the DataReceivedHandler sub to the module. Probably a real last minute question here..when I'm stepping through
    the code and I find out that it goes through the DataReceivedHandler() twice every time there's data returned.

    Code:
    Public Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            Dim sp As SerialPort = CType(sender, SerialPort)
            COMportReadBuffer1 = sp.ReadExisting()
             'xxxx            'some time delay so that COMportReadBuffer1 can get the whole returned string
                                       
            End Sub
    and the first time ReadExisting gets the actual returned string, but after it gets to End Sub, it goes right back to COMportReadBuffer1 = sp.ReadExisting()
    and this second time it just reads back blank, cuz there's nothing else to read.
    Do you know why the DataReceivedHandler would fire twice? for one datareceived event
    -------------------------------
    ok....based on stanav's reply, i should probably move this
    Code:
     AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler
    to the public module as well?? instead of using AddHandler in the two forms..twice
    -------------
    nope.. can't put addhandler in the public module. but I deleted the addhandler on form2. and it works..it only fires once now
    Last edited by SandiegoSSD; Aug 24th, 2012 at 04:09 PM.

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: How to use the same SerialPort instance on a second form?

    Just handle the port's DataReceived event in 1 form. In the event handler, after you read the data, raise your own event and pass that data to whoever subcribes to the event via event args. In form2, you just have to handle the custom event raised by form1.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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