Go Down

Topic: multiple software serial inputs (Read 617 times) previous topic - next topic

katesfb

Hi,
I have a project that requires 2 software serial links to an LCD and an RFID tag reader. If i set up just the RFID reader it works fine and i can read output from the reader on the serial monitor. If i setup the LCD by itself it works fine and displays "tag reader" . However if i set up the LCD and RFID together i get no output on the serial monitor and no output on the LCD except for "tag reader" being displayed. See code below.

Any help is much appreciated.


This works and gives output in serial monitor;
Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

void setup()
{
  Serial.begin(9600);      //Serial monitor
  rfid.begin(38400);      //rfid conms
  //lcd.begin(9600);        //lcd coms BR

  Serial.println("Tag reader");
  //lcd.write(12);
  //lcd.print("Tag reader");
  delay(1000);
}

void loop() {
  if (rfid.available() > 0) {
    char inByte = rfid.read();
    Serial.write(inByte);
    //lcd.write(12);
    //lcd.write(inByte);
  }
 
}


This does not work - gives no output in serial monitor and no output on LCD exccept for "Tag reader";
Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

void setup()
{
  Serial.begin(9600); 
  rfid.begin(38400);
  lcd.begin(9600);        //lcd coms BR

  Serial.println("Tag reader");
  lcd.write(12);         //cls and return to home position
  lcd.print("Tag reader");
  delay(1000);
}


void loop() {
  if (rfid.available() > 0) {
    char inByte = rfid.read();
    Serial.write(inByte);
    lcd.write(12);      //cls and return to home position
    lcd.write(inByte);
  }
 
}

Simpson_Jr

#1
Mar 18, 2013, 02:17 am Last Edit: Mar 18, 2013, 02:37 am by Simpson_Jr Reason: 1
You can create multiple softwareserial ports, but... arduino is capable of listening to only one of those ports at the same time. By using the functions listen() and isListening() of the library it's possible to switch ports/detect which one is monitored. Since you last activated the LCD-port, I guess softwareserial now listens to the LCD- instead of RFID-port resulting in rfid.available() always being 0.

Since it looks like you only write to the LCD and you only read from the RFID reader, you could use a single software serial port for both of them with TX to LCD and RX from RFID reader.. I think.

katesfb

Hi,
And thanks for the replies. Yes it looks like i can only have one port open at a time and must make use of the .end() method to close the port before opening another port.

I will move this thread to the code section as it probably more of a coding issue.

Thanks for all your help.

Simpson_Jr

As far as I understood you can have multiple softwareserial ports open at the same time.
Sending info to a software port doesn't require much attention, but keeping track of incoming data from two softwareserial ports with a relative slow micro controller does. You should there for be able to send info to all ports whenever you want and have them opened simultaneous, but need to tell which one you want to monitor.

I can't test it right now since I'm not at home, but as far as I understand the documentation of softwareserial the next code should...  work in theory.

Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial rfid = SoftwareSerial(5, 6,true);
SoftwareSerial lcd = SoftwareSerial(2,3);

void setup()
{
  Serial.begin(9600); 
  rfid.begin(38400);
  lcd.begin(9600);        //lcd coms BR
  Serial.println("Tag reader");
  lcd.write(12);         //cls and return to home position
  lcd.print("Tag reader");


  rfid.listen() // this should... do the trick.


  delay(1000);
}

void loop() {
  if (rfid.available() > 0) {
    char inByte = rfid.read();
    Serial.write(inByte);
    lcd.write(12);      //cls and return to home position
    lcd.write(inByte);
  }
 
}


By the way, the info displayed on the LCD probably is hard to read. In your code you clear the screen before each character you receive. When the rfidreader sends a space, return or other non-visible as last character, you may... see nothing at all.

Listening to the rfid-port probably is most important in your project and I don't know whether it's possible to check the status of your lcd (to get some info from both softwareserial ports).
Should you want to, you should be able to do it by adding code like this :

Code: [Select]


   lcd.listen(); // temporarily switch port to listen to the LCD
   checkstatus(); // assuming you programmed a function to check/display the LCD status
   rfid.listen(); // switch back to the most important port asap


Go Up