Loading

C# SerialPort.Open() COM蓝牙串口阻塞问题

这种问题一般是蓝牙串口的问题,即使设置了SerialPort.TimeOut也是没有用的,需要验证串口名称,跳过该串口SerialPort.Open()步骤

这种有问题的蓝牙串口设备名称中会带有BTHENUM

那么我们就要取到串口名称,C#提供的SerialPort类只能取到串口编号,所以我们需要借助WMI取串口名称

private bool IsBTHENUM(string comName)
{
    string query = $"SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%({comName})%'";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ManagementObjectCollection devices = searcher.Get();
    bool result = false;
    foreach (ManagementObject device in devices)
    {
        string deviceID = device["DeviceID"] as string;
        if (deviceID.Contains("BTHENUM"))
        {
            result = true;
            break;
        }
    }
    return result;
}
posted @ 2024-01-22 14:17  Stay627  阅读(100)  评论(0编辑  收藏  举报