RFID reader YHY502CTG

Hi,

I’m trying to read a tag and write it at Monitor Serial.

My code works with the duemilanove, but it doesn’t work with mega.

With duemilanove I use port 2 for TX and port 3 for RX with Mega I’have tried port 14 for TX and port 15 for RX.

Could anybody please help?
Thanks a lot.

This is the code:


#include <SoftwareSerial.h>

//SoftwareSerial rfid(2, 3); // Duemilanove
SoftwareSerial rfid(14, 15); //Mega

int val = 0,i=0;
int ledPin = 13;
int status = 0;
unsigned char searchCMD = {0xAA, 0xBB,0x02, 0x20, 0x22};
unsigned char searchRES[4];

void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
rfid.begin(19200);
pinMode(13, OUTPUT);
}

int searchCard()
{
rfid.write(searchCMD, 5);
delay(100);
status = 0;

while(true){
if (rfid.available() > 0) {
val = rfid.read();

switch (status) {
case 0: // parse 0xAA
if (val == 0xAA)
status = 1;
break;
case 1: // parse 0xBB
if (val == 0xBB)
status = 2;
else return -1;
break;
case 2:
if (val == 0x06)
status = 3;
else return -1;
break;
case 3:
if (val == 0x20)
status = 4;
else return -1;
break;
case 4:
case 5:
case 6:
case 7:
searchRES[status - 4] = val;
status ++;
break;
case 8:
return 0; // read successfully
break;
default:
return -1;
break;
}
}
}
}

void loop()
{
if (searchCard() != -1) {
digitalWrite(13, HIGH);

for(i=0; i<5; i++){
Serial.print(searchRES*,HEX);*

  • Serial.print(",");*

  • }*

  • Serial.print("\n");*

  • delay(1000);*

  • digitalWrite(13, LOW);*

  • delay(1000);*

  • }*
    }
    -------------------------------------------------------------------------------

It worked changing to SoftwareSerial rfid(11, 10);

Since the Mega has four hardware serial ports, there's no need to use Software Serial.

How do you suggest it should be, wildbill?

Thank you for your attention.

How do you suggest it should be,

You should be using the hardware serial ports instead of software serial if you have a Mega.