By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

Serial Port UnauthorizedAccess exception

100+
P: 344
I'm working on simple chat program via serial port. It works fine, but i face this problem.
When i try to open the exe, UnauthorizedAccess exception occurs, i can't use COM1 becuase someone else has it. I checked it and found that another copy of the program still work (by task manager). That's mean closing the program will not close the serial port. I tried these :
Note: I read it using a separate thread.

1)override OnClose to, close the serial port.
But this evaluates an exception, where the thread is accessing the port and i terminate it.
2)override OnClose to, abort the thread then close the port.
A message that information cloud be lost appears.
3)override OnClose to, suspend the thread then close the port.
exception occurs just like (1).

I don't want to open the port and close it evey time i read or write, it'll be very ugly.
Feb 6 '09 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Please don't take offense at the title of the post. I'm not being picky about your grammar - But computers are VERY picky about it. Forms are closed. Applications are exited. It's important that you as the coder know the difference because you are only going to find what you look for. If you look for a means to close that is what you will find. If you know you need to look for a means by which to 'exit' then that is what you will find. Intellisense in Visual Studio will help you get what you ask for, not what you intend.
I checked it and found that another copy of the program still work (by task manager). That's mean closing the program will not close the serial port.
I don't think that's what it means. If you found the program still running via Task Manager, then it means you didn't really exit the application which is why the port was still in use. Once you used Task Manager to exit the still running application your serial port was closed and available to you, right? So exiting the application really does close the serial port. I'm going to guess that you coded what you described in your post "closing" not "exiting".Just because you don't have a form open doesn't mean a program isn't running.

I deal with serial ports all the time: Laser sensors, contact closure switches, custom USB->serial controllers etc. I can assure you that if all the threads of your application actually exit, then the serial port will close.

I'm going to guess that when your code think's its exiting the application it really isn't. Instead it may be only closing a form.
Could you show your code for when you exit the application?
If you have something like
Expand|Select|Wrap|Line Numbers
  1. this.close();
that is going to close the form, not exit the application. You want to use something like
Expand|Select|Wrap|Line Numbers
  1. application.exit();
instead.
Feb 7 '09 #2

100+
P: 344
Thanks a lot,
I only had to add an event handler to form_closing to close the port, and the thread try to access the port if exception it abort.
Thanks again for your help.
Regrads,
Bassem
Feb 8 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.