Multiple serial port not working

This is working for me. I get data when I upload sketch https://electrosome.com/em-18-rfid-reader-arduino-uno/

int count = 0;                                          // count = 0
char input[12];                                         // character array of size 12 
boolean flag = 0;                                       // flag =0
void setup()
{
   Serial.begin(9600);                                  // begin serial port with baud rate 9600bps
}
void loop()
{
   if(Serial.available())
   {
      count = 0;
      while(Serial.available() && count < 12)          // Read 12 characters and store them in input array
      {
         input[count] = Serial.read();
         count++;
         delay(5);
      }
      Serial.print(input);                             // Print RFID tag number 
      
      if((input[0] ^ input[2] ^ input[4] ^ input[6] ^ input[8] == input[10]) && 
         (input[1] ^ input[3] ^ input[5] ^ input[7] ^ input[9] == input[11]))
            Serial.println("No Error");
      else
            Serial.println("Error");      
   }
}

Above code only work for hardware serial but I need to read multiple port to read many RFIDS

I found this link https://docs.arduino.cc/tutorials/communication/TwoPortReceive

I don't get reader data when I upload below sketch

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11

SoftwareSerial portOne(10, 11);

// software serial #2: RX = digital pin 8, TX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega

SoftwareSerial portTwo(8, 9);

void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  // Start each software serial port

  portOne.begin(9600);

  portTwo.begin(9600);
}

void loop() {

  // By default, the last initialized port is listening.

  // when you want to listen on a port, explicitly select it:

  portOne.listen();

  Serial.println("Data from port one:");

  // while there is data coming in, read it

  // and send to the hardware serial port:

  while (portOne.available() > 0) {

    char inByte = portOne.read();

    Serial.write(inByte);

  }

  // blank line to separate data from the two ports:

  Serial.println();

  // Now listen on the second port

  portTwo.listen();

  // while there is data coming in, read it

  // and send to the hardware serial port:

  Serial.println("Data from port two:");

  while (portTwo.available() > 0) {

    char inByte = portTwo.read();

    Serial.write(inByte);

  }

  // blank line to separate data from the two ports:

  Serial.println()

Did you read this?

Limitations of This Library

SoftwareSerial library has the following known limitations:

[...]
If using multiple software serial ports, only one can receive data at a time.

1 Like

Change your hardware. A Mega2560 has 4 hardware serial interfaces.

1 Like

i know but why my code is not working. I have written program for two port but i am testing only one rfid so i was expecting it should be work

Perhaps your device does not know it is connected. Are you using the actual pins associated with that particular software serial set up?

You didn't upload a complete sketch.

Yes

I have uploaded the complete sketch given in link and after compiling I saw there was no error.

Sketch is posted on the forum from mobile, so there may be some mistakes

I have uploaded below sketch but it doesn't display data on serial monitor. Do I have to use array to store 12 bit data in code ?

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    char c = mySerial.read();
}

There are no serial print() or write() calls, so that isn't a surprise.

Done but still not getting data on serial monitor

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
}

void loop() {
    if (mySerial.available() > 0) {
        mySerial.read();
        char c = mySerial.read();
        Serial.write(c);
        mySerial.print(c);
    }
}

This could print a strange char for each character received, if there were a Serial.begin.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.