array of serial ports

Posted on 2004-11-04
Last Modified: 2008-01-09
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?
Question by:kevinmeredith
    LVL 12

    Accepted Solution

    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.

    LVL 12

    Expert Comment

    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.

    Author Comment

    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.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now