串行端口system.unauthorizedaccess

时间:2013-04-15 18:58:52

标签: vb.net serial-port unauthorizedaccessexcepti

我的问题是,当我运行程序时,它工作得非常好,但在程序的一个循环后崩溃。这是一些代码:

    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

我认为问题出现在代码

的这一点上
Process.Start(System.Windows.Forms.Application.StartupPath & "\Test.exe", c.PortName)

系统抛出System.UnauthorizedAccessException错误。此外,测试程序还与同一串行端口通信。当我运行程序时,进程启动并完成,但当前进程会抛出错误。这是UAC错误吗?

1 个答案:

答案 0 :(得分:1)

两个程序无法同时打开串口。在这些情况下,尝试打开串口会抛出该异常:

  

端口拒绝访问。

     

- 或 -

     

当前进程或系统上的其他进程已经通过SerialPort实例或非托管代码打开指定的COM端口。

Source。因此,您无法在程序中打开串口,也可以在test.exe

中打开串口