• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1272
  • Last Modified:

C# - Recognizing & Classifying SERIAL PORTS

Hello Everyone.

I need to find the computer's SERIAL PORTS; however I need separate REAL SERIAL PORT from those that are USB SERIAL PORT.

In the code below I can find all SERIAL PORTS.
How to filter the USB SERIAL PORTS ?
Thanks in advance,
ocaccy

        public Form1()
            {
            InitializeComponent();
            CommPort com=CommPort.Instance;
            int foundRSP=0;

            string[] portList=com.GetAvailablePorts();
            #region This code takes all the serial ports
            for(int i=0;i<portList.Length;++i)
                {
                string name=portList[i];
                comboBox1.Items.Add(name);
                if(name==Settings.Port.PortName)
                    foundRSP=i;
                }

            if(portList.Length>0)
                comboBox1.SelectedIndex=foundRSP;
            #endregion

            string[] USBPorts=System.IO.Ports.SerialPort.GetPortNames();
            #region In this code I need only the USB Serial Port
            for(int i=0;i<USBPorts.Length;++i)
                {
                string name=USBPorts[i];
                comboBoxUSB.Items.Add(name);
                if(name==Settings.Port.PortName)
                    foundRSP=i;
                }

            if(USBPorts.Length>0)
                comboBoxUSB.SelectedIndex=foundRSP;
            #endregion
            }

Open in new window

Real
0
ocaccy
Asked:
ocaccy
  • 4
  • 4
1 Solution
 
CallandorCommented:
One distinction you should be aware of is that USB ports do not have baud rate, data bits, parity and stop bits.  USB uses packets which look like this: http://www.beyondlogic.org/usbnutshell/usb3.shtml

For your project, you should look at this article: http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto
0
 
ocaccyAuthor Commented:
Thank you; Already tried the code of the  http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto, but without success.

Teach me how to display only the USB Serial Ports in (comboBoxUSB.Items.Add(name);)

string[] USBPorts=System.IO.Ports.SerialPort.GetPortNames();
            #region In this code I need only the USB Serial Port
            for(int i=0;i<USBPorts.Length;++i)
                {
                string name=USBPorts[i];
                comboBoxUSB.Items.Add(name);
                if(name==Settings.Port.PortName)
                    foundRSP=i;
                }

            if(USBPorts.Length>0)
                comboBoxUSB.SelectedIndex=foundRSP;
            #endregion

Open in new window


New Form with USB Serial Port with only Port Name.
0
 
CallandorCommented:
I'm not a C# guru, but the example clearly has code for detecting USB:

//If the serial port's instance name contains USB

When you say you tried the code, what did you try, and what did you see?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ocaccyAuthor Commented:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Management;

namespace Search_USB_SP
    {
    public partial class Form1:Form
        {
        public Form1()
            {
            InitializeComponent();
            CommPort com=CommPort.Instance;
            int foundRSP=0;

            string[] portList=com.GetAvailablePorts();
            #region This code takes all the serial ports
            for(int i=0;i<portList.Length;++i)
                {
                string name=portList[i];
                comboBox1.Items.Add(name);
                if(name==Settings.Port.PortName)
                    foundRSP=i;
                }

            if(portList.Length>0)
                comboBox1.SelectedIndex=foundRSP;
            #endregion

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
            string[] USBPorts = System.IO.Ports.SerialPort.GetPortNames();
            #region In this code I need only the USB Serial Port

            foreach (ManagementObject queryObj in searcher.Get)
            {
                lbl_InstanceName.Text="InstanceName: {0}"+ queryObj["InstanceName"];
                lbl_PortName.Text="PortName: {0}"+ queryObj["PortName"];

                //If the serial port's instance name contains USB 
                //it must be a USB to serial device
                if (queryObj["InstanceName"].ToString().Contains("USB"))
                {
                    comboBoxUSB.Items.Add(queryObj["PortName"]);
                }
            }
            #endregion

Open in new window


Get Error
0
 
CallandorCommented:
Can you post the error?  More details will be helpful.
0
 
ocaccyAuthor Commented:
OK.
Error capture
0
 
CallandorCommented:
Right-click References on the right and manually add System.Management.  People have reported this as a problem, even though the "using" statement is there.

You can see that there is a constructor that takes two arguments: http://msdn.microsoft.com/en-us/library/438s6ddc.aspx
0
 
ocaccyAuthor Commented:
Thank you, still not as I need, but this much better than before your tip.
ocaccy
0

Featured Post

Meet the Family that is Made for Collaboration

The TeamConnect Family product group as part of the Sennheiser for Business Portfolio comprising high-quality, technically well-conceived meeting solutions for business communication – designed for any meeting room and any meeting situation.

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