Using SerialPassthrough to route incoming data to serial(USB)

I am building a system that reads from multiple RFID readers to one arduino. Each RFID reader is hooked up to one of the four serial ports. Since I want the numbers of the RFID tags to be displayed in the serial monitor, I am using SerialPassthrough to rout all incoming data to the serial(usb), which then reads to my computer. the code looks like this:

int RFIDResetPin = 13;
////////////////////////////////////////////////////////////////////
//this uses the serial passthrough method, that sends data read by the serial1, 2 and 3 ports, sends them throught a TTL to the Serial0 port to be read by the computer
// read more about this method at https://www.arduino.cc/en/Tutorial/SerialPassthrough
////////////////////////////////////////////////////////////////////

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

}

void loop()
{
char tagString0[13];
int index = 0;
boolean reading = false;

if(Serial.available()){

int readByte0 = Serial.read(); //read next available byte

if(readByte0 == 2) reading = true; //begining of tag
if(readByte0 == 3) reading = false; //end of tag

if(reading && readByte0 != 2 && readByte0 != 10 && readByte0 != 13){
//store the tag
tagString0[index] = readByte0;
index ++;
}
}

Serial.println(tagString0); //read out any unknown tag

clearTag(tagString0); //Clear the char of all value
resetReader(); //eset the RFID reader

char tagString1[13];

if(Serial1.available()){

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

if(readByte1 == 2) reading = true; //begining of tag
if(readByte1 == 3) reading = false; //end of tag

if(reading && readByte1 != 2 && readByte1 != 10 && readByte1 != 13){
//store the tag
tagString1[index] = readByte1;
index ++;
}
}

Serial.write(Serial1.read());

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

}

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

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; } } boolean compareTag(char one[], char two[]){
  ///////////////////////////////////
  //compare two value to see if same,
  //strcmp not working 100% so we do this
  ///////////////////////////////////
  if(strlen(one) == 0) return false;
  //empty for(int i = 0; i < 12; i++){ if(one[i] != two[i]) return false; } return true;
  //no mismatches }
}

This compiles fine, but is unable to upload to the Mega chip I’m using.

i have no idea why the markup went screwy

When you figure it out, consider rewriting your code to use an array of readers.
That will make your code much shorter and readable.

I fixed the markup,But I don't see the point of setting each reader to an array. I can just find it by their specific serial number

I assume you are using a Mega since you mention 4 serial ports.

If you have an RFID device connected to Pins 0 and 1 (Serial) that will interfere with uploading a program. I am surprised that it does not also interfere with sending data to the Serial Monitor.

...R

I don't see the point of setting each reader to an array. I can just find it by their specific serial number

Just trying to help. It won’t fix your issues but will make your code much shorter and more readable. Not forcing you to improve your style...

I do appreciate the help, I just want to solve all the problems first.

Robin2: If you have an RFID device connected to Pins 0 and 1 (Serial) that will interfere with uploading a program. I am surprised that it does not also interfere with sending data to the Serial Monitor.

It actually does interfere with uploading, I have to disconnect everything before uploading or else I get a timeout error. however, with some recent changes I've made to the program, it doesn't upload at all. I'll be starting yet another thread soon, as I keep running into walls with each step.

Avram_Alter: . I'll be starting yet another thread soon, as I keep running into walls with each step.

Better to keep all the info about one project in the same Thread - that way people don't have to wonder what advice has already been given.

...R

  1. Use control-T to format your code.

When you do, you will see that this code fell outside the first Serial.available()

Serial.println(tagString0); //read out any unknown tag

Most of the time this will print nothing except empty lines. This shoyld only be printed once a string is recognized as complete.

  1. You use the same index for both readers. So if one reads one byte from a tag and then the other reads a byte, it will be placed in the wrong place in the string.

  2. You never check if index exceeds the size of the array. This will cause memory corruption and crashes.

  3. ClearTag() will put a null in the first character of the string and then strlen() will report a length of zero. So the for() loop is useless.

  4. The format is too badly damaged for me to continue past that point.

@OP, perhaps you should put the project aside for a moment, and sum these numbers... 11306 55821 ++++ They add up to more than 12

@MorganS I used ctrl +T, and now the code does look nicer. I was thrown into this project with zero prior experience with either arduino or coding, so im just trying got keep my head afloat. let me go through the points one by one, and see if I am understanding. I'm working off someone else's code, and that person does not remember much of what they were doing when they first wrote it.

I do want a blank line to be printed when no tag is present.

I have attempted to make the indices different for each one, but I get an error chastising me for restating the index.

I'm not sure what you mean by checking if the index exceeds the array. A google search got me stuff on Matlab (which I have to learn on the fly as well) but nothing helpful for the current situation.

why does the clear tag do that? I thought it made sure Arduino didn't just keep printing to the serial monitor the same rfid tag, even when it wasn't present anymore.

I wish I could explain more, but I lack the knowledge to even put into words my problems with this.

@lastchancename

I do not understand, sorry. :confused:

Maybe this will help for problem #3: https://forum.arduino.cc/index.php?topic=390049.0

