Go Down

Topic: Reading from multiple RFID-modules at the same time (Read 58 times) previous topic - next topic

Creativefre

Hello everyone,
I recently bought 2 RFID-modules witch I connected to the arduino DUE.

The RFID-modules are from parallax: RFID-read/write module  LINK
I have managed to let them read but the only problem is, when one RFID-module is reading , the other stop reading until his reading function is called again then the other stops reading and so on ...

I have tried to use multi threading but that didn't work , also I have tried interrupts but when the RFID module is reading he constantly gets information back also if no card is scanned.

So i'm hoping anyone could help me, letting the 2 RFID modules read at the same time.

If you need more information just ask.

Here is my code
(Don't mind the led and bits to hex part)
Code: [Select]
// rx Serial ingang (connecteer aan de RFID's SOUT pin)
// tx Serial uitgang (connecteer aan de RFID's SIN pin)

#define BUFSIZE    12  // Size of receive buffer (in bytes) for incoming data from the RFID R/W Module (this should be adjusted to be larger than the expected response)

// RFID R/W Module Commands
// Number of bytes returned in ()
#define RFID_Read         0x01   // Read data from specified address, valid locations 1 to 33 (5)

// Memory map/address locations for EM4x50 tag
#define ADDR_Serial   32   // Device Serial Number

// Status/error return codes
#define ERR_OK   0x01   // No errors

int ledPinGroenInside = 2; // pin binnen Groene led
int ledPinRoodInside = 3; // pin binnen Rode led
int ledPinGroenOutside = 4; // pin buiten Groene led
int ledPinRoodOutside = 5; // pin buiten Rode led

int DeurSlot = 7; // pin deurslot

void setup() {

 
  //Instellingen ledjes
  pinMode(ledPinGroenInside,OUTPUT);
  digitalWrite(ledPinGroenInside,LOW);
  pinMode(ledPinRoodInside,OUTPUT);
  digitalWrite(ledPinRoodInside,LOW);
  pinMode(ledPinGroenOutside,OUTPUT);
  digitalWrite(ledPinGroenOutside,LOW);
  pinMode(ledPinRoodOutside,OUTPUT);
  digitalWrite(ledPinRoodOutside,LOW);
 
  //Instellingen Deurslot
  pinMode(DeurSlot,OUTPUT);
  digitalWrite(DeurSlot,LOW);
 
  // Arduino serieële monitor instellen
  Serial.begin(9600);
  while (!Serial);   // Wachten tot hij klaar is
  Serial.println("Scan je kaart in 1 van de 2 RFID modules");
 
  // set the baud rate for the SoftwareSerial port

  Serial1.begin(9600);
  Serial2.begin(9600);
 
  Serial.flush();   // wait for all bytes to be transmitted to the Serial Monitor
 
}

void loop() {
 

  uint8_t rfidData[BUFSIZE];  // Buffer voor inkomende data
 
  ledControle();
  //RFID BINNEN   
  rfidRead1(rfidData, ADDR_Serial);
  ledControle();
  //RFID BUITEN
  rfidRead2(rfidData, ADDR_Serial);
}

//Ledjes controle
void ledControle()
{
  if (Serial.available() > 0) {

  //int Controle = Serial.read() - '0'; // deduct ascii Controleue of '0' to find numeric Controleue of sent number
  char Controle = Serial.read();
 
  if (Controle == '1'){
   //Serial.println("Deurslot openen voor 3 seconden");
   digitalWrite(DeurSlot , HIGH);
   delay (3000);
   digitalWrite(DeurSlot , LOW);
  }
  else if (Controle == '2'){
   //Serial.println("Led groen binnen gaat voor 3 seconden aan");
   digitalWrite(ledPinGroenInside , HIGH);
   digitalWrite(DeurSlot , HIGH);
   delay (3000);
   digitalWrite(ledPinGroenInside , LOW);
   digitalWrite(DeurSlot , LOW);
  }
  else if (Controle == '3'){
  // Serial.println("Led rood binnen gaat voor 3 seconden aan");
   digitalWrite(ledPinRoodInside , HIGH);
   delay (3000);
   digitalWrite(ledPinRoodInside , LOW);
  }
  else if (Controle == '4'){
  // Serial.println("Led groen buiten gaat voor 3 seconden aan");
   digitalWrite(ledPinGroenOutside , HIGH);
   digitalWrite(DeurSlot , HIGH);
   delay (3000);
   digitalWrite(ledPinGroenOutside , LOW);
   digitalWrite(DeurSlot , LOW);
  }
  else if (Controle == '5'){
  // Serial.println("Led rood buiten gaat voor 3 seconden aan");
   digitalWrite(ledPinRoodOutside , HIGH);
   delay (3000);
   digitalWrite(ledPinRoodOutside , LOW);
  }
  //deurslot
  else if (Controle == '7'){
  // Serial.println("Deurslot openen");
   digitalWrite(DeurSlot , HIGH);
   delay (1000);
  }
  else if (Controle == '8'){
  // Serial.println("Deurslot sluiten");
   digitalWrite(DeurSlot , LOW);
   delay (1000);
  }
  }
}

//RFID BINNEN (1)

char rfidRead1(uint8_t *data, uint8_t address)
{
  char offset;  // offset1 into buffer
 
  rfidFlush1();  // empties any buffered incoming serial data
 
  Serial1.print("!RW");       // header
  Serial1.write(RFID_Read);   // command
  Serial1.write(address);     // address
   
  while (Serial1.available() != 5);  // wait until data is received from RFID module
 
  if (Serial1.read() == ERR_OK)  // if our status byte is OK
  {
    for (offset = 0; offset < 4; offset++)
      data[offset] = Serial1.read();  // get the remaining data
      Serial.print("0-RFID Binnen: ");
      PrintHex(data, 4);   // De rfidData1 string moet nu het serienummer van de tag bevatten, en geeft het weer op de seriêle monitor
      Serial.println();
      Serial.flush();
      delay(1000);
    return 0; // return good
  }
 
  return -1; // return error
}


void rfidFlush1()  // Empties any buffered incoming serial data
{
    while (Serial1.available() > 0)
      Serial1.read();
}



//RFID BUITEN


char rfidRead2(uint8_t *data, uint8_t address)
{
  char offset;  // offset2 into buffer
 
  rfidFlush2();  // empties any buffered incoming serial data
 
  Serial2.print("!RW");       // header
  Serial2.write(RFID_Read);   // command
  Serial2.write(address);     // address
   
  while (Serial2.available() != 5);  // wait until data is received from RFID module
 
  if (Serial2.read() == ERR_OK)  // if our status byte is OK
  {
    for (offset = 0; offset < 4; offset++)
      data[offset] = Serial2.read();  // get the remaining data
       Serial.print("1-RFID Buiten: ");
       PrintHex(data, 4);   // De rfidData1 string moet nu het serienummer van de tag bevatten, en geeft het weer op de seriêle monitor
       Serial.println();
       Serial.flush();
       delay(1000);
    return 0; // return good
  }
 
  return -1; // return error
}


void rfidFlush2()  // Empties any buffered incoming serial data
{
    while (Serial2.available() > 0)
      Serial2.read();
}

// prints 8-bit data in hex

void PrintHex(uint8_t *data, uint8_t length)
{
  char tmp[length*2+1];
  byte first ;
  int j=0;
  for (uint8_t i=0; i<length; i++)
  {
    first = (data[i] >> 4) | 48;
    if (first > 57) tmp[j] = first + (byte)39;
    else tmp[j] = first ;
    j++;

    first = (data[i] & 0x0F) | 48;
    if (first > 57) tmp[j] = first + (byte)39;
    else tmp[j] = first;
    j++;
  }
  tmp[length*2] = 0;
  Serial.print(tmp);
}

MorganS

What do you think this does if 6 chars are received?
Code: [Select]
while (Serial1.available() != 5);
Even though it's very very unlikely, it is theoretically possible to get more than 5. Always write this kind of test with a ">", "<", ">=" or "<=".

Your rfidRead#() functions are sending stuff to Serial as well as adding a one-second delay. The description of the function should match its action. If it's called "read" then it shouldn't ever be writing to another output (and then waiting more than a second for that output to go out.)

These comments are wrong. Always update your comments when you add new functions:
Code: [Select]
// rx Serial ingang (connecteer aan de RFID's SOUT pin)
// tx Serial uitgang (connecteer aan de RFID's SIN pin)

And then review the comments again before posting to the forum. What pins are the modules actually connected to?

OK, let's look at the loop...
Code: [Select]
void loop() {
  uint8_t rfidData[BUFSIZE];  // Buffer voor inkomende data
 
  ledControle();
  //RFID BINNEN   
  rfidRead1(rfidData, ADDR_Serial);
  ledControle();
  //RFID BUITEN
  rfidRead2(rfidData, ADDR_Serial);
}


Is this the code which doesn't work or does work? It looks like you've just written test code because it never uses the contents of the data buffer.
GoForSmoke: "What GShield? You never mentioned a shield."

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy