array of serial ports

I want to have an array of serial ports (mscomm or axmscomm) so that I can choose the serial port in my code and then set comm parameters and input and output data on that port.

I've tried:

Public serialports() As AxMSCommLib.AxMSComm

ReDim serialports(5)
serialports(0) = New AxMSCommLib.AxMSComm
serialports(1) = New AxMSCommLib.AxMSComm
serialports(2) = New AxMSCommLib.AxMSComm
serialports(3) = New AxMSCommLib.AxMSComm
serialports(4) = New AxMSCommLib.AxMSComm

serialports(0).Commport = 1     Throws an InvalidActiveStateException

Is there any other way of having an array of serial ports, even if I drag the serial control 5 times onto my form and then put them into an array?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I've done this with .NET controls, but not with activeX controls, so it may work, may not.

If you drag them on, you should get something like serial1, serial2, serial3, serial4, serial5.
Then you can just do this in the Form_Load (or other initialization code).

Dim serialports() As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm() {serial1, serial2, serial3, serial4, serial5}

If that gives you problems, you can try:
Dim serialports() As Object = New Object() {serial1, serial2, serial3, serial4, serial5}

Then, make an accessor that returns the MSComm control type.  Something like ...

Private ReadOnly Property Port(ByVal index As Integer) As AxMSCommLib.AxMSComm
      Return DirectCast(serialports(index), AxMSCommLib.AxMSComm)
    End Get
End Property

You should then be able to use
  Port(0).Commport = 1

NOTE: Your real problem may be that you're just doing a "New" and that's all, and more setup is needed.  Drag one control on, then look at the code in the region "Windows Form Designer generated code" to see everything needed to correctly set it up.  If you duplicate this functionality in your own code, you should be able to dynamically create the controls, and you should be able to avoid actually dragging the controls on.  In place of designer code that uses WithEvents, you'll need to appropriately use AddEventHandler.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Also, I vaguely recall having to do something like this sometimes:

Dim currentPort As AxMSCommLib.AxMSComm = serialports(0)
currentPort.Commport = 1

In other words, the array syntax itself was causing the problem.
kevinmeredithAuthor Commented:
Here is the solution I used:

I dragged 5 serial controls onto the form:  AxMSComm1, AxMSComm2, AxMSComm3, AxMSComm4, AxMSComm5,

In Form declarations:

    Public serialportctls(5) As AxMSCommLib.AxMSComm

In Form Load:

        serialportctls(0) = AxMSComm1
        serialportctls(1) = AxMSComm2
        serialportctls(2) = AxMSComm3
        serialportctls(3) = AxMSComm4
        serialportctls(4) = AxMSComm5


        ' Set up serialport 0 for talking to the S4
        serialportctls(0).CommPort = 1
        serialportctls(0).Settings = "9600,N,8,1"
        serialportctls(0).InputLen = 0
        serialportctls(0).InputMode = MSCommLib.InputModeConstants.comInputModeBinary
        serialportctls(0).PortOpen = True

In the end, I was able to talk to 3 serial ports (via the array) more or less simultaneously.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.