Post your updated code.

One thing you can not do is use the first serial port for both the a reader and the USB connection as they share the same UART in the Mega chip. Even if you manage to upload, they will still interfere with each other when the code is running.

If the requirement is less than 4 readers, use Serial1, Serial2 and Serial3 for the readers and Serial for the communication with the PC. If your requirement is 4 readers, you can use SoftwareSerial (or the better alternatives AltSoftSerial or NeoSWSerial) for the 4th reader.

@MorganS, here is the updated code:

int RFIDResetPin = 13;
////////////////////////////////////////////////////////////////////
//this uses the serial passthrough method, that sends data read by the serial1, 2 and 3 ports, sends them throught a TTL to the Serial0 port to be read by the computer
// read more about this method at www.arduino.cc/en/Tutorial/SerialPassthrough
////////////////////////////////////////////////////////////////////

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

}

void loop()

{
char tagString0[26];
int index = 0;
boolean reading = false;

if (Serial.available()) {

int readByte0 = Serial.read(); //read next available byte

if (readByte0 == 2) reading = true; //beginning of tag
if (readByte0 == 3) reading = false; //end of tag

if (reading && readByte0 != 2 && readByte0 != 10 && readByte0 != 13) {
//store the tag
tagString0[index] = readByte0;
Serial.println(String("reader1: ") + tagString0); //read out any unknown tag
index ++;
}
}

clearTag(tagString0); //Clear the char of all value
resetReader(); //eset the RFID reader

char tagString1[26];
if (Serial1.available()) {

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

if (readByte1 == 2) reading = true; //begining of tag
if (readByte1 == 3) reading = false; //end of tag

if (reading && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) { //store the tag
tagString1[index] = readByte1;
Serial.write("Reader2: " + Serial1.read());
index ++;
}
}

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

}

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

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 = 0;

  • }*
    } boolean compareTag(char one[], char two[]) {
  • ///////////////////////////////////*
  • //compare two value to see if same,*
  • //strcmp not working 100% so we do this*
  • ///////////////////////////////////*
  • if (strlen(one) == 0) return false;*
  • //empty*
  • for (int i = 0; i < 12; i++) {*
    if (one != two*) return false;*
    * } return true; //no mismatches*
    }

Code tags, please. The compiler doesn't understand italic code.

@MorganS Everytime I use the cod tags, the entire thing goes screwy, not sure why, tell me if this worked:

int RFIDResetPin = 13;
////////////////////////////////////////////////////////////////////
//this uses the serial passthrough method, that sends data read by the serial1, 2 and 3 ports, sends them throught a TTL to the Serial0 port to be read by the computer
// read more about this method at www.arduino.cc/en/Tutorial/SerialPassthrough
////////////////////////////////////////////////////////////////////
 
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);
 
}
 
void loop()
 
{
  char tagString0[26];
  int index = 0;
  boolean reading = false;
 
  if (Serial.available()) {
 
    int readByte0 = Serial.read(); //read next available byte
 
    if (readByte0 == 2) reading = true; //beginning of tag
    if (readByte0 == 3) reading = false; //end of tag
 
    if (reading && readByte0 != 2 && readByte0 != 10 && readByte0 != 13) {
      //store the tag
      tagString0[index] = readByte0;
      Serial.println(String("reader1: ") + tagString0); //read out any unknown tag
      index ++;
    }
  }
 
 
 
  clearTag(tagString0); //Clear the char of all value
  resetReader(); //eset the RFID reader
 
  char tagString1[26];
  if (Serial1.available()) {
 
    int readByte1 = Serial1.read(); //read next available byte
 
    if (readByte1 == 2) reading = true; //begining of tag
    if (readByte1 == 3) reading = false; //end of tag
 
    if (reading && readByte1 != 2 && readByte1 != 10 && readByte1 != 13) { //store the tag
      tagString1[index] = readByte1;
      Serial.write("Reader2: "  + Serial1.read());
      index ++;
    }
  }
 
 
  clearTag(tagString1); //Clear the char of all value
  resetReader(); //eset the RFID reader
 
}
 
 
 
void resetReader() {
  ///////////////////////////////////
  //Reset the RFID reader to read again.
  ///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
}
 
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 = 0;
  }
} boolean compareTag(char one[], char two[]) {
  ///////////////////////////////////
  //compare two value to see if same,
  //strcmp not working 100% so we do this
  ///////////////////////////////////
  if (strlen(one) == 0) return false;
  //empty
  for (int i = 0; i < 12; i++) {
    if (one != two) return false;
  } return true; //no mismatches
}

That looks pretty good. It depends on your tab formatting... The code tags seem to have some limitations... I use Notepad++, and the code tags throw the indents all skewiff as well when I cut & paste into the forum. You could look into removing excess blank lines.

Ok. So you fixed the first problem I found. But all the others are still there, some of them worse than you started.

    one = 0;

Despite the absurdity of setting one to a value not equal to 1, this is not setting the first character of the string pointed to by one. It sets the pointer to zero, leaving the string untouched.

Try to fix the other problems I numbered.