regardez mon code d'origine ➜ la fonction lireTrame se débrouille toute seule, vous devez l'appeler tout le temps et simplement vérifier la valeur retournée par la fonction. si c'est true alors c'est que la trame a été lue correctement et alors vous pouvez l'imprimer
c'est pour cela que j'avais
if (lireTrame()) {
imprimer();
}
essayez avec ce code
#include <SoftwareSerial.h>
SoftwareSerial rfidSerial = SoftwareSerial(12, 13); // Rx, Tx
// Flux de test en HEXA // le même pour CoolTerm
// 0xAA, 0x0F, 0x08, 0x00, 0x02, 0x10, 0x34, 0x27, 0x80, 0x16, 0x06, 0x96, 0xBB // AA 0F 08 00 02 10 34 27 80 16 06 96 BB
// 0xAA, 0x0F, 0x08, 0x00, 0x02, 0X10, 0x00, 0x1D, 0xFC, 0xBE, 0x19, 0x00, 0xBB // AA 0F 08 00 02 10 00 1D FC BE 19 53 BB
// 0xAA, 0x0F, 0x08, 0x00, 0x02, 0x10, 0x00, 0x1D, 0xD2, 0x9F, 0xF9, 0x00, 0xBB // AA 0F 08 00 02 10 00 1D D2 9F F9 BC BB
struct TrameRFID
{
uint8_t pays[2]; // 2 octets pays (exemple 03 84)
uint8_t identifiant[5]; // 5 octets identifiant (exemple 12 DB FA E7 D5)
};
struct Animal
{
const char * nom;
const TrameRFID id;
};
TrameRFID trame;
// Une trame comporte les octets suivants:
// 1 octet marqueur début (0xAA)
// 10 octets données:
// 3 octets fixes (0x0F, 0x08, 0x00)
// 2 octets pays
// 5 octets tagID
// 2 octets d'épilogue
// 1 octet checksum
// 1 octet marqueur fin (0xBB)
Animal lesAnimaux[] =
{
{"Garfield", {{0x00, 0xFA}, {0x34, 0x27, 0x80, 0x16, 0x06}}},
{"Lucifer", {{0x00, 0xFA}, {0x00, 0x1D, 0xD2, 0x9F, 0xF9}}},
{"Felix", {{0x00, 0xFA}, {0x00, 0x1D, 0xFC, 0xBE, 0x19}}},
{"Feuvert", {{0x00, 0xFA}, {0x3E, 0xC4, 0xDD, 0x44, 0xF2}}},
{"Chanel", {{0x00, 0xFA}, {0x3E, 0xC4, 0xDD, 0x44, 0xF3}}},
};
const size_t nbAnimauxConnus = sizeof lesAnimaux / sizeof * lesAnimaux;
void imprimerOctet(uint8_t o)
{
Serial.print(F("0x"));
if (o < 0x10) Serial.write('0');
Serial.print(o, HEX);
Serial.write(' ');
}
char *ulltoa(uint64_t value, char *buf) {
char tmp[sizeof(uint64_t) * 8 + 1];
char *p1 = tmp, *p2;
static const char *xlat = "0123456789";
do {
*p1++ = xlat[value % 10u];
} while ((value /= 10u));
for (p2 = buf; p1 != tmp; *p2++ = *--p1) ;
*p2 = '\0';
return buf;
}
void imprimerTrame()
{
bool animalConnu = false;
uint16_t codePays = 0;
codePays = trame.pays[0]; codePays <<= 8;
codePays += trame.pays[1];
Serial.print(F("Pays : ")); Serial.println(codePays);
uint64_t rfid64 = 0;
rfid64 += trame.identifiant[0]; rfid64 <<= 8;
rfid64 += trame.identifiant[1]; rfid64 <<= 8;
rfid64 += trame.identifiant[2]; rfid64 <<= 8;
rfid64 += trame.identifiant[3]; rfid64 <<= 8;
rfid64 += trame.identifiant[4];
char rfid64Text[sizeof(uint64_t) * 8 + 1];
ulltoa(rfid64, rfid64Text);
Serial.print(F("ID : ")); Serial.println(rfid64Text);
for (size_t i = 0; i < nbAnimauxConnus; i++) {
if ((memcmp(lesAnimaux[i].id.pays, trame.pays, sizeof trame.pays) == 0) && (memcmp(lesAnimaux[i].id.identifiant, trame.identifiant, sizeof trame.identifiant) == 0)) {
animalConnu = true;
Serial.print(F("Animal reconnu : ")); Serial.println(lesAnimaux[i].nom);
break;
}
}
if (!animalConnu) Serial.println(F("Animal inconnu"));
}
bool nouvelleTrameRecue()
{
static enum {DEBUT, FIXE, PAYS, TAGID, CKSUM, FIN} etape = DEBUT;
static uint8_t position;
static uint8_t cksum;
bool trameDisponible = false;
int r = rfidSerial.read();
if (r == -1) return false; // rien à lire
switch (etape) {
case DEBUT:
if (r == 0xAA) {
position = 0;
cksum = 0; // initialisation du check sum
etape = FIXE;
}
break;
case FIXE:
cksum ^= (uint8_t) r;
switch (position) {
case 0: if (r == 0x0F) position++; else etape = DEBUT; break;
case 1: if (r == 0x08) position++; else etape = DEBUT; break;
case 2: if (r == 0x00) {
position = 0;
etape = PAYS;
} else etape = DEBUT;
break;
}
break;
case PAYS:
cksum ^= (uint8_t) r;
trame.pays[position++] = r;
if (position >= sizeof trame.pays) {
position = 0;
etape = TAGID;
}
break;
case TAGID:
cksum ^= (uint8_t) r;
trame.identifiant[position++] = r;
if (position >= sizeof trame.identifiant) etape = CKSUM;
break;
case CKSUM:
if (cksum == (uint8_t) r) {
etape = FIN;
} else {
etape = DEBUT;
}
break;
case FIN:
trameDisponible = (r == 0xBB);
etape = DEBUT;
break;
}
return trameDisponible;
}
// ------------------------ LE CODE PRINCIPAL------------------------
void setup()
{
Serial.begin(115200); Serial.println();
rfidSerial.begin(9600);
Serial.println(F("134.2khz Animal RFID Reader"));
Serial.println(F("Ready"));
}
void loop()
{
if (nouvelleTrameRecue()) {
imprimerTrame();
}
// ici on peut faire autre chose
}
j'ai changé le nom de la fonction lireTrame
en nouvelleTrameRecue()
comme ça c'est peut être plus clair quand on écrit si nouvelle Trame Recue alors imprimer la Trame
➜
if (nouvelleTrameRecue()) {
imprimerTrame();
}
il n'y a que l'impression d'un nombre sur 64 bits que votre arduino ne sait pas faire. Pour le code pays un simple print fonction (c'est un entier sur 16 bits)