Both-way serial communication 2 arduino UNOs

Hello guys, im working on a project, which should act as an access control for parking lot. We are using 2 Arduino UNOs. One of them would act as card reader(RFID) at the entrance(“slave”). The second one would be used as a “master” (inside the building/reception) which would contain a database(text file on and SD card). These two arduinos would be connected via RX/TX pins (All other pins are already used so this is the only way we can go). The “slave” would read an RFID card, save it inside a String variable. Then it should send the string to “master”. “Master” should go through the file on SD card and search for the String it recieved. Then it should send the result of the search (char/int or anything else) to the “slave” , which would then open the gate or refuse access. We have more features but i cut it so it would be simpler to explain. I tested the comunication by sending strings. It works, but i have to make sure that everytime i send the RFID tag String to the “master”, it would recieve it and perform the searcvh and talk back to the “slave”. Any tips how to achieve this?? (“master” also has other features, so i want to make sure that whatever it is doing, it will stop and do what “slave” wants it to do). Thank you for responses!

You have to write only non-blocking code for everything. Then you check for serial input on every invocation of loop() and do whatever the slave wants to be done.

DrDiettrich:
You have to write only non-blocking code for everything. Then you check for serial input on every invocation of loop() and do whatever the slave wants to be done.

What do you mean by "non-blocking code" ? On master or slave?

Non-blocking code means code that does not block other activities over a possibly lengthy time. What's "lengthy" depends on the required response time. This rule applies to every controller that must respond on some event in time.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino communication.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

If you want 2-way communication then just put a send and receive function on both Arduinos.

…R