ID-12/ID-20 RFID Reader Driver
#21
Posted 03 March 2011 - 10:19 PM
#22
Posted 03 March 2011 - 11:28 PM
#23
Posted 11 March 2011 - 10:59 PM
#24
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
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
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
Posted 07 April 2011 - 10:33 PM
#28
Posted 07 April 2011 - 11:02 PM
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
Posted 08 April 2011 - 04:42 AM
#30
Posted 08 April 2011 - 05:20 AM
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
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
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
Posted 08 April 2011 - 07:28 PM
#34
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
Posted 08 April 2011 - 07:47 PM
#36
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.
- Eivind likes this
#38
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
Posted 06 November 2011 - 04:43 PM
#40
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