How do I fix a "Too many posts to a semaphore" error in a vb app?

09849128 20 Reputation points
2023-08-13T15:32:06.9166667+00:00
I am writing a vb application on a new Windows 11 Surface.  When the application opens the serial port, I receive a "Too many posts made to a semaphore" error.  I have followed all steps I have found on the subjects (disabled anti-virus, disabled all startup apps, running as administrator).  Any help would be greatly appreciated.    

 Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

        Dim myport As SerialPort

        myport = New SerialPort()

        myport.PortName = "COM4"   'Assign the port name to the MyCOMPort object

        myport.BaudRate = 9600      'Assign the Baudrate to the MyCOMPort object

        myport.Parity = Parity.None   'Parity bits = none  

        myport.DataBits = 8             'No of Data bits = 8

        myport.StopBits = StopBits.One  'No of Stop bits = 1

        myport.Open()

        myport.Write("A")           ' Write an ascii "A"

        myport.Close()

        Frame1.Content = "Change"

    End Sub
Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-08-14T07:13:32.0066667+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.