NewSoftSerial (HELP)

Hello all,

Your help in the problem below is wellcome.

I need to use 2 Tx outputs, one to setup the GPS and other one to send the Lat., Long., etc to another system (two way radio control receiver). I made several attempts but without success, I can't get any signal at the TX pin3 with the code below (it has a lot of patches just for test the input, because, I wasn't also able to read the inputs sentence. I had to setup the Baudrate to 4800 in order to get the sentence without errors):

#include <NewSoftSerial.h>

NewSoftSerial gps1(2, 3);
NewSoftSerial gps(0, 1);
String chkStr = String(5);
String dataStr = String(65);
String returnStr = String(12);

char chkByte;
char inByte;
int i = 10;
int page = 1;

void setup() {

  pinMode(0, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  lcd.begin(DOG_LCD_M163,0x28);
  lcd.setCursor(0, 0);
  lcd.print("--Arduino  GPS--");
  lcd.setCursor(0, 1);
  lcd.print("------v2.1------");
  delay(2000); 
  gps1.begin(38400);
  Serial.begin(38400);
  gps1.read();
  Serial.print("$PMTK251,4800*14\r\n");  
  gps.begin(4800);
  Serial.begin(115200);
  delay(200);

  gps.read();
 
}

void loop() {
   chkStr = '\0';
 // if (gps.available() > 0) {
  //  chkByte = gps.read();
    //while (chkByte > 0) {
      if (i > 0) {
        if (gps.available() > 0) {
          inByte = gps.read();
          //chkStr.concat(inByte);
            Serial.print(inByte);
          i--;
        }
        else {
          waitForByte();
        }
      }
      else {
        i = 10;
      //  break;
      }
      while (chkStr.equals("GPRMC")) {
        if (gps.available() > 0) {
              inByte = gps.read();
            dataStr.concat(inByte);
                //Serial.print(inByte);
          }
         else {
          waitForByte();
        }
        }
    }
 // }
//}
void waitForByte() {
  while (true) {
    if (gps.available() > 0) {
      break;
    }
    else {
      continue;
    }
  }
}

Considering that I didn't get any result with the sketch (Kind of) above, I tried the example below, but I didn't get any output also at the TX pin3 and TX pin5. I always get the output at the TX pin1, in both cases . Your help is appreciated.

thanks and regards,
Manuel

  #include <NewSoftSerial.h>

NewSoftSerial nss(2, 3);
NewSoftSerial nss2(4, 5);
       
void setup()
{
  nss2.begin(19200);
  nss.begin(19200);
  Serial.begin(115200);
}
       
void loop()
{
// Every 10 seconds switch from 
// one serial GPS device to the other
if ((millis() / 10000) % 2 == 0)
  {
    if (nss.available())
    {
      Serial.print(nss.read(), BYTE);
    }
 }

  else
  {
    if (nss2.available())
   {
      Serial.print(nss2.read(), BYTE);
   }
  }
}
  pinMode(0, INPUT);
  pinMode(1, OUTPUT);

Pins 0 and 1 are the Serial class pins. You should not be setting the mode for these pins.

  gps1.read();
  gps.read();

What's this for? If you are not capturing the output, what is the purpose of reading? It's like reading a book with a blindfold on.

Considering that I didn't get any result with the sketch (Kind of) above, I tried the example below, but I didn't get any output also at the TX pin3 and TX pin5. I always get the output at the TX pin1, in both cases .

Where are you writing anything using gps or gps1. If you don't write, how can you expect to get data on the TX pins for those classes?

Pauls, thanks for your reply.

The "pinmode" I also though that wasn't necessary, but, I saw it an example, and just in case I did it.

I did the "gps1.read();" because in the "NewSoftSerial" instructions we can read:
" // collect data from the GPS unit for a few seconds
read_gps_data(); // use gps as active device"

My thought was that to activate the device we had to do the read (nothing)

Conclusion, I do not know how to write to TX pins 3 in one example and 3 and 5 in the other example.

I read the page of the NewSoftSerial "Arduina" could not find any thing specific to write to the pin.
I read also several example sketchs and didn't see nothing to write to a specific pins. my thought was that when we do the "Serial.print" it also directs it to the pins defined as output.

I know that I am reading because the output in comming out at the Tx pin 1.

If you can help, I will appreciate.

Thanks.
Manuel

Conclusion, I do not know how to write to TX pins 3 in one example and 3 and 5 in the other example.

You write to the NewSoftSerial TX pins EXACTLY the same way that you write to the hardware serial pins, using (in your case) gps.write(), gps.print(), or gps.println() (or the gps1 instance).

Any use of a different instance of NewSoftSerial changes the active instance, whether that is nss.available(), nss.read(), nss.write(), nss.print(), or nss.println(). The useless read() in setup() isn't necessary.

my thought was that when we do the "Serial.print" it also directs it to the pins defined as output.

You need to use the correct instance of the hardware or software serial class to write to that instance's TX pin.

Pauls, Thanks! It is working! It was really simple.

Thanks again.
regards,
Manuel