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.
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.
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.
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.)
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.
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 .
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.