ID-12/ID-20 RFID Reader Driver - Page 2 - Project Showcase - Netduino Forums
   
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

ID-12/ID-20 RFID Reader Driver


  • Please log in to reply
61 replies to this topic

#21 Hinnie

Hinnie

    New Member

  • Members
  • Pip
  • 9 posts

Posted 03 March 2011 - 10:19 PM

Trey, I saw in your example you rfid code yields a large number, you will get overflow. This allows you to better "uint" instead of "int" use. A small comment to your code RFid.cs. In your code comments you talking about the RFID code "62E3086CED08" whose "E3086CED" is used. Jotting this decimal 60717296877, according to the corrected code must be 3808980205.

#22 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 03 March 2011 - 11:28 PM

Hinnie, Yeah, that code '62E3086CED08' was copied from http://garden.seeeds...ID_Card_Reader. Which is a very large decimal number. Might be an example rather than from an actual card. My code is actually 450052C2FE2B. Which works fine with your code. -Trey

#23 kenNET

kenNET

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts
  • LocationSweden

Posted 11 March 2011 - 10:59 PM

I have some strange result with the "hex convert", some cards work but some don't... A card that works have "0001640297" printed on it, the card give me "3e00190769" (without checksum) and that translate back to "1640297" so that is correct. Another card have "0012290963" printed on it, the card give me "1d00bb8b93". But that converts to: "284131219". Same with this: Printed "0012286404", RFID: "1d00bb79c4", convert back: "284131316". I have no checksum since I use this RFID reader: http://www.sparkfun.com/products/8856 I have tested with a RFID reader that post ASCII, and all readings with that reader have same as on the card. Any suggestions? /Ken

#24 kenNET

kenNET

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts
  • LocationSweden

Posted 11 March 2011 - 11:01 PM

I have some strange result with the "hex convert", some cards work but some don't...


OK, I solved it... "1d00bb79c4" must be "1D00BB79C4", no lowercase!! Thanks for a great piece of code!

#25 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 11 March 2011 - 11:51 PM

OK, I solved it... "1d00bb79c4" must be "1D00BB79C4", no lowercase!! Thanks for a great piece of code!


Nice catch.
Maybe change this line.
sa = s.ToCharArray();
to
sa = s.ToUpper().ToCharArray();

I have not tested this yet.

#26 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 07 April 2011 - 10:04 PM

That you for sharing this class.
First time using the Netduino and loving it.

I'm having some problems getting it to work.
The card says it is reading data, but it is just returning all 255's.
Card scanned: ÿÿÿÿÿÿÿÿÿ, time: 01/01/2009 00:36:19
I have tried putting together just a simple serial reader and end up with the same output.
I can't seem to figure out why I'm not getting a valid RFID.
I have two cards and I'm pretty sure the code for both are not ÿÿÿÿÿÿÿÿÿ.


I'm pretty much using the class the same way you specify.
I coped the wiring from some other thread I found.
Pins:
11,2 = 5v
1,7 = gnd
10 = led
9 = Netduino pin 1
Hopefully I have something wired up wrong, I would hate to think i have a bad ID-20.

Thanks,
Trey Aughenbaugh


Trey, by accident I reproduced this by connecting pin 7, FS to 5v. This hopefully might of help to someone one day :)

#27 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 07 April 2011 - 10:33 PM

