473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

System.UnauthorizedAccessException error

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.
Jan 13 '08 #1
1 6648
I'm having exactly the same error. Issue is reproducable if unplug and replug USB cable and stop my application (crash happens during application rundown).

It appears to be a subtle interaction between FTDI's drivers and Microsoft's SerialPort object.

Can you check to see if you are using FTDI device drivers to give you the virtual COM port? Support at FTDIchip.com told me the stuff below. Haven't tried because don't know where to put their code. I suppose I'd have to override the Dispose method for the Serial Port object and do something there.

How did you fix your problem?
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
You could use D2XX APIs to check the device presence and if it is not present close the USB handle or reload the device.

Code snippets are given in D2XX programmer's guide.

Please refer following APIs

FT_OpenEx and FT_Relaod. If FT_OpenEx return False you may call FT_Reload and try opening the handle again.

http://www.ftdichip.com/Documents/ProgramGuides/D2XX_Programmer's_Guide(FT_000071).pdf
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Nov 13 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Burton Roberts | last post by:
This is also posted in the security newsgroup. Sorry for cross-posting, but I'm desperate. I have a Winforms application in VB.NET with a SQL Server (MSDE) backend. In the root application...
1
by: Krazitchek | last post by:
Hi, i check all directories on a disk but an error occurs when i try to enter in the "System Volume Information" directory. Is there a way to "jump" this directory without to check the name of...
0
by: Efi | last post by:
Hi, We have a simple 3 tier application which its core application is VC++ 6.0 ATL COM running as a server application in the COM+. An asp pipe is in charge of handling the requests and passes it...
0
by: masago | last post by:
Hi....how are you ?? they can help me to solve this problem ?? Access to the path = "c:\windows\microsoft.net\framework\v1.1.4322\Temporary ASP.NET = Files\reports\06639073\bbab30a7" is...
4
by: 101 | last post by:
I get a security error when trying to write out to an XMLSchema file "myDs.WriteXmlSchema(strXMLSchemaFile)". I am running XP SP2, No Domain, MS file and print sharing uninstalled. I was able to...
4
by: Anbu | last post by:
Hi all, I have developed an application in VS .NET 2003 (framework 1.1) on a XP platform. It is working fine in that PC. Now, I setup new PC with same configuration for another develper....
0
by: nime | last post by:
I've got a problem. I cannot debug my app. which one contains WebBrowser control. I found a resolution but it's for an ASP related problem. I couldn't find a correct "user" to give permisson then...
5
by: Max | last post by:
hi i have file browser control to select any file and a button to upload file on my web page now when i select any file. now on click of upload button i have check that file exist or no if...
2
by: job | last post by:
In a sharepoint setup using smartpart to load our user controls using enterprise blocks (data) we are getting some strange errors (logged to the event log). We dont get the error all the time. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.