2013-04-15 130 views
2

我的問題是,當我運行程序它工作得很好,但程序的一個週期後崩潰。下面是一些代碼:串行端口system.unauthorizedaccess

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

兩個程序不能同時打開串口。在這些情況下嘗試打開串口會引發該異常:

訪問被拒絕給端口。

- 或 -

當前過程,或在系統上的另一過程,已經有指定的COM端口或者由的SerialPort實例或在非託管代碼中打開。

Source。所以你不能在你的程序中打開串口,也可以在test.exe

+0

好,如果你看看代碼我打開test.exe之前關閉端口,所以不應該再次打開端口? –

+0

什麼是'c'和'COMPort'?兩個不同的串行端口? – dsolimano