Guys, are you running regular Netduino, Plus or Mini? I have been struggling getting consistent readings with my ID-20. So far I have managed to isolate these factors: I have tested on - 2 different N+'s, - 2 different ID-20's, - 4 different sets of code (my own, the two libs posted in this thread and one from another thread), - 3 different breadboards, - several different sets of wires. Every run starts with the first reading being ok, then symptoms include; - DataReceived being fired more than once, - bytes separated. (See log) Now, lately I have been running 4.1.1.0_Alpha7 and 4.1.1.0_Beta1 but I figured I would try an older version. 4.1.0.6 actually seems to produce slightly more consistent results, see log. Output: With 4.1.1.0_Alpha7 and 4.1.1.0_Beta1 it might look like this: Card scanned: 3D004BEA0B97, time: 01/01/2009 00:04:04 Card scanned: , time: 01/01/2009 00:05:52 Card scanned: D004BEA0B97 , time: 01/01/2009 00:07:17 Card scanned: , time: 01/01/2009 00:08:24 Card scanned: D004BEA0B97 , time: 01/01/2009 00:08:28 Card scanned: 3D004BEA0B97 Card scanned: 3D004BEA0 Card scanned: 97 Card scanned:  Card scanned: 3D00 Card scanned: BE Card scanned: 0B9 Card scanned: With 4.1.0.6 it now might look like this: Card scanned: 3D004BEA0B97 Card scanned: 3D004BEA0B97 Card scanned: 3D004BE Card scanned: 0B9 Card scanned: Card scanned: Card scanned: 3D004BEA0B97 Card scanned: 3D004BEA0B Card scanned: 7 Card scanned: Card scanned: 3D004BEA0B97 Card scanned: So, I am wondering what hardware and firmware you have made this work with? I will try even older firmware, any other suggestions? I do not own a oscilloscope or an logic analyzer so I do not know how I could investigate data transmission and timing. Wiring using sparkfun breakout board: 5v: pin 11(5V) ground: pin 1(GND), pin 7(FS - what is this?) D4: pin 2(RST) D0: pin 9 (D0) I have tried with w/o led with resistor and permanent reset, no difference. Led signals correctly each time. Regards, Eivind

#28 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 07 April 2011 - 11:02 PM

See if you get better results with this class.

I'm running a Netduino Plus.
I believe I had the same problem as you until I added some minor changes to the original class.
Hopefully it will work ok for you.

Nice catch on the pin as well, it is possible that was my problem.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO.Ports;
using System.Threading;

namespace IDx2Reader
{

    public class idReader
    {
        //(** new **)
        #region Public Event

        public delegate void RfidEventDelegate(object sender, RfidEventArgs e);

        public event RfidEventDelegate RfidEvent;

        public class RfidEventArgs : EventArgs
        {
            private string _RFID;
            private DateTime _ReadTime;

            public string RFID
            {
                get { return _RFID; }
                set { _RFID = value; }
            }

            public DateTime ReadTime
            {
                get { return _ReadTime; }
                set { _ReadTime = value; }
            }

        }

        private void OnRfidEvent(RfidEventArgs e)
        {
            if (RfidEvent != null)
            {
                RfidEvent(this, e);
            }
        }

        #endregion

        private SerialPort rfidPort; // Serial port the reader is connected on.
        private SerialDataReceivedEventHandler _serialReadEvent; // Serial event handler.
        private OutputPort rfidResetPort; // Digital port the reader's reset pin is connected to.
        private int readDelay; // How long to wait between card reads.
        private bool _readerRunning = false; // State of the reader.

        // Buffers for incoming card data.
        byte[] rawData = new byte[16]; // Incoming raw bytes from the serial connection.
        char[] idStringData = new char[12]; // ID String data.

        public idReader(string _rfidSerialPort, Cpu.Pin _rfidResetPin, int _readDelay = 500)
        {
            // Set up the ports as necessary.
            rfidPort = new SerialPort(_rfidSerialPort, 9600, Parity.None, 8, StopBits.One);
            rfidResetPort = new OutputPort(_rfidResetPin, false);

            // Set up the event handler.
            _serialReadEvent = new SerialDataReceivedEventHandler(_rfidCardDataReceived);

            // Open the port and set an event handler to receive incoming serial data.
            rfidPort.Open();
            rfidPort.DataReceived += _serialReadEvent;

            // Store other passed parameters.
            readDelay = _readDelay;
        }

        ~idReader()
        {
            // Disable the reader.
            if (_readerRunning) rfidResetPort.Write(false);

            // Close the serial port.
            rfidPort.Close();

            // Detach the event handler.
            rfidPort.DataReceived -= _serialReadEvent;

            // GC will do the rest. (???)
        }

        // Start watching for a card.
        public void Start()
        {
            // Do nothing if the reader is already running.
            if (_readerRunning) return;

            // Release the reader from the reset state.
            rfidResetPort.Write(true);

            // Open the callback function to receive data and handle it.
            _readerRunning = true;
        }

