Pages: [1]   Go Down
Author Topic: ID-12 gives weird characters as output  (Read 1475 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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!
« Last Edit: May 02, 2012, 05:16:50 pm by Malala » Logged

Poznań
Offline Offline
Newbie
*
Karma: 1
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
        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:
Serial.print(code);
or
Code:
Serial.write(code);
it does not work.

Is there any way to achieve what I am trying?

Thanks.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
  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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Thank you muchly.
Logged

Pages: [1]   Go Up
Jump to: