Click here to Skip to main content
15,796,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have connected customer Display with POS application. I am using serial port class for display message on customer Display. To message i need to know with com port this USB display are using. I have searched allowed there are many examples for getting all com pot for that cumputer but i am able to find any help to get perticular com port no e.g COM93,COM01 or COM2.

I have tried following program and also i have tried Microsoft program WMI code creator.

<pre lang="c#">// Get a list of serial port names. string[] ports = SerialPort.GetPortNames();

    Console.WriteLine("The following serial ports were found:");

    // Display each port name to the console.
    foreach (string port in ports)
    {
        Console.WriteLine(port);
    }

Console.ReadLine();





        SerialPort sp = new SerialPort();
        sp.PortName = "COM93";------------- How i can find this no automatic 
        sp.BaudRate = 9600;
        sp.Parity = Parity.None;
        sp.DataBits = 8;
        sp.StopBits = StopBits.One;
        sp.Open();
        sp.Write("\f");
        sp.WriteLine("***Velkommen***");
        sp.Close();
        sp.Dispose();
        sp = null;

I have tried following solution as well, to use this as well for this I get access denied so i need to edit registory to give rights that i dot wana do i want solution programatically

C#
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");


What I have tried:

<pre lang="c#">ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");
Posted
Updated 12-Feb-17 22:45pm
Comments
Ralf Meier 13-Feb-17 3:23am    
If I understood you in the right way you need to know which COM-Port is used by your USB-Device ?
Member 12545398 13-Feb-17 3:33am    
Yes, But how i can find out this with c#
Ralf Meier 13-Feb-17 3:36am    
Some time ago I have had a similar requirement. My Approach was to find the Vendor-ID-Information inside the Registry. But this Solution (which worked for me) was not independent from the Operating System. But I will look for it and provide it as solution when I have found it.
Please wait a while ...
Member 12545398 13-Feb-17 3:37am    
Thanks

You can use SerialPort.GetPortNames() to populate a list box, let the user select a port, and store that as user settings to be re-used at next program start. But this requires that the user knows which port is your display device.

To help the user identifying the device you can also provide the description as shown in the Windows Device Manager or use the description to select the port automatically.

But getting the description is not provided by .NET so that you have to call Windows API functions or use WMI (which requires administrative privileges).

An example can be found at Enumerate All COM Ports and Find Their Name and Description in C# | Svetlin Nakov's Blog[^].
 
Share this answer
 
It was easier than I thought ...

VB
Imports Microsoft.Win32
Imports System.Text.RegularExpressions

Public Class ReaderInfo
    Public DriverExists As Boolean

    Public PortName As String
    Public PortExists As Boolean
    Public PortOpened As Boolean

    Public VendorID As String
    Public ProduktID As String
    Public DriverID As String

    Public Description As String
    Public Typ_Converter As String
    Public Typ_Reader As String

    Sub Clear()
        DriverExists = False

        PortName = ""
        PortExists = False
        PortOpened = False

        VendorID = ""
        ProduktID = ""

        Description = ""
        Typ_Converter = ""
        Typ_Reader = "Kaba B-Net 9107 Card-Reader"
    End Sub

    Sub New()
        Clear()
    End Sub

    Public Sub Get_RegistryInfo(VID As String, PID As String)
        Dim rk1 As RegistryKey = Registry.LocalMachine  ' HKEY_LOCAL_MACHINE
        Dim rk2 As RegistryKey = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum")  ' HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum

        Clear()
        VendorID = VID
        ProduktID = PID

        Dim pattern As String = String.Format("^VID_{0}.PID_{1}", VID, PID)
        Dim _rx As Regex = New Regex(pattern, RegexOptions.IgnoreCase)

        Dim rk2_SubKeyNames, rk3_SubKeyNames, rk4_SubKeyNames As String
        Dim Desc, Mfg As String
        Dim Index As Integer

        For Each rk2_SubKeyNames In rk2.GetSubKeyNames()
            Dim rk3 As RegistryKey = rk2.OpenSubKey(rk2_SubKeyNames)

            For Each rk3_SubKeyNames In rk3.GetSubKeyNames()
                If _rx.Match(rk3_SubKeyNames).Success Then
                    Dim rk4 As RegistryKey = rk3.OpenSubKey(rk3_SubKeyNames)
                    DriverExists = True


                    For Each rk4_SubKeyNames In rk4.GetSubKeyNames()
                        Index = CInt(rk4_SubKeyNames)

                        Dim rk5 As RegistryKey = rk4.OpenSubKey(rk4_SubKeyNames)
                        If rk5.OpenSubKey("Control") IsNot Nothing Then 
                            Dim rk6 As RegistryKey = rk5.OpenSubKey("Device Parameters")

                            Dim rk5_DeviceDesc As String() = rk5.GetValue("DeviceDesc").ToString.Split(";")
                            Desc = rk5_DeviceDesc(rk5_DeviceDesc.Length - 1)

                            If Index = 0 Then
                                Description = Desc
                                PortName = rk6.GetValue("PortName")

                                Dim myComports As String() = System.IO.Ports.SerialPort.GetPortNames()
                                For i As Integer = 1 To myComports.Length
                                    If myComports(i - 1) = PortName Then
                                        PortExists = True
                                        Exit For
                                    End If
                                Next
                            Else ' Index <> 0 // ? > 200
                                Dim rk5_Mfg As String() = rk5.GetValue("Mfg").ToString.Split(";")
                                Mfg = rk5_Mfg(rk5_Mfg.Length - 1)
                                Typ_Converter = Mfg + vbCrLf + Desc
                                DriverID = Index.ToString
                            End If
                        End If
                    Next
                End If
            Next
        Next

    End Sub

End Class


of course ... the sample is VB - but I think you could see how it works. Should I convert it to C# ?
The only Thing is to know the Vendor-ID and the Product-ID. With this Information you call the Method "Get_RegistryInfo"
I hope, it helps - otherwise feel free to contact me again ...
 
Share this answer
 
Comments
Ralf Meier 13-Feb-17 5:04am    
No ... all Info's and Data are written into the Public-Vars of the Class (like for example "PortName") and so on.
This code worked (for me) with Windows7.
But to be sure what happens look with the Debugger. Perhaps the Registry-Keys for your Requirement are different.
Ralf Meier 13-Feb-17 5:05am    
Sorry ... because I don't have your USB-Driver I can't test it for you or give better Information ...
Member 12545398 13-Feb-17 6:17am    
Thanks for great help. I really appreciate that. I have modified it and it worked for me as well and i have hard corded device name. Function is here :


public string Get_RegistryInfo(string VID, string PID)
{
try
{
RegistryKey rk1 = Registry.LocalMachine;
// HKEY_LOCAL_MACHINE
RegistryKey rk2 = rk1.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM");
// HKEY_LOCAL_MACHINE\HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM
VendorID = VID;
ProduktID = PID;
string pattern = string.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
string rk2_SubKeyNames = null;
foreach (string rk2_SubKeyNames_loopVariable in rk2.GetValueNames())
{
rk2_SubKeyNames = rk2_SubKeyNames_loopVariable;
if (rk2_SubKeyNames == "\\Device\\ProlificSerial0")
{
COM_Port = rk2.GetValue(rk2_SubKeyNames).ToString();

}
}
return COM_Port;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return COM_Port;
}

}

Thank for you great help. Thank you so much
Ralf Meier 14-Feb-17 14:44pm    
You're welcome ...
I'm glad, that this idea could work for you too ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900