        // Stop watching for a card.
        public void stop()
        {
            // Do nothing if the reader is already stopped.
            if (!_readerRunning) return;

            // Lock the reader to the reset state.
            rfidResetPort.Write(false);

            // Close the callback function.
            _readerRunning = false;
        }

        // Property to determine the reader's state.
        public bool isRunning
        {
            get { return _readerRunning; }
        }

        /* This method receives card data from the RFID reader via serial callback. */
        private void _rfidCardDataReceived(object _cardPort, SerialDataReceivedEventArgs unused)
        {
            // Do nothing if the reader is not supposed to be running.
            if (!_readerRunning) return;

            // Capture the port that the data was received on.
            SerialPort cardPort = (SerialPort)_cardPort;

            String cardID = String.Empty;
            int GPStoRead = cardPort.BytesToRead;
            if (GPStoRead == 16)
            {
                cardPort.Read(rawData, 0, rawData.Length);
                for (int i = 0; i < idStringData.Length; i++)
                {
                    idStringData[i] = (char)rawData[i + 1];   // skip first STX char 
                }
                String str = new string(idStringData);
                if (str.Length == 12)
                {
                    int[] chk = new int[6];
                    chk[0] = hexConvert(str.Substring(0, 2));
                    chk[1] = hexConvert(str.Substring(2, 2));
                    chk[2] = hexConvert(str.Substring(4, 2));
                    chk[3] = hexConvert(str.Substring(6, 2));
                    chk[4] = hexConvert(str.Substring(8, 2));
                    chk[5] = hexConvert(str.Substring(10, 2));
                    if ((chk[0] ^ chk[1] ^ chk[2] ^ chk[3] ^ chk[4]) == chk[5])
                    {
                        //String finishedID = new String(idStringData);
                        cardPort.Write(rawData, 0, rawData.Length);
                        RfidEventArgs _args = new RfidEventArgs();
                        _args.RFID = _args.RFID = (((chk[1] << 8 | chk[2]) << 8 | chk[3]) << 8 | chk[4]).ToString(); //((chk[2] << 8 | chk[3]) << 8 | chk[4]).ToString();
                        _args.ReadTime = DateTime.Now;
                        OnRfidEvent(_args);
                    }
                }

            }
        }


        public byte hexNibble(string s)
        {
            char[] hexValue = new char[16] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            byte b = 0xFF;
            char[] sa = new char[1];
            sa = s.ToCharArray();
            for (int i = 0; i <= 15; i++)
            {
                if (sa[0] == hexValue[i])
                {
                    b = (byte)i;
                    break;
                }
            }
            return b;
        }

        public int hexConvert(string hex)
        {
            return (hexNibble(hex.Substring(0, 1)) << 4) + hexNibble(hex.Substring(1, 1));
        }

    }
}


#29 priya123

priya123

    New Member

  • Members
  • Pip
  • 1 posts

Posted 08 April 2011 - 04:42 AM

