Domanda

My problem is when I run program it work perfectly fine but crashes after one cycle of the program. Here is some code:

    Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
    Dim RXByte As Byte
    key = 0
    cut = False
    c.Open()
    Do
        RXCnt = 0
        Do
            RXByte = COMPort.ReadByte
            If (RXByte = 29) Then
                c.Write(Chr(10))
                cut = True
                c.Close()
                If c.IsOpen = False Then
                        ts = "BLAH"
                        Process.Start(System.Windows.Forms.Application.StartupPath & "\e.exe", ts)
                        Process.Start(System.Windows.Forms.Application.StartupPath & "\Test.exe", c.PortName)
                Else

                End If

            End If
            If (cut = True) Then
                Exit Do
            End If
            c.Write(Chr(RXByte))
            addText(Chr(RXByte))
            RXArray(RXCnt) = LookUpTable(RXByte >> 4)
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = LookUpTable(RXByte And 15)
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = " "
            RXCnt = RXCnt + 1
            SpaceCount = (SpaceCount + 1) And 31      
            If SpaceCount = 0 Then                    
                RXArray(RXCnt) = Chr(13) ' CR
                RXCnt = RXCnt + 1
                RXArray(RXCnt) = Chr(10) ' LF
                RXCnt = RXCnt + 1
            Else
                If (SpaceCount And 3) = 0 Then        
                    RXArray(RXCnt) = " "
                    RXCnt = RXCnt + 1
                    RXArray(RXCnt) = " "
                    RXCnt = RXCnt + 1
                End If
            End If
        Loop Until (COMPort.BytesToRead = 0)
        Me.Invoke(New MethodInvoker(AddressOf Display)) 
    Loop Until (COMPort.BytesToRead = 0)  
    c.Close()
End Sub

The problem I believe occurs at this point in the code

Process.Start(System.Windows.Forms.Application.StartupPath & "\Test.exe", c.PortName)

The system throws a System.UnauthorizedAccessException error. Moreover the test program also communicates with the same serial port. When I run the program the process gets launched and completes but the current process throw the error out. Is it a UAC error?

È stato utile?

Soluzione

Two programs can't have the serial port open at the same time. Attempting to open a serial port throws that exception under these circumstances:

Access is denied to the port.

-or-

The current process, or another process on the system, already has the specified COM port open either by a SerialPort instance or in unmanaged code.

Source. So you can't have the serial port open in your program, and also in test.exe.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top