Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Serial framework Bug?


  • Please log in to reply
3 replies to this topic

#1 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 30 January 2012 - 10:38 PM

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?

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

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 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

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 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

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

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.