.NET Framework - Multiple Undetermined Number of Variables
Asked By JesseeHolme
29-Oct-07 03:43 PM

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
SerialPort.GetPortNames
(1)
GetPortNames
(1)
SerialPort
(1)
PortNumber
(1)
VarName
(1)
Jessee
(1)
Booo
(1)
Urns
(1)
Nicholas Paldino [.NET/C# MVP] replied...
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@spam.guard.caspershouse.com
Peter Duniho replied...

On 2007-10-29 12:43:01 -0700, Jessee Holmes
In general, if you can't get MSDN sample code to work, then you are
doing it wrong. The MSDN samples are not 100% correct; I've seen bugs
in them myself. But on the whole, the sample code is excellent and in
most cases even where the code could be better, it does work.
So your first thought should always be directed toward how you might be
misunderstanding or misusing the sample code.
Now, as for this particular case: certainly the MSDN code you've
included here sure looks like it ought to do what you want. So either
you haven't used the MSDN code as you've posted it here, or you haven't
described correctly what it is you want, or you have somewhere else
misused the variable declared in the code you've posted.
Since you didn't post any of your own code, it's hard to say exactly
what your mistake is. If you want more specific advice, you should
post a concise example of code that demonstrates the problem you're
having. In this case, I suspect that just the immediate context (that
is, the declaration of the variable and the code that uses the
variable) is sufficient, rather than posting an entire program. Do
make sure that you explain in detail where you are getting an error or
other problem, and what that error or other problem is.
Pete

Serialport Object kann nicht zerstört_werden .NET Framework Ich habe ein Problem mit der System.IO.Ports.SerialPort Klasse. Dieses Benutze ich, um ein externes Ger?t (in diesem Fall ein EFT Terminal XP (1) Visual Studio 2005 (1) Windows Vista (1) SerialPinChange (1) EventHandler (1) RegistryKey (1) SerialPort (1) St.Georgen (1) Hallo Holger, Ich verstehe nicht, warum die Verbindung unterbrochen sein muss ordentliche Kommunikation auf dem Comport stattfinden k?nnen. ich benutze erneut die .Write() Methode der Serialport Klasse um weitere Daten an das Terminal zu schicken. Und das erzeugt die besagte Exception Schnittstellensignale M1 / S1, M2 / S2 u. M5 erfüllt sind. Ohne zu wissen, wie Du Deinen SerialPort öffnest und konfigurierst, welches Übertragungsprotokoll Anwendung findet und ob HW-Hanshaking genutzt wird, kann man mit anschlie?endem .Open() hilft da leider nichts. Hallo Holger IMHO kaum. Beachte, die .NET SerialPort-Klasse hat (leider) diverese 'Randbedingungen', welche nicht explizit dokumentiert sind. Daher solltest du auf die Fragen von Peter _exakte_ Auskunft geben, und (mind.) alle Einstellungen der SerialPort-Instanz hier posten, besser gleich auch die relevanten Code-Ausschnitte. Ich tippe tendendziell auch auf ein Handshake / Timeout Problem (zB wegen dem internen Thread der SerialPort-Instanz) vorausgesetzt es ist nicht dein Code selber. - - Thomas Scheidegger - 'NETMaster' http: / / dnetmaster.net / Hallo
multiple event args in vb.net? VB.NET Discussions MuxClass.GetDualDSIInputCOM1 (1) System.IO.Ports.SerialPort (1) GetDualDSIInputCOM1 (1) DoDualDSIWorkCOM1 (1) DoDualDSIWorkCOM (1) ChangeCOMBaudRateIfNecessary (1) DodualDSIWorkerCOM1 (1) GetDSIInputCOM1 (1) Yes you argument into the DoDualDSIWorkCOM1? Also while in that sub it calls the sub GetDSIInputCOM1 the serialport is only being passed COM1 and never COM 2 how can I get both COM args() As Object = 3D e.Argument Dim intloop As Integer CType(args(0), IO.Ports.SerialPort).DiscardInBuffer() muxClass.GetDualDSIInputCOM1(CType(args(0), IO.Ports.SerialPort)) Catch ex As Exception tListener.AddMethodError(ex) End Try End Sub Public Sub GetDualDSIInputCOM1(ByVal serialPort As IO.Ports.SerialPort) Dim intloop As Integer Try serialPort.ReadTimeout = 3D 500 serialPort.DiscardInBuffer() Dim strDataCOM1 As String = 3D String.Empty If Not serialPort.IsOpen Then strDataCOM1 = 3D
SerialPort, USB ports and Port Names .NET Framework I'm trying to send some printer commands (ZPLII) to an attached USB printer using the SerialPort component. I didn't get very far at all. In fact I haven't gotten anywhere. The first problem I encountered is that a call to SerialPort.GetPortNames() returns an empty string[]. In other words, NO port names are returned. I'm not tab I see several ports listed (LPT1, COM1, USB001, etc). So my question is: Should SerialPort.GetPortNames() be returning the USB, LPT1 and COM ports I see in the printer dialog? Am somethinh it shouldn't? There very simple line of code: string[] ports = System.IO.Ports.SerialPort.GetPortNames(); The port I want to open is called "USB001" - when I try to open that
auch noch in nächsten MS Betriebssystemen funktioniert. .NET Framework - German Discussions System.IO.Ports.SerialPort.GetPortNames (1) System.IO.Ports.SerialPOrt (1) System.IO.Port.SerialPort (1) PNames.GetUpperBound (1) GetUpperBound (1) GetPortNames (1) RegistryKey (1) SerialPort (1) Hallo Matthias, Mit System.IO.Ports.SerialPort GetPortNames() bekommst Du ein Array mit den Namen aller verfügbaren Com-Ports. Damit kannst Du Dir
SerialPort.GetPortNames mit MSCOMM32 (VB6)? .NET Framework Hallo allerseits, Ist es möglich, mit MSCOMM32 die Liste der verfügbaren seriellen Ports abzufragen? Ich denke da an die Funktion SerialPort.GetPortNames, die im Dotnet vorhanden ist. Ein Kollege programmiert in VB6, wo es diese Funktion anscheinend für den Benutzer) einfacher gehen. Die Frage ist halt wie. Gruß Falko VB - German Discussions SerialPort.GetPortNames (1) ModComSuchen.ComSuchen (1) GetDefaultCommConfig (1) COMVorhanden (1) SetupAPI (1) EnumSerialPorts (1) QueryDosDevice (1) AnzahlSerPort Antworten. Ich glaube, das hilft meinem Kollegen weiter, zumindestens wirkte er sehr zuversichtlich. Falko keywords: SerialPort.GetPortNames, mit, MSCOMM32, (VB6)? description: Hallo allerseits, Ist es möglich, mit MSCOMM32 die Liste der verfügbaren