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

RFID Reader/Write (13.56khz - Mifare) on Netduino

rfid 13.56khz netduino2

  • Please log in to reply
6 replies to this topic

#1 Daniel Minnaar

Daniel Minnaar

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts
  • LocationJohannesburg, South Africa

Posted 08 May 2013 - 08:14 AM

Hi there,

 

After using the 125khz readers on the Netduino, I decided I want to write data to the tag, which forces me to use 13.56khz tags (Mifare) and appropriate reader/writers.

 

Initially I bought a NFC shield, which was a disaster because I couldn't get the SPI driver to work with Netduino. I've read multiple threads about it and no-one has been able to get it right with a Netduino. So I decided to get a new reader/writer using UART comms instead.

 

The problem is that the UART commands are all in hexadecimal format and I don't have much experience working with hex conversion into bytes to send over UART...

 

 

So a few questions before I start:

 


    [*]If I wanted to send 0x01 as a command to search for cards, could I simply do this? serialPort.Write(Text.Encoding.UTF8.GetBytes("0x01"), 0, theLength);
    [*]If I needed to encode something as hex, such as string or number (i.e "Hello World", or "12345"), how do I do this? Is there a library or method extension out there I can use to convert it?
    [*]At a first glance, would this reader/writer work with the Netduino 2? I see it uses a 4-pin wire, but I can just put in some solder wire tips and connect it to the headers: http://www.elechouse...roducts_id=2156
    [*]If I have 1k space on the Mifare Card, how can I calculate how much space "Helllo World" or "123456" would take in hex format on the card? I'll have a few variables i want to push into the card so I need to make sure it will all fit.
    [/list]

     

    I'd REALLY appreciate some help, it's for a project at work and I need to make sure of my facts. Thanks a ton!


  • Thomasraw likes this

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 08 May 2013 - 10:31 AM

I've played around some with the SM130 Mifare module from SonMicro and didn't have any problems. Maybe that's not the one you got though.

 

In general hexadecimal formatting is merely used as a notation on paper and in programming languages. If you write 0x80 in C# (and C/C++) the actual value will be 128 decimal as it would if you wrote 128 in the first place.

 

In C# you can use hexadecimal notation freely so you should not need any conversion - unless of course the hexadecimal notation is in a string, then you need to convert it.

 

For single bytes, this can be done quite easily back and forth using (in analogy with the above example) 128.ToString("X2") and byte.Parse("0x80", NumberStyles.AllowHexSpecifier) functions respectively.

 

I don't know of a smart way to convert whole strings, but you could of course always traverse the array and assemble a string as you go using the mentioned functions.

 

This methods below are supposed to translate byte arrays into strings with 8 bit space separated hexadecimal numbers and vice versa. This is from the top of my head so you may need to alter it a bit if it does not compile as is:

public class HexConversion{   private static string _hex = "0123456789ABCDEF";   public static string toHex(byte[] b)   {      string s = "";      for(int i = 0; i < b.length; i++, s += " ")         s += _hex[b[i] >> 4] + _hex[b[i] & 0xf];      return s;   }   public static byte[] fromHex(string s)   {     string[] a = s.Split(' ');     byte[] b = new byte[a.Length];     for(int i = 0; i < b.length; i++)        b[i] = byte.Parse(a[i], NumberStyles.AllowHexSpecifier);     return b;   }}

It could be you need to add the "0x" prefix in both the functions above. You would then use these functions like so:

// convert a regular string to a hex stringstring hex = HexConversion.toHex(Encoding.ASCII.GetBytes("Hello world"));// convert a hex string into a byte arraybyte[] bytes = HexConversion.fromHex(hex);// convert a byte array to a hex stringhex = HexConversion.toHex(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});

  • Thomasraw likes this

#3 Daniel Minnaar

Daniel Minnaar

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts
  • LocationJohannesburg, South Africa

Posted 09 May 2013 - 04:50 AM

 

I've played around some with the SM130 Mifare module from SonMicro and didn't have any problems. Maybe that's not the one you got though.

 

In general hexadecimal formatting is merely used as a notation on paper and in programming languages. If you write 0x80 in C# (and C/C++) the actual value will be 128 decimal as it would if you wrote 128 in the first place.

 

