G
Guest
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; 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
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; 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