Re: Multiple Undetermined Number of Variables

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Jessee,

If you want to monitor all the ports, then you will have to create a
number of SerialPort instances that correspond to the number of ports on the
system.

In your loop that gets the port names, you would add an entry to a
Dictionary<string, SerialPort> using the name of the serial port as a key.
I assume you would also sign up for whatever events you want to listen for
while creating the ports (note, you want to use the SerialPort constructor
that takes the name of the serial port).


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Jessee Holmes" <JesseeHolmes@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:32699D29-1D55-4B92-9832-584146E12346@xxxxxxxxxxxxxxxx
I'm not sure I'm going about this the right away, and I've attempted to
search every online topic that I can which makes me think this is not
possible or I'm simple coding this incorrectly.

What I'd like to do is this:

1) See hot many COM ports are on a computer: foreach (string port in
SerialPort.GetPortNames()) ....

2) Uniquely and simultaneously use these ports: SerialPort
comport[portNumber] = new SerialPort(); <---- PROBLEM

The problem is I wouldn't know at first if a computer had 1 port or 5
ports
so my code needs to be dynamic with this (therefore I use GetPortNames()
to
establish how many there are).

I can't just say SerialPort comport = new SerialPort(); and assign which
port I'm working with programmaticly, because I want to monitor ALL COM
ports
at the same time. Would hate to make 50 different variables too since that
would limit my program to looking at only 50 ports at once. (lol yeah I
know
that's a lot).

Lastly, I've tried MSDN's suggestion: SerialPort[] comport = new
SerialPort[SerialPort.GetPortNames().Length]; But I don't think that works
since I can't access the variable afterwards: comport[portNumber]; unless
I'm
doing it wrong.

Maybe I'm making this more difficult than it needs to be and missing
something obvious here, maybe I'm just too much of a beginner still in C#,
or
maybe this simply isn't possible at all with what I'm trying to do within
C#.
(Booo Urns)!

Can anyone shed some light on this for me? Doesn't have to be in relation
to
this example. Just a simple, string varName[i]; within a for loop to make
10
variables or something would suffice. I read that this is impossible
online
somewhere, so I'm afraid there is no solution; but I'd like to check the
community first to see if others have ran across this problem or if I'm
just
simply dumb. :(

Thank you for your time. Much appreciated.
-Jessee


.



Relevant Pages

  • Re: Com port Communication
    ... static CommPortIdentifier portId; ... Method declaration ... If your ports are listed then you've installed it correctly. ...
    (comp.lang.java.programmer)
  • Re: Multiple Undetermined Number of Variables
    ... I"m betting that you are NOT allocating the individual SerialPort instances. ... foreach (string portName in SerialPort.GetPortNames()) ... Uniquely and simultaneously use these ports: ...
    (microsoft.public.dotnet.languages.csharp)
  • Java Communications API, help to start!
    ... I have problem with Java Communications API. ... SerialPort serialPort; ... expect the list of ports, ... javax.comm.properties file correct? ...
    (comp.lang.java.programmer)
  • Re: RFID with PIC (PIC16F876)
    ... My laptop has no COM ports. ... My latest cheapy desktop has 2 serial ports and 4 USB ports ... yes, most desktops still have a serialport, but.. ...
    (sci.electronics.design)
  • Re: Looking for New Monitor this Season
    ... meant to say the really new ones have *hdmi* or something called display ... ports and you may have trouble connecting these to your current computer... ... new monitor has that same kind of port (of course it could have additional ...
    (alt.sys.pc-clone.dell)