Multiple RFID With UART

Hi, everyone. I have two different RFID reader module. Both are working with UART. I am trying to use them with Arduino uno. Here is my code :

#include <SoftwareSerial.h>
SoftwareSerial RFID1(2, 3);
SoftwareSerial RFID2(4, 5);
byte b2;
byte b;
byte buffer[30];
uint8_t idx;
boolean started = false;
byte XOR;
byte inverted;
uint64_t value;
char *uint64_to_string(uint64_t input)
{
  static char result[21] = "";
  // Clear result from any leftover digits from previous function call.
  memset(&result[0], 0, sizeof(result));
  // temp is used as a temporary result storage to prevent sprintf bugs.
  char temp[21] = "";
  char c;
  uint8_t base = 10;
  while (input)
  {
    int num = input % base;
    input /= base;
    c = '0' + num;

    sprintf(temp, "%c%s", c, result);
    strcpy(result, temp);
  }
  return result;
}
void setup()
{
  // Initialize Serial port
  Serial.begin(9600);
  while (!Serial) continue;
  RFID2.begin(9600);
  RFID1.begin(9600);
  pinMode(8, OUTPUT); // This is reset pin of RFID1.
}
void loop()
{
  digitalWrite(8, HIGH);
  delay(1000);
  RFID1.listen();
  while (RFID1.available())
  {
    b = RFID1.read();
    Serial.print(b);
    if (b == 0x02) // Start byte
    {
      idx = 0;
      started = true;
    }
    if (started) // Ignore anything received until we get a start byte.
    {
      buffer[idx++] = b;
      if (b == 0x03) // End byte
      {
        started = false;
        // Display the received data.
        Serial.print(F("Received data: "));
        for (int x = 0; x < idx; x++)
        {
          if (buffer[x] < 0x10)
          {
            Serial.print(F("0")); // Pad with leading 0
          }
          Serial.print(buffer[x], HEX);
          Serial.print(" ");
        }
        value = 0;
        Serial.print(F("This is my buffer[x] from 1 to 10: "));
        for (int x = 10; x >= 1; x--)
        {
          Serial.print(buffer[x], HEX);
          if (buffer[x] <= '9')
            value = (value << 4) + buffer[x] - '0';
          else
            value = (value << 4) + buffer[x] - '7';
        }
        Serial.println(F(""));
        Serial.print(F("Card number: "));
        Serial.print(uint64_to_string(value));
        String i = uint64_to_string(value);
        // Extract the country number from bytes 11 (LSB) - 14 (MSB).
        value = 0;
        for (int x = 14; x >= 11; x--)
        {
          if (buffer[x] <= '9')
            value = (value << 4) + buffer[x] - '0';
          else
            value = (value << 4) + buffer[x] - '7';
        }
        Serial.println(F(""));
        Serial.print(F("Country number: "));
        String z = uint64_to_string(value);
        if (z == NULL)
          z = "0";
        String rfidTag = z + i;
        Serial.println(rfidTag);
        Serial.println(F("\r"));
        digitalWrite(8, LOW);
      }
    }
  }

  while (RFID2.available())
  {
    b2 = RFID2.read();
    Serial.print(b2);
    Serial.println("hiii");
  }
}

I can only read RFID1's values. RFID2's reader module is working (green led is blinking when I put rfid tag close to it) but I can't see any value. If I use RFID2 alone, I can see RFID2 is working. What should I do for using both of them together ?

I tried RFID1.listen() and RFID2.listen() . It didn't work too.

You seem to take not care of proper indention in your code.
The compiler does not care about indention But you should.

simply press Ctrl-T to autoformat your code to the correct indentions

If your RFID is sending a single byte this means

RFID1.available()

results in true
not sure if this helps
If you do not use a while-loop but instead a code like shown in

Be the change you want to see in the world
best regards Stefan

Hi, thank you for Ctrl-T, I didn't know that.

I tried to upload my code. I tried like this:

#include <SoftwareSerial.h>
SoftwareSerial RFID (2, 3);
SoftwareSerial RFID2(4, 5);
char receivedChar;
byte receivedChars;
boolean newData = false;

void setup()
{
  // Initialize Serial port
  Serial.begin(9600);
  RFID2.begin(9600);
  RFID.begin(9600);
}
void loop()
{
  recvOneChar();
  showNewData();
  recvMultipleChars();
  showMultipleData();
}

void recvOneChar() {
  if (RFID2.available() > 0) {
    receivedChar = RFID2.read();
    newData = true;
  }
}
void showNewData() {
  if (newData == true)
  {
    Serial.print(receivedChar);
    newData = false;
  }
}
void recvMultipleChars() {
  if (RFID.available() > 1) {
    receivedChars = RFID.read();
    newData = true;
  }
}
void showMultipleData() {
  if (newData == true)
  {
    Serial.print(receivedChars);
    newData = false;
  }
}

RFID2 don't show anything with RFID. If I use RFID2 alone, RFID2 can print rfid tag but when I try both of them together, only RFID is printing.

You should provide more details.

Does using RFID2 alone mean:
RFID1 is physically disconnected from IO-pins 2 and 3 and RFID2 is still connected to IO-pins 4 and 5?

or does it mean something different?

Okay, when everything is connected (RFID and RFID2)'s UART pins, when I delete only RFID.begin(9600); line, RFID2 is working but they are not working together. I can't share RFID2's datasheet because I bought from aliexpress and there is no datasheet.

#include <SoftwareSerial.h>
SoftwareSerial RFID (2, 3);
SoftwareSerial RFID2(4, 5);
char receivedChar;
byte receivedChars;
boolean newData = false;