In C# you can use hexadecimal notation freely so you should not need any conversion - unless of course the hexadecimal notation is in a string, then you need to convert it.

 

For single bytes, this can be done quite easily back and forth using (in analogy with the above example) 128.ToString("X2") and byte.Parse("0x80", NumberStyles.AllowHexSpecifier) functions respectively.

 

I don't know of a smart way to convert whole strings, but you could of course always traverse the array and assemble a string as you go using the mentioned functions.

 

This methods below are supposed to translate byte arrays into strings with 8 bit space separated hexadecimal numbers and vice versa. This is from the top of my head so you may need to alter it a bit if it does not compile as is:

public class HexConversion{   private static string _hex = "0123456789ABCDEF";   public static string toHex(byte[] 
B) { string s = ""; for(int i = 0; i < b.length; i++, s += " ") s += _hex[b[i] >> 4] + _hex[b[i] & 0xf]; return s; } public static byte[] fromHex(string s) { string[] a = s.Split(' '); byte[] b = new byte[a.Length]; for(int i = 0; i < b.length; i++) b[i] = byte.Parse(a[i], NumberStyles.AllowHexSpecifier); return b; } }

It could be you need to add the "0x" prefix in both the functions above. You would then use these functions like so:

// convert a regular string to a hex stringstring hex = HexConversion.toHex(Encoding.ASCII.GetBytes("Hello world"));// convert a hex string into a byte arraybyte[] bytes = HexConversion.fromHex(hex);// convert a byte array to a hex stringhex = HexConversion.toHex(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});

 

Hanzibal, is there a chance you could share the reading and writing code for the SM130 module? I think I might go with this if I have some proven code to go with... thanks!


  • Thomasraw likes this

#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 09 May 2013 - 07:19 AM

The code was for PC with the module attached to kind of a USB to serial converter so it might actually be of pretty limited use to you. Also, I just made a few simple tests, basically only detecting tags coming into the field but I'll try and dig it out for you.

This is the one I was using:
http://www.sonmicro....id=57&Itemid=70

Which module are you using?

#5 Daniel Minnaar

Daniel Minnaar

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts
  • LocationJohannesburg, South Africa

Posted 09 May 2013 - 10:40 AM

The code was for PC with the module attached to kind of a USB to serial converter so it might actually be of pretty limited use to you. Also, I just made a few simple tests, basically only detecting tags coming into the field but I'll try and dig it out for you.

This is the one I was using:
http://www.sonmicro....id=57&Itemid=70

Which module are you using?

So you haven't used it on a Netduino yet?

 

I'm going for this one: http://www.elechouse...roducts_id=2156

 

Hopefully I can figure out these damn hex commands...



#6 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 09 May 2013 - 12:00 PM

I used to work with serial RFID devices on a daily basis and actually had all the main (MiFare, Prox, legic etc.) RS232 based readers working fine with a ND.

 

First of all can you connect the reader to a serial port on your PC and read the tags serial number into hyper terminal/putty?

That will help in determining if the reader has been set up correctly.

 

Once that is proven working the hex conversion is pretty straightforward (i will post some example code when i get home from the office).

You will often find that if the device returns text to hyper terminal i.e. you present the tag and the serial number is displayed then the hex you can see in the received comms is actually ascii codes.

 

i would recommend using the Netmf Toolbox on codeplex and the Chars2Bytes & Bytes2Chars functions which will take care of the conversion for you, i.e. if you have your serial comms set up right on your ND feeding the response into Bytes2Chars should give youa a char array of the ascii values returned.

 

Nak.



#7 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 09 May 2013 - 03:50 PM

...Once that is proven working the hex conversion is pretty straightforward (i will post some example code when i get home from the office). You will often find that if the device returns text to hyper terminal i.e. you present the tag and the serial number is displayed then the hex you can see in the received comms is actually ascii codes. i would recommend using the Netmf Toolbox on codeplex and the Chars2Bytes & Bytes2Chars functions which will take care of the conversion for you, i.e. if you have your serial comms set up right on your ND feeding the response into Bytes2Chars should give youa a char array of the ascii values returned.

Good, you seem more experienced in this but what's so wrong with the hex conversion code I supplied before?





Also tagged with one or more of these keywords: rfid, 13.56khz, netduino2

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.