|
-
Oct 12th, 2001, 08:47 AM
#1
Thread Starter
Lively Member
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)
-
Oct 12th, 2001, 12:20 PM
#2
Hyperactive Member
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
-
Oct 12th, 2001, 12:23 PM
#3
Thread Starter
Lively Member
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?
-
Oct 12th, 2001, 05:27 PM
#4
Lively Member
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.
-
Oct 12th, 2001, 06:09 PM
#5
You have to use
Dim With Events Myarr(10) as MSComm
-
Oct 12th, 2001, 06:23 PM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|