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

Parallax RFID Reader


  • Please log in to reply
28 replies to this topic

#21 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 July 2011 - 07:48 PM

I just got my Netduino board and RFID reader, and I wanted to test it out. I used afulki's class to do the reading. However I'm noticing an issue with noise readings. I'll read a badge and the Netduino sends a UDP packet to my computer and it flashes some information. But every once in a while a "tag" flashes up even though no badge came near the reader. Any suggestions would be appreciated. Thanks, TJ

#22 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 01 July 2011 - 08:20 PM

Hi Justinius, which chip is used on the RFID board?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#23 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 July 2011 - 08:29 PM

There is a PIC 16F627A uController and a EM4095 RFID chip

#24 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 02 July 2011 - 11:58 AM

ah too bad, that's not even close to my RFID reader; https://stefan.co/ne...30-rfid-reader/
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#25 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 05 July 2011 - 07:50 PM

No ideas? Using the class from Afulki I get spurious reads. The solution I think is to do two reads in a short period of time and compare the results. But with the interrupt I'm not sure thats feasible. Any ideas?

#26 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 06 July 2011 - 02:43 AM

I think I have a solution. I'm going to add some members variables to the class to preserve a state. On the first triggering it will parse as normal, but set a state variable. Then if the user keeps the card there long enough it will retrigger the event. I'll check the state and if the IDs match I'll certify that its a valid card. I'll post when I get it tested tomorrow.

#27 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 06 July 2011 - 11:32 PM

public class ParalaxRFID
    {
        private int _readDelay = 1500;
        private bool _readerEnabled = false;

        public SerialPort SerialPort { get; set; }
        public OutputPort RfidControl { get; set; }

        public delegate void CardReadEventHandler(ParalaxRFID sender, string cardID);
        public event CardReadEventHandler OnCardRead;

        public delegate void StatusChangedEventHandler(ParalaxRFID sender, bool readerEnabled);
        public event StatusChangedEventHandler OnStatusChanged;

        private byte[] _messageBuffer = new byte[10];
        private int _messageCounter = -1; // -1 = waiting for start char, 0 or more means reading message
        
        private string _cardID;
        private int _num_reads = 0;

        public int ReadDelay
        {
            get { return _readDelay; }
            set { _readDelay = value; }
        }

        public bool ReaderEnabled
        {
            get { return _readerEnabled; }
            private set 
            { 
                _readerEnabled = value;
                if (OnStatusChanged != null)
                {
                    OnStatusChanged(this, value);
                }
            }
        }
        
        public ParalaxRFID(string portName, Cpu.Pin controlPin, bool autoEnable = true)
            : this(new SerialPort(portName, 2400, Parity.None, 8, StopBits.One), new OutputPort(controlPin, false), autoEnable)
        {
        }

        public ParalaxRFID(SerialPort sPort, OutputPort rfidControl, bool autoEnable = true)
        {
            this.SerialPort = sPort;
            this.RfidControl = rfidControl;

            SerialPort.WriteTimeout = 1000;

            SerialPort.Open();
            SerialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
            
            if (autoEnable) EnableReader();
        }

        public void DisableReader()
        {
            RfidControl.Write(true);
            if (SerialPort.IsOpen) SerialPort.Close();
            ReaderEnabled = false;
        }

        public void EnableReader()
        {
            if (!SerialPort.IsOpen) SerialPort.Open();
            RfidControl.Write(false);
            ReaderEnabled = true;
        }

        private void DoReadDelay()
        {
            if (ReaderEnabled)
            {
                DisableReader();
                Thread.Sleep(ReadDelay);
                EnableReader();
            }
        }


        void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender; 

            while (sp.BytesToRead > 0)
            {
                // temp buff to hold message,
                byte[] buf = new byte[1];

                // Read a single byte from the recv buffer,
                if (sp.Read(buf, 0, 1) > 0)
                {
                    // Start of Message?
                    if (buf[0] == '\n') 
                    {
                        _messageCounter = 0;
                    }
                    // End of message?
                    else if (buf[0] == '\r')
                    {
                        if (_messageCounter > 9)
                        {
                            String cardID = String.Empty;
                            for (int i = 0; i < 10; i++)
                            {
                                cardID += (char)_messageBuffer[i];
                            }

                            if (_num_reads == 1 && OnCardRead != null)
                            {
                                _num_reads = 0;
                                if(card_ID == _cardID)
                                    OnCardRead(this, cardID);
                            }
                            else if(_num_reads == 0)
                            {
                                _num_reads++;
                                _cardID = cardID;
                            }
                        }

                        _messageCounter = -1;
                        DoReadDelay();
                    }
                    else // Store it in the global buffer, 
                    {
                        if (_messageCounter < 10)
                        {
                            _messageBuffer[_messageCounter++] = buf[0];
                        }
                        else
                            _messageCounter = -1;
                    }
                }
            }
        }
    }

I then changed the read delay member to a shorter time span. The user has to hold the card there longer, but it seems to work.

Although I have a question/comment about this solution. When I set the read delay to 100, I get an IndexOutOfRange Exception in the _messageBuffer[_messageCounter++] = buf[0]; line. If I change the read delay to 50, 150, 200, 500 I do not recieve this error. Any ideas?

#28 Anshul

Anshul

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 27 January 2012 - 05:43 AM

I thought Netduino doesn't support serial communication without an FTDI chip??

#29 Anshul

Anshul

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 27 January 2012 - 05:43 AM

I thought Netduino doesn't support serial communication without an FTDI chip??




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.