UWP串口UART测试VB.NET版本无法运行?C#转译后serialPort为空
serialPort Null Issue on Raspberry Pi 3 (Translated from Working C# Sample) Hey there! Let’s break down why your VB.NET UWP serial port code is ending up with a null serialPort variable on Raspberry Pi 3—especially since the original C# version works flawlessly. I’ll walk through the most common translation pitfalls and key areas to check:
1. Async/Await Syntax Misalignment
C# and VB.NET handle async code differently, and a misstep here can lead to race conditions where serialPort isn’t initialized before you try to use it.
Working C# Example:
private async Task<List<DeviceInformation>> FindSerialDevices() { string aqs = SerialDevice.GetDeviceSelector(); var devices = await DeviceInformation.FindAllAsync(aqs); return devices.ToList(); }
Correct VB.NET Translation:
Private Async Function FindSerialDevices() As Task(Of List(Of DeviceInformation)) Dim aqs As String = SerialDevice.GetDeviceSelector() Dim devices = Await DeviceInformation.FindAllAsync(aqs) Return devices.ToList() End Function
Check: Make sure the calling method is also marked Async, and you’re using Await (not blocking calls like .Result) when fetching device info. Skipping Await can cause your code to proceed before device enumeration finishes, leaving serialPort null.
2. Missing Null Checks for Selected Devices
It’s easy to overlook null checks when translating FirstOrDefault() from C# to VB.NET. If no devices are found, FirstOrDefault() returns Nothing—and calling FromIdAsync on a null device ID will result in a null serialPort.
Wrong VB.NET (No Null Check):
Dim selectedDevice = devices.First() ' Throws if no devices exist serialPort = Await SerialDevice.FromIdAsync(selectedDevice.Id)
Correct VB.NET:
Dim selectedDevice = devices.FirstOrDefault() If selectedDevice IsNot Nothing Then serialPort = Await SerialDevice.FromIdAsync(selectedDevice.Id) End If
Note: Even if a device is found, SerialDevice.FromIdAsync can return Nothing if your app lacks permissions or the port is already in use.
3. App Capabilities in Package.appxmanifest
This is a super common gotcha! UWP apps need explicit permissions to access serial ports—make sure your VB.NET app’s manifest matches the working C# version.
Add this under the <Capabilities> section (either via the Capabilities tab in Visual Studio or manually editing the XML):
<DeviceCapability Name="serialCommunication"> <Device Id="any"> <Function Type="name:serialPort" /> </Device> </DeviceCapability>
Check: Double-check that Serial Communication is enabled in the manifest UI, or verify the XML is present. Missing this capability will cause FromIdAsync to return null even if the device exists.
4. Variable Scope Mistakes
If you accidentally declared serialPort as a local variable inside a method instead of a class-level field, it will be null outside that method’s scope.
Correct Class-Level Declaration in VB.NET:
Private serialPort As SerialDevice ' Declare at the class level, not inside a method
5. Raspberry Pi UART Configuration
Even if the C# app works, ensure your Pi’s UART is properly enabled:
- Open
raspi-config> Interfacing Options > Serial, enable the serial port, and disable the serial console (so it’s available for your app). - Reboot the Pi after making changes—this is easy to forget!
6. Debugging Tips to Narrow It Down
Add debug logs to pinpoint where things go wrong:
Debug.WriteLine($"Total devices found: {devices.Count}") If selectedDevice IsNot Nothing Then Debug.WriteLine($"Selected device ID: {selectedDevice.Id}") serialPort = Await SerialDevice.FromIdAsync(selectedDevice.Id) Debug.WriteLine($"serialPort is null? {serialPort Is Nothing}") Else Debug.WriteLine("No serial devices found!") End If
This will tell you if the issue is with device enumeration, permission, or initialization.
内容的提问来源于stack exchange,提问作者LuckSaiber

