Question

This is my first SO question so please correct me where I am not supplying enough or correct information.

I'm interfacing two RFID tag readers via RS232 ports on a FTDI serial to USB hub to my program. The program runs perfectly outside VS2010, but I cannot debug the code. The program starts fine during debugging, but as soon as I bring a tag within reading proximity I get an error. This only happens during debugging.

The error:

Error Opening Serial Port COM9

System.UnauthorizedAccessException: Access to the port 'COM9' is denied.

   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)

   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)

   at System.IO.Ports.SerialPort.Open()

   at Tag_Reader.COMs.openPort(String sPortName) in C:\XXX\Tag_Reader\COMs.cs:line 36

COMs.cs:line 36:

 _port.PortName = sPortName;
 _port.BaudRate = 9600;
 _port.DataBits = 8;
 _port.Parity = Parity.None;
 _port.StopBits = StopBits.One;
 _port.ReadTimeout = 500;
 _port.WriteTimeout = 500;
 _port.Open();

I would appreciate any help! As mentioned above the programs runs without a glitch if I run the .exe outside VS.

EDIT: This error only occurs during debug and only when there is data being streamed from the tag reader (when a tag is brought within the reader's reading proximity). This indicates that the port setup is correct.

Was it helpful?

Solution

The exception is very specific, it only ever means one thing. The port is already opened, you cannot open it again. It might be another process, it could be your own.

A standard mistake you could make that makes it specific to a debug session is to use another program to look at the data sent by the port. A common thing to do, you'd want to compare the data displayed by such a program with the data you display to look for a mismatch. This cannot work, you start such a program before you start your own, it will open the port and your program will always fail. You need a different kind of utility to do this, a filter driver that injects itself ahead of the serial port driver and spies on the driver requests. SysInternals' PortMon is an example.

Any only when there is data being streamed from the tag reader

This comment is very troublesome, you must of course always open the port well before the tag reader starts streaming data. This hints at a very different kind of bug, ought to be visible from the full stack trace. Some scenario where the code in the DataReceived event handler causes the Open() method to be called again. Possibly well hidden due to a Begin/Invoke() call inside your DataReceived event handler.

Last but not least, do make sure that you never repeatedly call Close() and Open(). The Close() call doesn't actually close the serial port, it only starts to close it. An internal worker thread needs to exit, that takes time. Only ever call Close() at program termination.

OTHER TIPS

you can list all aviable comports with this:

List<string> portNames = SerialPort.GetPortNames().ToList();
                if (portNames.FirstOrDefault(p => p.ToLower() == "COM9".ToLower()) != null){
//Connect

}

when com9 is aviable you should check your parameters for the connection and then try connecting your port.

edit: SerialPort.GetPortNames only shows the aviable ports for your application. When any other Application blocks e.g. Com9 , only the other Comports will be shown

Have you thought about timing issues due to the RFID protocol handling?
I would try to check what kind of data flows to the host and back. I know that in some RFID protocols, timing is (nearly) everything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top