Go Down

Topic: ID-12 gives weird characters as output (Read 1 time) previous topic - next topic

Malala

May 03, 2012, 12:04 am Last Edit: May 03, 2012, 12:16 am by Malala Reason: 1
Hello,

I followed the following scheme to connect my ID-12. RX on Digital 5 and TX on Digital 6.


I am simply trying to let run the ID-12 using SoftwareSerial.
This is my code;

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(5, 6);
...
void setup()
{
 ...
 SoftSerial.begin(9600);
 ...
}

void loop()
{
 ...
   char tagString[13];
   int index = 0;
   byte tag;
   boolean reading = false;
   
   if (SoftSerial.available() > 0)
   {
     while (SoftSerial.available())
     {
       int readByte = SoftSerial.read();
       
       if (readByte == 2) reading = true;
       if (readByte == 3) reading = false;
       
       if (reading && readByte != 2 && readByte != 10 && readByte != 13)
       {
         tagString[index] = readByte;
         index++;
       }
     }
     
     Serial.write(tagString);
     Serial.println();
     
     // Check the tag.
     if (check_tag(tagString))
     {
        Serial.println("Accepted");
     }
     
     clear_tag(tagString);
   }
 ...
}


Weird enough, this outputs the following;


Oh and the check_tag() method returns false. This is basically a method that compares the tag of the RFID tag with a hard-coded char.

Anyone knows the problem?

Thanks in advance!

gr4jp3r

Hi, You're using Serial.write() instead of Serial.print(). Try using print to get ASCII coded output (write() returns a binary output). Additionally, if there's no data available at softSerial you'll get some random values at output.


Malala

That didn't work so I started over using the code from the Arduino playground. Tweaked it to use SoftwareSerial.
http://arduino.cc/playground/Code/ID12

Now I was trying to get the tag-bytes into a char. The following code displays the entire tag correctly in the Serial Monitor, but I do not seem to get it working to get that value into a char.

Code: [Select]
        if (bytesRead == 12)
        {
          Serial.print("6-byte code: ");
          for (i = 0; i < 6; i++)
          {
            if (code[i] < 16) Serial.print("0");
            Serial.print(code[i], HEX);
          }
         
          Serial.println();
        }


Even if I try
Code: [Select]
Serial.print(code); or
Code: [Select]
Serial.write(code); it does not work.

Is there any way to achieve what I am trying?

Thanks.

Nick Gammon

Quote
Is there any way to achieve what I am trying?


Yes, of course. How do you think all those RFID systems work?

Code: [Select]
  char tagString[13];
    int index = 0;
    byte tag;
    boolean reading = false;
   
    if (SoftSerial.available() > 0)
    {
      while (SoftSerial.available())
      {
        int readByte = SoftSerial.read();
       
        if (readByte == 2) reading = true;
        if (readByte == 3) reading = false;
       
        if (reading && readByte != 2 && readByte != 10 && readByte != 13)
        {
          tagString[index] = readByte;
          index++;
        }
      }
     
      Serial.write(tagString);
      Serial.println();
     


This is wrong on at least three counts.

First, you don't terminate the string with 0x00. Thus you get garbage at the end.

Second, you assume that after reading some bytes you have the entire tag. Which you almost certainly won't.

Third, you keep adding to tagString without checking if you have put more than 13 characters into it.

http://www.gammon.com.au/serial

Malala

Hello,

I know it is wrong and that's why, as I stated in my previous post, I started all over using the Playground's code.
So the 'is there any way to achieve this' was not meant for my first post, but the second.

So again, the question is;
Can I put the value of the code byte into a char in order to compare it with the hard-coded (a char array) allowed tags?

Nick Gammon


I know it is wrong and that's why, as I stated in my previous post, I started all over using the Playground's code.


It's hard to debug snippets so I looked at your original post.

Quote
Can I put the value of the code byte into a char in order to compare it with the hard-coded (a char array) allowed tags?


Maybe this will help:

http://www.gammon.com.au/forum/?id=11283

Malala

Great. Exactly what I was looking for and well described.

Keep up the good work, I will check your forum out!

Thank you muchly.

Go Up