确认串口是否为正常的COM或SPP
本文关键字:COM SPP 串口 是否 确认 | 更新日期: 2023-09-27 18:18:11
我正在寻找一种方法来确定COM是标准COM还是SPP COM,也称为COM设备的电缆替换蓝牙适配器。
我有一个设备,它可以在USB (COM -> USB)和蓝牙中工作,蓝牙接口与SPP一起工作。
我目前使用System.IO.Ports.SerialPort.GetPortNames()
来获取COMs。
是否有办法确定它是否与蓝牙或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
您无法通过SerialPort类找到此信息。你需要做一个WMI查询。
按照这条线做某事可能会把你引向它
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select *
from WIN32_SerialPort");
foreach(ManagementObject Port in searcher.Get()) {
string a = (string) Port.GetPropertyValue("Name");
}
我还没有加载这段代码,所以我不知道你可以获得什么进一步的属性。然而,如果有的话,WMI将是实现它的方法。
我看到你在看蓝牙连接设备:
查询Win32_PnPSignedDriver并查看InfName属性。该值应为bthspp.inf
我不能肯定地说,对于支持SPP协议的每个厂商的蓝牙设备,inf文件总是这个名称,但这是默认的。
COM &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%'
);
也许这对某人有帮助。代码将返回所有可用的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; }
}