void setup()
{
  // Initialize Serial port
  Serial.begin(9600);
  RFID2.begin(9600);
}
void loop()
{
  recvOneChar();
  showNewData();
  recvMultipleChars();
  showMultipleData();
}

void recvOneChar() {
  if (RFID2.available() > 0) {
    receivedChar = RFID2.read();
    newData = true;
  }
}
void showNewData() {
  if (newData == true)
  {
    Serial.print(receivedChar);
    newData = false;
  }
}
void recvMultipleChars() {
  if (RFID.available() > 1) {
    receivedChars = RFID.read();
    newData = true;
  }
}
void showMultipleData() {
  if (newData == true)
  {
    Serial.print(receivedChars);
    newData = false;
  }
}

output:

000000000032654
900874236741593

In your code you have posted there is

what do the two lines

mean?

Holding two RFIDs one to RFID1 which results in printing
000000000032654

and a second RFID holding near to RFID2 printing
900874236741593
??

I have two tags, RFID2 is printing both of them to the serial monitor. If I use RFID.begin
and RFID2.begin together, RFID2 is blinking but it is not printing anything to the serial monitor. I have to delete RFID.begin for printing tag from RFID2.

I guess here is the answer.
I found this by - guess what? . . . : googling

https://codebender.cc/example/SoftwareSerial/TwoPortReceive#TwoPortReceive.ino

Be the change you want to see in the world
best regards Stefan

Why are you so mean, I wrote on question, RFID1.listen() and RFID2.listen() . It didn't work too. Guess what it is not working.

You have to activate RFID by with the command

RFID.listen()

If RFID is activated RFID2 is DE-activated

then check if something is received by RFID

then you have to activate RFID2
with the command

RFID2.listen()

If RFID2 is activated RFID is DE-activated

there is always only one software-serial-port active.
mutually exclusive one Software-UART

If you need both active at the same time you have to use a different microcontroller

Be the change you want to see in the world
best regards Stefan

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3);
SoftwareSerial portTwo(4, 5);
void setup()
{
  Serial.begin(9600);
  while (!Serial) {
  }
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop()
{

  portOne.listen();
  while (portOne.available() > 0) {
    byte inByte = portOne.read();
    Serial.write(inByte);
  }

  Serial.println();

  portTwo.listen();

  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  Serial.println();
}

If I use like this, nothing printing on serial monitor. RFID and RFID 2 is working (I know because when I put close tag to them, RFID and RFID2 is blinking but nothing shown on serial monitor.)

Serial.println(); // print an empty line 1000 times per second

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3);
SoftwareSerial portTwo(4, 5);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop() {

  portOne.listen();
  while (portOne.available() > 0) {
    byte inByte = portOne.read();
    Serial.write(inByte);
  }

  Serial.println(); // print an empty line 1000 times per second

  portTwo.listen();

  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  Serial.println(); // print an empty line 1000 times per second
}
#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3);
SoftwareSerial portTwo(4, 5);
bool tryingToStop = false;
void setup()
{
  Serial.begin(9600);
  while (!Serial) {
  }
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop()
{
  portOne.listen();
  while (portOne.available() > 0) {
    byte inByte = portOne.read();
    Serial.print(inByte);
    tryingToStop = true;
    
  }
  
  if (tryingToStop ==true)
  {
    Serial.println();
    tryingToStop = false;
  }
  portTwo.listen();

  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.print(inByte);
    tryingToStop=true;
  }
  if (tryingToStop ==true)
   {
  Serial.println();
  tryingToStop=false;
   }
}

It is not working too. When I delete 'portOne.begin(9600)', portTwo can print the rfid tag, but when I use 'portOne.begin(9600)' and 'portTwo.begin(9600)' together, only portOne is working.

When only portTwo activeted, output like this:

9
0
0
8
74
2
3
6
74
1
5
93




9
0
0
8
74
2
3
6
74
1
5
93


I made a test with an Arduino Uno and your demo-code

with commenting out the Serial.println(); that get executed with each iteration of loop
the receiving works exactly like it is programmed. Though this is not what you want

The code can not predict at which exact millisecond the receiving starts. This means some
of the characters where misinterpreted and some are missing completely.

Because the receiving on one port starts at an unpredictible time.
If your RFID-reader is able to repeat to send the RFID-number it might work in the second
sending

Or if your RFID-reader has another output that could be used as "I'm active" signal this could be used to switch on RFID1 or RFID2 to receive the data.

Or if your RFID-readers can be used in a request/answer-mode
You could send a request to RFID1 waiting half a second for the answer and if no answer is received activating RFID2 and send a request to RFID2

If these options do not work I would recommend indeed using a microcontroller that has in sum three hardware UARTS. For example an ESP32
hardware UART0 uploading to flash
hardware UART1 RFID1
hardware UART2 RFID2

Be the change you want to see in the world
best regards Stefan

Okay, thank you. I was using ESP01(UART1) and RFID1(UART2) together without problem. I tried ESP 01(UART1) and RFID2 (UART2) together without problem. They are working. I don't know what is the problem. When I use both RFID together, I can't get them work.

By the way, I was using micropython with RP2040 which has two UART (UART1 and UART2), RFID1 and RFID2 didn't worked together too.

I guess the problem is micropython so I decided to go with arduino .

ESP 01 is an ESP8266
ESP32 is a different microcontroller

ESP 01


.
.
.
.
.
.

ESP32

No, I was using serial communication between ESP 01 and arduino (UART1) then RFID1 (UART2) . I was using 2 UART but they are working good. I don't get it why 2 RFID is not working together.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.