Results 1 to 6 of 6

Thread: Creating Undetermined number of mscomm objects

  1. #1

    Thread Starter
    Lively Member dbarr's Avatar
    Join Date
    Oct 2001
    Location
    Syracuse, NY
    Posts
    86

    Question Creating Undetermined number of mscomm objects

    A project I'm working on needs to control communications on multiple comm ports on the PC. This must be done inside an ActiveX EXE. My problem is I do not know how many ports and therefore how many mscomm objects will be needed at design time. I use some code to create a control listed below:

    Public WithEvents commObj As MSComm

    Private Sub Form_Load()
    Set commObj = Controls.Add("MSCommLib.MSComm", "commObj")
    With commObj
    .CommPort = 1
    .Settings = "9600,e,7,1"
    .RThreshold = 1
    .PortOpen = True
    End With
    End Sub


    How do I make Public Declarations such as above once my code determines how many serial ports I will need? (Need the component accessible from anywhere in the program)

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    Lightbulb Make a control array

    If you make the MSComm object a control array then you can just add as many as you need at runtime. The WithEvents will work for one or many objects in the array.

    You could keep a public variable to define how many objects were added. Then, anywhere in the form module you could reference the object MSComm(n)...

    I hope that helped.
    -scott
    he he he

  3. #3

    Thread Starter
    Lively Member dbarr's Avatar
    Join Date
    Oct 2001
    Location
    Syracuse, NY
    Posts
    86

    Cool Next Question

    If I make a mscomm control array does each element of the array get it's own events. Can the elements be connected to seperate Comm Ports?

  4. #4
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    Yes.
    Each event declaration will change, inserting a new parameter "Index". When the event is fired, the index parameter will indicate which lement of the array the event has come from therefore allowing you to respond using the same element.
    With your original multiple instance method, you would have had to replicate each of the event handlers for each of the MSCOMM instances, with the control array, you have only one instance of each event handler.
    I hope this clarifies things for you.

  5. #5
    jim mcnamara
    Guest
    You have to use

    Dim With Events Myarr(10) as MSComm

  6. #6
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    Place a MSCOMM control onto a form, and call it msComm.
    Set the Index property to 0

    whenever you need another control,

    Load msComm(UBound(msComm()+1))

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