0

I currently have the code running through Serial1, which works exactly as I want it to:

int RFIDResetPin = 13;


void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);
  Serial.println("Arduino is ready");
}

void loop(){

  char tagString1[13];
  int index = 0;
  boolean reading1 = false;
  ///////////////////////////////////////
  //this reads from the reader installed
  //on serial1, which is pin 19
  //////////////////////////////////////
  while (Serial1.available() > 0) {

    int readByte1 = Serial1.read(); //read next available byte

    if (readByte1 == 2) reading1 = true;
    if (readByte1 == 3) reading1 = false;

    if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
      tagString1[index] = readByte1;
      index++;
    }
  }
  Serial.print("Reader 1: "); //read out any unknown tag
  Serial.println(tagString1);


  clearTag(tagString1); //Clear the char of all value
  resetReader(); //reset the RFID reader
}




void resetReader() {
  ///////////////////////////////////
  //Reset the RFID reader to read again.
  ///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]) {
  ///////////////////////////////////
  //clear the char array by filling with null – ASCII 0
  //Will think same tag has been read otherwise
  ///////////////////////////////////
  for (int i = 0; i < strlen(one); i++) {
    one[i] = 0;
  }
}

However, I want to add serial0, 2, and 3 as well. there are 2 main questions I have 1) is it possible to have copy paste this section of the code:

    while (Serial1.available() > 0) {

    int readByte1 = Serial1.read(); //read next available byte

    if (readByte1 == 2) reading1 = true;
    if (readByte1 == 3) reading1 = false;

    if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
      tagString1[index] = readByte1;
      index++;
    }
  }
  Serial.print("Reader 1: "); //read out any unknown tag
  Serial.println(tagString1);


  clearTag(tagString1); //Clear the char of all value
  resetReader(); //reset the RFID reader
}

while changing some of the variable names to connect the other serial ports? I saw something here, but don't know how to implement it, or if it even would work for me.

and 2) since I am routing the incoming data through serial0, Is it possible to have this code working on it as well?

  • There might be a way to do it if in the void loop I set up certain functions, and then treat each function as its own void tag (unsure if that is the correct terminology). That is basically what the link does, but I have no idea how I would go about setting that up in code. – Rambunctiouslad May 8 at 21:36
  • Is there something that defines the end of the data? For example linefeed? Is there a start byte? What kind of data is received. – Jot May 8 at 23:32
  • I didn't four serial ports in your code. What I understand is that you hadn't upload your full code or you just created four object for a single port. As @jsotola said you can't read all serial at the same time with single ALU or Ardunio you need four ALU or arduino to process four peripherals at the same time. If you are going to sequential reading my advice will be that you first read the data from all ports first the go for data analysis part. – Vaibhav May 9 at 5:26
  • @Jot CR/LF used to for new line defined beginning and end of data – Rambunctiouslad May 9 at 14:46
  • @jsotola true, I meant sequentially, good point – Rambunctiouslad May 9 at 14:46
3

The Arduino already reads from the 4 serial ports at the same time. Or at least as close to “at the same time” as possible with a single CPU: every time a byte is received at one port, an interrupt is triggered that reads the byte from the UART and puts it into a buffer. You can then get it from the buffer using Serial.read().

What you now have to do is move the bytes from these buffers (which belong to the Arduino hardware serial library) into you own buffers for further processing. You can do this by checking the ports sequentially and processing each one in turn. Everything will be fine as long as you do not have blocking code in there. If you do block, then you run the risk of having one of the library buffers overflow. The code you posted is non-blocking, so you are fine.

is it possible to have copy paste

You should avoid programming by copy-and-paste. It's not that it won't work (it will), but you would be creating a maintenance nightmare. Instead, put the code you would want to copy-paste into a function, and call that function as many times as required. For example:

void handlePort(Stream &port, const char *readerName, char *tagString)
{
    boolean reading1 = false;
    while (port.available() > 0) {
        int readByte1 = port.read(); //read next available byte

        if (readByte1 == 2) reading1 = true;
        if (readByte1 == 3) reading1 = false;
        if (reading1 && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) {
            tagString[index] = readByte1;
            index++;
        }
    }
    Serial.print(readerName); //read out any unknown tag
    Serial.print(": ");
    Serial.println(tagString);

    clearTag(tagString); //Clear the char of all value
    resetReader(); //reset the RFID reader
    }
}

And you call it in loop():

handlePort(Serial1, "Reader 1", tagString1);
handlePort(Serial2, "Reader 2", tagString2);
...
  • problem is, I want the reader to change names as well. for instance, when it is going through serial1, I want it to say "Reader1", fro serial2, I want "Reader 2" and so on. – Rambunctiouslad May 9 at 14:55
  • Also, tagString1 in the loop() is throwing an error – Rambunctiouslad May 9 at 15:03
  • @Rambunctiouslad: 1. Anything you want to change from port to port should be passed as an argument to the function. Specifically, for the "Reader 1"/"Reader 2" issue, see the code in my amended answer. 2. Re “tagString1 in the loop() is throwing an error”: sorry, I don't have a crystal ball to tell me what the error is. – Edgar Bonet May 9 at 15:38
  • all the errors was my own stupidity, as I called it wrongly. Once i figured out what was going on, I was able to fix it, and now everything works. thanks for the help – Rambunctiouslad May 9 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.