确定串行端口是正常的COM还是SPP

时间:2011-12-19 14:56:31

标签: c# bluetooth serial-port

我正在寻找一种方法来确定COM是标准COM还是SPP COM,也称为COM设备的电缆替换蓝牙适配器。

我的设备可以在USB(COM - > USB)和蓝牙中使用,蓝牙接口可与SPP配合使用。

我目前正在使用System.IO.Ports.SerialPort.GetPortNames()来获取COM。

有没有办法确定它是否与蓝牙或USB连接?

解决方案:

System.Management.ManagementObjectSearcher Searcher = new System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (System.Management.ManagementObject Port in Searcher.Get())
{
    foreach (System.Management.PropertyData Property in Port.Properties)
    {
        Console.WriteLine(Property.Name + " " + (Property.Value == null ? null : Property.Value.ToString()));
    }
}

输出类似:

Availability 2
Binary True
Capabilities 
CapabilityDescriptions 
Caption Standard Serial over Bluetooth link (COM10)
ConfigManagerErrorCode 0
ConfigManagerUserConfig False
CreationClassName Win32_SerialPort
Description Standard Serial over Bluetooth link
DeviceID COM10
ErrorCleared 
ErrorDescription 
InstallDate 
LastErrorCode 
MaxBaudRate 9600
MaximumInputBufferSize 0
MaximumOutputBufferSize 0
MaxNumberControlled 
Name Standard Serial over Bluetooth link (COM10)
OSAutoDiscovered True
PNPDeviceID BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\8&3062A492&0&000000000000_0000001C
PowerManagementCapabilities System.UInt16[]
PowerManagementSupported False
ProtocolSupported 
ProviderType RS232 Serial Port
SettableBaudRate True
SettableDataBits True
SettableFlowControl True
SettableParity True
SettableParityCheck False
SettableRLSD True
SettableStopBits True
Status OK
StatusInfo 3
Supports16BitMode False
SupportsDTRDSR True
SupportsElapsedTimeouts True
SupportsIntTimeouts True
SupportsParityCheck False
SupportsRLSD True
SupportsRTSCTS True
SupportsSpecialCharacters False
SupportsXOnXOff False
SupportsXOnXOffSet False
SystemCreationClassName Win32_ComputerSystem
SystemName JVALDRON-PC
TimeOfLastReset 

3 个答案:

答案 0 :(得分:7)

您无法通过SerialPort类找到此信息。您需要进行WMI查询。

做一些与此有关的事情可能会引导你去

ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * 
                                      from WIN32_SerialPort");

foreach(ManagementObject Port in searcher.Get()) {

       string a = (string) Port.GetPropertyValue("Name");

}

我没有加载此代码,因此我不知道您可以获得哪些其他属性。但是,无论如何,WMI都是这样做的方式。

答案 1 :(得分:0)

我看到你正在看蓝牙连接的设备:

查询Win32_PnPSignedDriver并查看InfName属性。值应为bthspp.inf

我不能肯定地说,对于支持SPP协议的每个供应商的蓝牙设备,inf文件总是这个名称,但这是默认设置。

COM&的类GUID LPT端口是:{4d36e978-e325-11ce-bfc1-08002be10318} 参考:https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426

ManagementObjectSearcher Searcher = new ManagementObjectSearcher( computer + @"root\cimv2", 
              "SELECT * FROM Win32_PnPSignedDriver " 
            + "WHERE    ClassGuid = '{4d36e978-e325-11ce-bfc1-08002be10318}' " 
            +       AND DeviceID LIKE 'BTHENUM%' 
            );

答案 2 :(得分:0)

也许这对某人有帮助。 代码将返回所有可用的SPP串行端口。

public List<AvailableComPort> LoadAvailableComPorts()
    {
        List<AvailableComPort> serialComPortList = new List<AvailableComPort>();
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
        {
            foreach (ManagementObject queryObj in searcher.Get())
            {
                if (queryObj["PNPDeviceID"] != null && queryObj["DeviceID"] != null)
                {
                    //
                    // "BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&1ADE6D9D&1&0012F328A5A3_C00000000"
                    // => get only "0012F328A5A3" as ID

                    string pnpDeviceId = queryObj["PNPDeviceID"].ToString();
                    string id = pnpDeviceId.Split(new string[] { "\\" }, StringSplitOptions.None).LastOrDefault();
                    id = id.Split(new string[] { "&" }, StringSplitOptions.None).LastOrDefault();
                    id = id.Split(new string[] { "_" }, StringSplitOptions.None).FirstOrDefault();

                    if (serialComPortList.Where(o => o.Id == id).Count() == 0)
                        serialComPortList.Add(new AvailableComPort() { Id = id, ComPort = queryObj["DeviceID"].ToString() });
                }
            }
        }

        List<AvailableComPort> comPortAdvancedList = new List<AvailableComPort>();
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity"))
        {
            foreach (ManagementObject queryObj in searcher.Get())
            {
                if (queryObj["PNPDeviceID"] != null && queryObj["Name"] != null)
                {
                    string pnpDeviceId = queryObj["PNPDeviceID"].ToString();
                    if (comPortAdvancedList.Where(o => o.Id == pnpDeviceId).Count() == 0)
                        comPortAdvancedList.Add(new AvailableComPort() { Id = pnpDeviceId, Name = queryObj["Name"].ToString() });
                }
            }
        }

        List<AvailableComPort> finalComPortList = new List<AvailableComPort>();
        foreach (var serialComPort in serialComPortList)
        {
            AvailableComPort comPortAdvanced = comPortAdvancedList.Where(o => o.Id.Contains("DEV_" + serialComPort.Id)).FirstOrDefault();
            if (comPortAdvanced != null)
            {
                comPortAdvanced.ComPort = serialComPort.ComPort;
                finalComPortList.Add(comPortAdvanced);
            }
        }
        return finalComPortList;
    }

public class AvailableComPort
{
    public string Id { get; set; }
    public string ComPort { get; set; }
    public string Name { get; set; }
}