UWP中SerialDevice.FromIdAsync无法返回SerialDevice问题求助
My Scenario
I'm building a UWP app for Windows 10 v1809 that needs to list available serial ports. I've added the Serial Communication capability to the app manifest, but when I call await SerialDevice.FromIdAsync(item.Id), it always returns null.
Here's the code I'm using to get port names:
private async void GetPortNames() { string deviceSelector = SerialDevice.GetDeviceSelector(); DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(deviceSelector); List<string> portNamesList = new List<string>(); foreach (var item in deviceCollection) { SerialDevice serialDevice = await SerialDevice.FromIdAsync(item.Id); string portName = serialDevice.PortName; portNamesList.Add(portName); } }
I've already tried two approaches without luck:
- Created a .NET Standard 2.0 library with
Microsoft.Windows.Compatibilityreferenced in the UWP app, but couldn't useSerialPort.GetPortNames() - Implemented the RJCP.IO library, which didn't fix the issue
Update: I suspect this is tied to the Panasonic Toughbook FZ-M1's hardware/architecture. SerialDevice.FromIDAsync(item.Id) returns null even though item.Id points to the correct registry entry. Strangely, item.Name shows the machine name ("DESKTOP-QR####") instead of the expected "COM##" label.
Troubleshooting Tips & Potential Fixes
Based on the details shared, here are targeted steps to resolve this:
Check for UWP-compatible serial drivers
Panasonic Toughbooks often use custom drivers for their integrated serial ports. Open Device Manager, locate your serial port, go to Properties > Driver, and confirm the driver is built for UWP (not just desktop). If not, download the latest UWP-specific driver from Panasonic's support site for the FZ-M1—older desktop drivers can block the UWP SerialDevice API from accessing the port.Validate device access permissions
Even with the Serial Communication capability enabled, UWP apps might need explicit user consent. Go to Settings > Privacy > Devices and make sure your app has permission to access serial devices. Also, try running the app as administrator—enterprise-grade hardware like the FZ-M1 sometimes enforces stricter access controls that require elevated privileges.Filter devices with the correct interface GUID
The defaultSerialDevice.GetDeviceSelector()might be picking up non-serial devices (which explains the machine name showing up initem.Name). Instead, use the explicit serial port interface class GUID to filter results:// Use the official serial port interface GUID to target only valid COM ports string serialInterfaceGuid = "{86E0D1E0-8089-11D0-9CE4-08003E301F73}"; string deviceSelector = $"System.Devices.InterfaceClassGuid:=\"{serialInterfaceGuid}\""; DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(deviceSelector);This ensures you're only enumerating actual serial port devices.
Try the Desktop Bridge (Centennial) approach
If the UWP SerialDevice API is incompatible with the FZ-M1's hardware, consider wrapping a desktop app (WPF/WinForms) that usesSerialPort.GetPortNames()(with theMicrosoft.Windows.Compatibilitypackage) into a UWP package via the Desktop Bridge. This lets you use the more reliable desktop serial APIs while still distributing as a UWP app.Patch Win10 1809 for known serial bugs
Windows 10 v1809 has documented issues with serial device enumeration in UWP. Install all available cumulative updates for this version to fix any API-level bugs. If possible, test the app on a newer Win10 release (like 20H2+) to confirm if the issue is version-specific or hardware-related.
内容的提问来源于stack exchange,提问作者Jeffrey M

