Hi! I'm using C# VS.NET 2003 and mscomm32.ocx for interfacing with the serial port. I want my app to determine the list of available COM ports, but it seems that the mscomm32.ocx does not support this function. So I tried to get the list of available port using this:
[code]for(int i = 0; i < 17; i++)
{
try
{
axMSComm1.CommPort = (short)i;
axMSComm1.PortOpen = true;
System.Threading.Thread.Sleep(10); // short delay
axMSComm1.PortOpen = false;
// add to list of available ports
portList[index++] = "COM" + i.ToString();
}
catch { }
}[/code]
This code works fine on serial ports. However, this code does not work for USB-RS232 serial adapters and virtual serial ports. How can I work around this problem? Is there another way of getting the list of COM ports available?
Comments
: the serial port. I want my app to determine the list of available
: COM ports, but it seems that the mscomm32.ocx does not support this
: function. So I tried to get the list of available port using this:
:
: [code]: for(int i = 0; i < 17; i++)
: {
: try
: {
: axMSComm1.CommPort = (short)i;
: axMSComm1.PortOpen = true;
: System.Threading.Thread.Sleep(10); // short delay
: axMSComm1.PortOpen = false;
:
: // add to list of available ports
: portList[index++] = "COM" + i.ToString();
: }
: catch { }
: }[/code]:
:
: This code works fine on serial ports. However, this code does not
: work for USB-RS232 serial adapters and virtual serial ports. How can
: I work around this problem? Is there another way of getting the list
: of COM ports available?
:
Well I migh not really have the solution for you since you are using an older version of Visual Studio. I am using Visual Studio 2005 and to detect all the available ports you can just use the serialPort component method GetPortNames(). So your code will look like the following:
[code]
//The following will detect the all the available serial ports on the computer and add to comboBox1
foreach (string s in SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
[/code]
The above code will detected all the serialports on your PC and it will add them to a comboBox. But like I said this is works for Visual Studio 2005 and I dont know if 2003 has the serialPort component as I have never used 2003.
loco
: : the serial port. I want my app to determine the list of available
: : COM ports, but it seems that the mscomm32.ocx does not support this
: : function. So I tried to get the list of available port using this:
: :
: : [code]: : for(int i = 0; i < 17; i++)
: : {
: : try
: : {
: : axMSComm1.CommPort = (short)i;
: : axMSComm1.PortOpen = true;
: : System.Threading.Thread.Sleep(10); // short delay
: : axMSComm1.PortOpen = false;
: :
: : // add to list of available ports
: : portList[index++] = "COM" + i.ToString();
: : }
: : catch { }
: : }[/code]: :
: :
: : This code works fine on serial ports. However, this code does not
: : work for USB-RS232 serial adapters and virtual serial ports. How can
: : I work around this problem? Is there another way of getting the list
: : of COM ports available?
: :
:
: Well I migh not really have the solution for you since you are using
: an older version of Visual Studio. I am using Visual Studio 2005 and
: to detect all the available ports you can just use the serialPort
: component method GetPortNames(). So your code will look like the
: following:
:
: [code]:
: //The following will detect the all the available serial ports on the computer and add to comboBox1
: foreach (string s in SerialPort.GetPortNames())
: {
: comboBox1.Items.Add(s);
: }
:
: [/code]:
:
: The above code will detected all the serialports on your PC and it
: will add them to a comboBox. But like I said this is works for
: Visual Studio 2005 and I dont know if 2003 has the serialPort
: component as I have never used 2003.
:
: loco
Thanks, loco, for your reply. But the 2003 version does not have the SerialPort component. That's why i used the mscomm control available in vb6. I wonder why they didn't include that in vs 2003...
I also tried using API calls to get the list of available COM ports but I still get the same problem.. If anyone can point me to the right direction, I will greatly appreciate it!