Ce site contient essentiellement des notes de travail. Le contenu est en constante évolution, et loin d'être achevé. (+ d'infos)
La plupart des documentations informatiques sont orientées Debian / Ubuntu.

Electronique/Hardware/Divers/RFID/YHY502C/Arduino

De Ordinoscope.net
< Electronique‎ | Hardware‎ | Divers‎ | RFID‎ | YHY502C
Sauter à la navigation Sauter à la recherche
int val = 0;
int ledPin = 13;
int status = 0;
unsigned char searchCMD[] = {0xAA, 0xBB, 0x02, 0x20, 0x22};
unsigned char searchRES[4];

#include <NewSoftSerial.h>
NewSoftSerial mySerial (2, 3);

void setup () {
  Serial.begin (115200);
  Serial.println ("setup");
  mySerial.begin (19200);
  pinMode (13, OUTPUT);
}

int searchCard () {
  mySerial.print (searchCMD[0]);
  mySerial.print (searchCMD[1]);
  mySerial.print (searchCMD[2]);
  mySerial.print (searchCMD[3]);
  mySerial.print (searchCMD[4]);
  delay (100);
  status = 0;
  while (true) {
    if (mySerial.available () > 0) {
      val = mySerial.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) {
    Serial.print (searchRES[0], HEX);
    Serial.print (" ");
    Serial.print (searchRES[1], HEX);
    Serial.print (" ");
    Serial.print (searchRES[2], HEX);
    Serial.print (" ");
    Serial.println (searchRES[3], HEX);
    digitalWrite (13, HIGH);
    delay (1000);
    digitalWrite (13, LOW);
    delay (1000);
  }
}