System.UnauthorizedAccessException error

J

JDS

I am getting the following error in my application:

System.UnauthorizedAccessException was unhandled
Message="Access to the port is denied."
Source="System"
StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32
errorCode, String str)
at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
at System.IO.Ports.SerialStream.Finalize()

The application uses a form to get (weight) data from an external
device on a USB connection mapped to a serial port. In normal
operation the form may be opened (ShowDialog) and closed several
times.

The sequence of events is as follows:
- device is disconnected whilst communicating (an error occurs then
but is handled in code)
- form closed
- device reconnected (i.e. USB physically plugged back in)
- form reopened and behaves normally (initially)
...
- error occurs (as above) but the timing is random, i.e. sometimes
soon after reconnection, sometimes whilst communicating with form
open, sometimes not; sometimes on first time form opened, sometimes
after several attempts

The form uses System.IO.Ports.SerialPort; the relevant code is as
follows (Try-Catch error handling removed):

'******* Selected form code *********
Public Shared WithEvents mPort As SerialPort
Private Shared m_FormDefInstance As frmWeigh

#Region "Form handling"
Public Shared Property DefInstance() As frmWeigh
...
Set(ByVal value As frmWeigh)
m_FormDefInstance = value
End Set
End Property

Private Sub frmWeigh_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
...
DefInstance = Me
...
OpenPort()
...
End Sub

Private Sub frmWeigh_FormClosing(ByVal sender As Object, ByVal e
As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
...
If Not IsNothing(mPort) Then
If mPort.IsOpen Then
mPort.Close()
End If
mPort = Nothing
End If
If Not IsNothing(m_FormDefInstance) Then
m_FormDefInstance = Nothing
End If
End Sub
#End Region

#Region "SerialPort"
Private Sub OpenPort()
...
mPort = New SerialPort()
mPort.Open()
mPort.DiscardInBuffer()
Me.Timer1.Enabled = True
Me.Timer1.Start()
...
End Sub
#End Region

#Region "Timer"
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
...
'Code to check status and send command to request data
...
End Sub
#End Region

#Region "Data handling"
Private Shared Sub mPort_DataReceived(ByVal sender As Object,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
mPort.DataReceived
Try
Dim Buffer As String = mPort.ReadExisting()
DefInstance.BeginInvoke(New DisplayData(AddressOf
Display), New Object() {Buffer})
End Sub

Public Delegate Sub DisplayData(ByVal Buffer As String)

Private Shared Sub Display(ByVal Buffer As String)
...
'Data processing code
...
End Sub
#End Region
'******* End of form code *********

From the calling code on error:
frmWeigh = Nothing
frmWeigh.Dispose

I have a feeling that it is because I am not destroying objects
correctly but I am not sure exactly what I am missing. Any help
greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top