2

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

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

4

1 回答 1

1

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

对端口的访问被拒绝。

-或者-

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

来源。所以你不能在你的程序中打开串口,也不能在test.exe.

于 2013-04-15T20:28:47.537 回答