How to Open a Serial Port by Friendly Name

How to open a serial port by friendly name?

Try running a WMI query on the Win32_SerialPort class. Download WmiCodeCreator to experiment and auto-generate the C# code.

How Do I get the friendly name of serial port in Mono and keep it cross platform

AFAIK there is no "friendly" name for the COMM devices in linux. What I suggest you do is use the /dev/ttyS# as your device name in a linux environment and list them as COMM# in windows.

The linux users will understand the terminology so there shouldn't be a worry there.

GET FRIENDLY PORT NAME Programmatically

Try this .

Public Shared Function ListFriendlyCOMPOrt() As List(Of String)

Dim oList As New List(Of String)

Try
Using searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM26%'")
For Each queryObj As ManagementObject In searcher.Get()
oList.Add(CStr(queryObj("Caption")))
Next
End Using

Return oList

Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try

Return oList

End Function

That should work..

How to locate a serial or tcp converter on a COM port by its name or ID?

Here's some code that looks for all serial ports that contain the given friendly device name (the name shown in device manager).

So if the device manager contains:

device manager

AutofindComPort("Prolific")

Would return a list containing "COM30".

If you'd prefer to search by vid/pid that's possible too with some minor changes.

You need to add a reference to System.Management for this code.

/// <summary>
/// Find all com ports that contain the given friendly device name. i.e.
/// AutofindComPort("Prolific") would return a list with the values "COM1",
/// and "COM3". If the device manager listed
/// "Prolific USB-to-Serial Comm Port (COM1)" and
/// "Prolific USB-to-Serial Comm Port (COM3)".
/// </summary>
/// <param name="deviceName">The friendly name of the device to find com
/// ports for.</param>
/// <returns>The com port names the device(s) are attached to.</returns>
private List<string> AutofindComPort(string deviceName)
{
List<ManagementBaseObject> devs = GetDevices(deviceName);

// Get the com ports from the ManagementBaseObject.
List<string> comnames = new List<string>();
foreach (ManagementBaseObject dev in devs)
{
comnames.Add(
ParsePortNameFromFriendlyName((string)dev.GetPropertyValue("Name")));
}

return comnames;
}

/// <summary>
/// Search through the devices connected to the computer, looking for any
/// that contain the given device name and "COM" in their friendly name.
/// </summary>
/// <returns>All of the matching devices found.</returns>
private List<ManagementBaseObject> GetDevices(string deviceName)
{
// Getting a list of all available com port devices and their friendly
// names. source:
// http://www.codeproject.com/KB/system/hardware_properties_c_.aspx
List<ManagementBaseObject> devices = new List<ManagementBaseObject>();

using (ManagementObjectSearcher searcher
= new ManagementObjectSearcher("Select * from Win32_PnpEntity"))
{
foreach (ManagementBaseObject device in searcher.Get())
{
object nameo = device.GetPropertyValue("Name");
if (nameo != null)
{
string name = (nameo as string);

// Only add item if the friendly names contains "COM" and the device
// name we want.
if (name.Contains("(COM") && name.Contains(deviceName))
{
devices.Add(device);
}
}
}
}

return devices;
}

/// <summary>
/// Parse the port name ("COM30") from the friendly device name ("Prolific
/// USB-to-Serial Comm Port (COM30)").
/// </summary>
/// <param name="friendlyName">The friendly device name to parse.</param>
/// <returns>The com port name.</returns>
private string ParsePortNameFromFriendlyName(string friendlyName)
{
Match m = Regex.Match(friendlyName, @".*\((COM\d+)\).*");
if (m.Success)
{
return m.Groups[1].Captures[0].Value;
}
else
{
return string.Empty;
}
}

Getting Serial Port Information

There is a post about this same issue on MSDN:

Getting more information about a serial port in C#

Hi Ravenb,

We can't get the information through the SerialPort type. I don't know why you need this info in your application. However, there's a solved thread with the same question as you. You can check out the code there, and see if it can help you.

If you have any further problem, please feel free to let me know.

Best regards,
Bruce Zhou

The link in that post goes to this one:

How to get more info about port using System.IO.Ports.SerialPort

You can probably get this info from a WMI query. Check out this tool to help you find the right code. Why would you care though? This is just a detail for a USB emulator, normal serial ports won't have this. A serial port is simply know by "COMx", nothing more.



Related Topics



Leave a reply



Submit