[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

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?
0
kevinmeredith
Asked:
kevinmeredith
  • 2
1 Solution
 
farsightCommented:
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
    Get
      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.

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpconeventsdelegates.htm
0
 
farsightCommented:
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.
0
 
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

Then:

        ' 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.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now