Hi I am interfacing RFID ID2 with arduino pro mini., but my code doesn seem to work. Even the sample codes which I downloaded from the net is not working. I have connected the RFID ID2 with 125khz RFID coil antenna-700uH (http://www.farnell.c...heets/44764.pdf) I am not sure if there is a problem with the antenna compatibility. Do i need to tune the antenna, but I was told it is pre-tuned. Could you kindly tell me why the RFID ID2 is not reading the tag. my code is as follows: char val = 0; // variable to store the data from the serial port void setup() { Serial.begin(9600); // connect to the serial port } void loop () { // read the serial port if(Serial.available() > 0) { Serial.print("Waiting for the tag"); val = Serial.read(); Serial.print(val, BYTE); } }

#30 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 08 April 2011 - 05:20 AM

Hi priya123,

For help with your Arduino Pro Mini, I'd recommend posting in one of the following two forums:

SparkFun Arduino Forum (makers of the Arduino Pro Mini)
Arduino Forum

Chris

#31 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 08 April 2011 - 05:26 PM

...
Output:
...
Card scanned: 3D004BEA0B97
Card scanned: 3D004BEA0
Card scanned: 97
Card scanned:

Card scanned: 3D00
Card scanned: BE
Card scanned: 0B9


Eivind, I had the same issues as you, I don't have my code in front of me, but I changed the dataReceived method to pause for 100 ms and check for more data. If I remember when I get home, I'll post the code I'm using.

#32 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 08 April 2011 - 05:52 PM

Eivind, I had the same issues as you, I don't have my code in front of me, but I changed the dataReceived method to pause for 100 ms and check for more data. If I remember when I get home, I'll post the code I'm using.


Have you tried it with my code updates from above yet?
Your data is still in the format of text and based on Hinnie your card ID should actually be numeric, after it has been converted. Of course, either way it will be a unique card ID.
My update just included the Hinnie's formula for conversion along with a check to make sure it had a valid scan.

Basically this line:
            int GPStoRead = cardPort.BytesToRead;
            if (GPStoRead == 16){
This makes sure that it doesn't trigger an OnRfidEvent(_args) event unless all card data was received.

Card scanned: 3D004BEA0B97
Should be
Card scanned: 4975115 or something like that.

Anyway, since I made the above changes, I have not had any problems with card scanning.
I'm now having some I2C bus issues I think, but that is a whole other thread!
I will try your 100ms delay because it may help with my other issue.

-Trey

#33 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 08 April 2011 - 07:28 PM

Trey, thanks a lot! You have no idea how much this has been bugging me. So I guess BytesToRead is the clue here? Also thanks for the work done parsing, extracting and converting the actual ID.

#34 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 08 April 2011 - 07:30 PM

Have you tried it with my code updates from above yet?


I actually wrote my own RFID class way back when so I haven't used your code changes. I have used a few ideas from your class though (can't recall them off the top of my head).

-dan

#35 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 08 April 2011 - 07:47 PM

My best guess is that DataReceived is raised and handled before the data stream is complete. As the event is handled and data keeps being transmitted I guess the event keeps being raised. If so, both Trey's and Dan's approach seem valid. Gonna come back to studying the actual data after I have set up a WCF+rest service with db.

#36 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 08 April 2011 - 08:01 PM

Trey, thanks a lot! You have no idea how much this has been bugging me. So I guess BytesToRead is the clue here? Also thanks for the work done parsing, extracting and converting the actual ID.


Oh yeah, it was bugging me a few weeks ago!

I think the BytesToRead is it.
I can't take credit for the initial work here, Charles wrote the initial class and its Hinnie's conversion formula.

This line "int GPStoRead = cardPort.BytesToRead; ", I obviously found in some GPS tutorial!
Man, I wrote nothing here. :)

#37 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 08 April 2011 - 10:27 PM

This line "int GPStoRead = cardPort.BytesToRead; ", I obviously found in some GPS tutorial!


Lol, I was wondering what kind of fancy code scheme resulted in that variable name :)
I found an updated thread over here where similar solutions are suggested.

#38 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 09 April 2011 - 05:51 PM

Lol, I was wondering what kind of fancy code scheme resulted in that variable name :)
I found an updated thread over here where similar solutions are suggested.


Elvind,
Here is the code I use in the DataReceived method of my RfId reader class:

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
	var result = "";

	while(_serialPort.BytesToRead > 0)
	{
		var buffer = new byte[_serialPort.BytesToRead];
		_serialPort.Read(buffer, 0, buffer.Length);
		
		if (buffer[0] == 0)
		{
			Debug.Print("Received null data...");
			return;
		}

		try
		{
			result += new string(Encoding.UTF8.GetChars(buffer)).Trim();
		}
		catch (Exception)
		{
			return;
		}

		//Make sure we get all the data that is due us for the card
		Thread.Sleep(75);
	}

	result = RemoveInvalidChars(result);

	if (result != "")
		OnCardReadEvent(new CardReadEventArgs(result, DateTime.Now));
}


#39 haxburgen

haxburgen

    Member

  • Members
  • PipPip
  • 21 posts

Posted 06 November 2011 - 04:43 PM

Hi guys, thanks for the code! For some reason, I can only read one card though. Nothing is output after the first read.

#40 Trey Aughenbaugh

Trey Aughenbaugh

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 08 November 2011 - 03:37 AM

Hi guys, thanks for the code! For some reason, I can only read one card though. Nothing is output after the first read.


I would say post your code and possibly how you have it wired up.
Are you using the code at the beginning or some of the code found from this post.

http://forums.netdui...dpost__p__11780

-Trey




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.