how to eread from a couple serial ports at once

I'm using a mega2560, and I want to take advantage of each serial port. Each one will be connected to a different RFID reader. This is the code I am working on right now:

int RFIDResetPin = 13;

 

 

void setup()

{

Serial.begin(9600);

pinMode(RFIDResetPin, OUTPUT);

digitalWrite(RFIDResetPin, HIGH);

Serial1.begin(9600);

pinMode(RFIDResetPin, OUTPUT);

digitalWrite(RFIDResetPin, HIGH);

}

 

void loop()

{

char tagString[13];

int index = 0;

boolean reading = false;

 

while(Serial.available()){

 

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

 

if(readByte == 2) reading = true; //begining of tag

if(readByte == 3) reading = false; //end of tag

 

if(reading && readByte != 2 && readByte != 10 && readByte != 13){

//store the tag

tagString[index] = readByte;

index ++;

}

}

 

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

 

clearTag(tagString); //Clear the char of all value

resetReader(); //eset the RFID reader

 

while(Serial1.available()){

 

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

 

if(readByte == 2) reading = true; //begining of tag

if(readByte == 3) reading = false; //end of tag

 

if(reading && readByte != 2 && readByte != 10 && readByte != 13){

//store the tag

tagString[index] = readByte;

index ++;

}

}

 

Serial1.println(tagString); //read out any unknown tag

 

clearTag(tagString); //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);

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

}

I built it off of a single serial reader, as you can see the read functions are doubled. First one is RX0 pin, second is for RX1 pin. I'm very new to this, any help would be appreciated.

Use Serial1 & Serial2 and Serial3, using Serial just cuts off your ability to talk through the USB port.

I attempted to do that, but I got an error that the port didnt exist. another issue is that i want to run my program on both serial1, 2, and 3, all at once

I attempted to do that, but I got an error that the port didnt exist.

Then you didn't do it right, post your code and lets see if we can spot what you did wrong.

another issue is that i want to run my program on both serial1, 2, and 3, all at once

What is stopping you? Again post your code where you try this. Basically you can't do things simultaneously only do them one after the other so quickly that it looks like it is happening at the same time.

ok here it is:

int RFIDResetPin = 13;


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


}

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

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.println(tagString1); //writes to serial monitor

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

char tagString2[13];


if(Serial2.available()){

int readByte2 = Serial2.read(); //read next available byte

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

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

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

clearTag(tagString2); //Clear the char of all value
resetReader(); //Reset the RFID reader

char tagString3[13];


if(Serial3.available()){

int readByte3 = Serial3.read(); //read next available byte

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

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

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

clearTag(tagString3); //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);
}

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
}

I really am new at this, I was basically thrown headlong into a project. In essence, I want an RFID reader to scan a tag, and then send the number of the scanned tag to the serial monitor. It also makes the mega continuously write to the serial monitor with null when there is no tag detected. This code compiles and uploads really nicely, but then does nothing. the rx light on the chip doesn't go on, and nothing appears in the serial monitor.

Your code is almost impossible to read. Well done for posting it in code tags, but please post it again, after you have performed an auto-format (tools menu).

I do not see Serial.begin() in setup().

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

That code can only read one byte and then the code that follows it writes one byte to the serial monitor and then wipes the value you have got.

Thing is you are trying to do too much all at once. Start of with just reading one serial port and put it in a function. When that is working you can extend this to use the other two readers.