I possess an Arduino Mega.
Connected to it are two modules.
When they are individually connected, they function properly. However, once both are connected simultaneously, they cease to operate correctly.
Gave power supply from different sources. Did not help.
Once Rx Tx of one module is taken out and the code is uploaded again, starts work fine.
The code runs smoothly without any freezes. However, the data reading does not occur.
The code is straightforward:
#include <Arduino.h>
// Declare strings as global variables
String receivedString1;
String receivedString2;
void setup() {
Serial.begin(9600); // Serial monitor
Serial1.begin(9600); // Module 1
Serial2.begin(9600); // Module 2
}
void loop() {
if (Serial1.available()) {
receivedString1 = Serial1.readString();
Serial.print("Module 1: ");
Serial.println(receivedString1);
}
if (Serial2.available()) {
receivedString2 = Serial2.readString();
Serial.print("Module 2: ");
Serial.println(receivedString2);
}
}
How many serial ports does your Nano actually have?
I have Mega. I have mentioned that post to describe the modules.
This is blocking code, and is more or less guaranteed to result in loss of data if two serial ports are both active at the same time.
String receivedString = Serial1.readString(); // Read the incoming string
Avoid using Strings on AVR based Arduinos, as Strings lead to program crashes.
To do this properly, have a look at the Serial Input Basics tutorial.
I have declared them outside also, does not work.
Did you notice the Serial.readString() blocks until it finishes reading a string? So if the first readString() never gets a string, your code will NEVER get to the second serial read.
Paul, how then to handle this?
While I'm not sanguine about the coding, it does appear the the readString() call should time out...
From Serial.readString() reference page:
Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).
From Serial.setTimeout() reference page:
Serial.setTimeout() sets the maximum milliseconds to wait for serial data. It defaults to 1000 milliseconds.
Mind you, on second reading, if one of the modules sends at least one character a second, what then?
Van der Decken, Thanks.
How may I modify the code to get a different picture?
Thought so. So ask Chat GPT why it doesn't work.
ChatGpt does not think it is due to the code, because I have used self-written several options before posting this, and they did not work either. This is for demonstration just.
Can you please show the output you get when you print the received string?
What transponder are you reading? Is the data sent continuously or just when a card is placed?
I receive 900_47811341123 (or other numbers).
I am reading a glass microchip. Just when the card is placed.
I have done the same with int and Serial.read etc.
I also played with timeouts
And yet you show none of your "self-written several options". I'm done here.
What readers do you have? How close together are they when you are trying to read two tags? What are the specifics of how you are trying to read the two tags?
This document for the 4100 reader says
https://www.priority1design.com.au/rfidread4100.pdf
In general operation the reader will continually scan for any transponders that come within range and
transmit their data to the reader. As EM4100 compatible transponders do not have collision avoidance
algorithms only one card can be scanned within the range of the reader at any one time.