【问题标题】:Serial Port system.unauthorizedaccess串口系统.unauthorizedaccess
【发布时间】:2013-04-15 18:58:51
【问题描述】:

我的问题是,当我运行程序时,它运行良好,但在程序运行一个周期后崩溃。这是一些代码:

    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 错误吗?

【问题讨论】:

    标签: vb.net serial-port unauthorizedaccessexcepti


    【解决方案1】:

    两个程序不能同时打开串口。在以下情况下尝试打开串行端口会引发该异常:

    对端口的访问被拒绝。

    -或-

    当前进程或系统上的另一个进程已经通过 SerialPort 实例或在非托管代码中打开了指定的 COM 端口。

    Source。所以你不能在你的程序和test.exe中打开串口。

    【讨论】:

    • 好吧,如果你看看我在打开 test.exe 之前关闭端口的代码,那不应该让端口再次打开吗?
    • cCOMPort 是什么?两个不同的串口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2013-02-23
    • 2015-12-15
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多