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

jVa*_*ron 7 c# serial-port bluetooth

我正在寻找一种方法来确定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()));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出类似于:

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 
Run Code Online (Sandbox Code Playgroud)

Ada*_*ley 7

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

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

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

我没有加载此代码,所以我不知道你可以获得什么进一步的属性.但是,无论如何,WMI都是这样做的方式.