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.

buzz

Member Since 28 Sep 2011
Offline Last Active Jan 13 2015 06:52 AM
-----

Posts I've Made

In Topic: ID-12/ID-20 RFID Reader Driver

07 February 2013 - 05:01 PM

I've ordered more ID-20s recently and it seems the new batch (with "LV" prefix) from sparkfun exhibits weird issues.  You can read some of them in the ID-20 thread.  I've found that if you connect the reader to the 3v3 instead of 5v, it's almost 'normal'.  I just dont know if this is a good idea moving forward or how consistent it'll be.  I also saw another method using a resistor.


In Topic: ID-12/ID-20 RFID Reader Driver

18 January 2013 - 06:32 AM

Finally got my ID-20 working well.  A big THANK YOU to everyone here who contributed...this stuff is truly fascinating.

 

I plan to run this RFID scanner in production with ~50 employees on it.  So after much stress testing, I'd like to contribute a few changes to the datareceived method to ensure maximum reliability.  Particularly with ID-20 reading multiple cards in quick succession.  Basically the code can't crash since no one may be around reboot it.

 

Changes I made:

- hopefully almost crashproof

- moved rawdata to local scope

- removed args.ReadTime, args.RFID output is now int, renamed class

- better error state output using args.Success (ie: to display error led's to user)

- better handles multiple/quick reads...basically when BytesToRead is not a perfect 16

- found that hexConvert was not as efficient as direct parsing of the paired hex

- removed cardPort.Write - this seems to echo the input...not sure why this was necessary...it seems to run fine w/o it and it speeds up the code quite a bit

 

 

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;	// Buffers for incoming card data.	byte[] rawData = new byte[16]; // Incoming raw bytes from the serial connection.	RfidEventArgs args = new RfidEventArgs();	string cardID = String.Empty;	int bytesToRead = cardPort.BytesToRead;	if ( bytesToRead == 16 )	{		cardPort.Read( rawData, 0, rawData.Length );		int[] chk = new int[6];		chk[0] = HexAsciiToInt( rawData, 1 );		chk[1] = HexAsciiToInt( rawData, 3 );		chk[2] = HexAsciiToInt( rawData, 5 );		chk[3] = HexAsciiToInt( rawData, 7 );		chk[4] = HexAsciiToInt( rawData, 9 );		chk[5] = HexAsciiToInt( rawData, 11 );		if ( ( chk[0] ^ chk[1] ^ chk[2] ^ chk[3] ^ chk[4] ) == chk[5] )		{			args.Success = true;			args.RFID = ( ( ( chk[1] << 8 | chk[2] ) << 8 | chk[3] ) << 8 | chk[4] );			OnRfidEvent( args );		}		else		{			args.Success = false;			args.ErrorMsg = "Checksum mismatch on RFID: " + ( chk[0] ^ chk[1] ^ chk[2] ^ chk[3] ^ chk[4] ) + " vs " + chk[5];			OnRfidEvent( args );		}	}	else if ( bytesToRead > 16 ) // bytesToRead exceeds 16 (ie: too many cards read at same time, nuke everything)	{		// clear buffer		cardPort.DiscardInBuffer();		args.Success = false;		args.ErrorMsg = "Serial port buffer exceeds 16 bytes: " + bytesToRead;		OnRfidEvent( args );	}}public int HexAsciiToInt( byte[] data, int offset, int length = 2 ){	int ret = 0;	for ( int i = 0; i < length; i++ )	{		ret = ret * (int) System.Math.Pow( 16, i ); // each digit is another power of 16		if ( data[offset + i] >= 48 && data[offset + i] <= 57 )			// number			ret += data[offset + i] - 48;		else			// letter A - F			ret += data[offset + i] - 55;	}	return ret;}

 

 

The full RFIDReader.cs...usage is similar to program code posted earlier in the thread except the class is renamed:

 

Attached File  RFIDReader.cs   4.28KB   9 downloads

 


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.