2,892 questions
Hi @09849128 ,
Please check if adding the following lines to your Package.appxmanifest helps if your project is UWP.
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
Or consider moving serial port operations to a separate thread. You can use the class BackgroundWorker to achieve this.
Private WithEvents backgroundWorker As New BackgroundWorker
Private myport As SerialPort
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
backgroundWorker.RunWorkerAsync()
Frame1.Content = "Opening serial port..."
End Sub
Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork
myport = New SerialPort()
myport.PortName = "COM4"
myport.BaudRate = 9600
myport.Parity = Parity.None
myport.DataBits = 8
myport.StopBits = StopBits.One
Try
myport.Open()
myport.Write("A")
Catch ex As Exception
' Handle any exceptions here
Finally
If myport.IsOpen Then
myport.Close()
End If
End Try
End Sub
Private Sub backgroundWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles backgroundWorker.RunWorkerCompleted
Frame1.Content = "Change"
End Sub
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.