Hello,
I am in the middle of creating an RFID reader library.
Because of the design of the RFID reader it allows two modes:
1. Detection: Reader waits for a card to be scanned and sends data Asychronously using serial.datareceived.
2. Standby: Reader expects a card to be present to read and execute command and return instant data synchronously
The problem is switching back and forth. When the library is initialized it is currently not connected to the rfid reader com port. Thus no serial object instantiated. Which is fine. Once the reader goes into detection mode a serial object is instantiated, initialized, and opened. This serial object being public to the class will stay open until standby mode it requested.
public void DetectionMode()
{
_serialPort = new SerialPort(_comPort, BAUD_RATE, Parity.None, 8, StopBits.One);
_serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
_serialPort.Open();
_serialPort.Write(new byte[1] { 0x01 }, 0, 1); // tells reader to notify me when a card is present.
_state = ReadState.DetectionMode;
}
The very first time the class uses detection mode, it works properly. All data gets received from the datareceived event handler.
However when the user needs to initiate standby mode to execute advanced functionality, the public serial object (above) is killed and a private is opened to grab the quick synchronous data. Standby mode means I need to disconnect and kill the public serial port like this...
if (_serialPort != null)
{
if (_serialPort.IsOpen) _serialPort.Close();
_serialPort.DataReceived -= serialPort_DataReceived;
_serialPort.DiscardInBuffer();
_serialPort.DiscardOutBuffer();
_serialPort.Dispose();
_serialPort = null;
}
I really kill this sucker!!!!!
Then I create a private serial port and immediately scan card for advanced reading. At this point I am good. Everything works as expected. Here is where it breaks...
If detection mode is requested again and the datareceived event handler is re-initiated, it won't ever fire again...
However I can re-instantiate a private serial object and use synchronous data transmission all day long. I just can't re-use the public one in asynchronous mode which I need for a card detection wait state.
Is this a bug or am I doing something wrong with closing the old public serial reference?
Serial framework Bug?
Started by Bendage, Jan 30 2012 10:38 PM
3 replies to this topic
#1
Posted 30 January 2012 - 10:38 PM
#2
Posted 30 January 2012 - 10:50 PM
When you create your private SerialPort object, is there a chance that it gets garbage collected (i.e. goes out of scope)?
You should be able to close and re-open a serial port an unlimited number of times.
Chris
#3
Posted 30 January 2012 - 11:15 PM
Chris,
It's not the private port I have a problem with.
Basically the problem is this...
A public serial port that uses the datareceived event handler, once the object is used, set to null, and reinstantiated, the datareceived event handler no longer fires even though its initialized and opened.
#4
Posted 30 January 2012 - 11:34 PM
Fixed!
Thread.Sleep is like duct tape!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users