0

I have a code that loops through 2 different serial inputs on the arduino mega, and sends them to serial constantly:

int RFIDResetPin = 13;


void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);
}

void loop() {

  char tagString1[13];
  int index = 0;
  boolean reading1 = false;
  boolean reading2 = 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.println(String("Reader 7: ") + tagString1); //the tag itself

delay(1);
  clearTag(tagString1); //Clear the char of all value
  resetReader(); //reset the RFID reader
  //////////////////////////////////////////////////
  // This bit reads from serial2, located on pin 17
  /////////////////////////////////////////////////
  while (Serial2.available() > 0) {

    int readByte2 = Serial2.read();

    if (readByte2 == 2) reading2 = true;
    if (readByte2 == 3) reading2 = false;

    if (reading2 && readByte2 != 2 && readByte2 != 10 && readByte2 != 13) {
      tagString1[index] = readByte2;
      index++;
    }
  }
  Serial.println(String("Reader 8: ") + tagString1);

  clearTag(tagString1);
  resetReader();
}




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;
  }
}

when not scanning anything, the serial monitor outputs this:

Reader 1:
Reader 2:
Reader 1:
Reader 2:

When I scan a tag, lets say on reader 1, I get this:

Reader 1: 000004f5
Reader 2:
Reader 1: 000004f5
Reader 2:

it is not possible to scan more than a single scanner at a time. Is it possible to get the first line to wait until a certain number of characters have been scanned (indicating a tag has been scanned), and then output to the serial monitor?After that initial run, the code will loop as I showed above. any help is appreciated.

7
  • Your dcode doesn't match your output (e.g. "Reader 7: ") At 9600 baud, you could have lots of time between characters and do simultaneous reads. Try reading the bytes from the separate ports into separate buffers bytewise, and then only acting when you fill up a buffer.
    – Dave X
    Feb 3, 2020 at 19:12
  • I've tried that in the past, the problem is that I don't get any output at all, including the Reader names. I want them to appear at all times, even when there is no tag. I only want the first line to wait until a tag has been scanned
    – user54795
    Feb 3, 2020 at 19:17
  • It looks like index is shared between the tags, and you do a 150ms reset cycle and clear each cycle, Per Bra1n's answer, do consider a state machine, and maybe use a index1 and index2 to track the number of bytes you've read on each, and separate the reporting into something conditional on having a "certain number of characters have been scanned"
    – Dave X
    Feb 3, 2020 at 19:32
  • @jsotola I have no problem receiving from multiple serial ports at once (or functionally at once), as I explained in the question. I currently have 8 of these system working properly together. I just need this modification on the first one, to initiate certain functions in a different code. I appreciate the thought.
    – user54795
    Feb 3, 2020 at 19:53
  • @daveX that definitely seems like something worth investigating, thank you
    – user54795
    Feb 3, 2020 at 19:54

2 Answers 2

2

I would use a programming concept called a state machine. For example an integer variable called State could be used to track the current status which could for example be WAITING_FOR_DATA or RECEIVING_DATA or DATA_COMPLETE and only print when DATA_COMPLETE. I'm sure there are plenty of examples you could find with an appropriate search.

2
  • this sounds like a good idea, I'll do some research. Thanks very much
    – user54795
    Feb 3, 2020 at 19:51
  • Better use an enum type for State instead of an int (and using a better name, like UartState, or UartState1, UartState2 if you need) Feb 3, 2020 at 20:07
1

1.prefer using Serial.readStringUntil(); OR Serial.readString() that way its much easy to manipulate.

2.use a static variable in loop function which stores the string or use a global variable to save char array tagString1 before going in println function same goes with the second reader that way the println will show the previous scan until a new scan is not done.

3.print both the tagstrings in the end of the code one after another

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.