Arduino Forum

Topics => Science and Measurement => Topic started by: Klausi1967 on Oct 18, 2019, 02:29 pm

Title: Problems with Custom Software Serial I/F
Post by: Klausi1967 on Oct 18, 2019, 02:29 pm
Hello,


I have a problem with the Custom Software Serial Library.

I use a Nano with a PZEM017 ModBus Device connected. This Device needs a Uart with 9600, 8N2.
The standard SoftwareSerial does not support 8N2 so I used the Custom Software Serial instead.

Now I need to use it in function calls. Thus I have to declare it inside the function and when I do that, then it gets instantiated a new in every function call, which eventually makes the heap and the stack mingle and do their evil thing ... the Nano crashes after 26 loops and restarts.

When I declare it in the setup, then I cannot use it in the funtion call => the compiler spits => 'PZEMSerial' was not declared in this scope

I'm at my wits end.


This is the crucial piece of code:


  CustomSoftwareSerial* PZEMSerial;               // Declare serial
  PZEMSerial = new CustomSoftwareSerial(10, 11); // rx, tx
  PZEMSerial->begin(9600, CSERIAL_8N2);         // Baud rate: 9600, configuration: CSERIAL_8N2

....

....

....

  PZEMSerial->end();



I have attached the function as *.txt


1. How would you declare the CustomSoftwareSerial so that it can be used globaly? In Main/loop and in all functions?
2. Is there a way to use the SoftwareSerial with 8N2?
3. Whatever other ideas you have.

Thanks in advance.
Title: Re: Problems with Custom Software Serial I/F
Post by: Idahowalker on Oct 18, 2019, 03:04 pm
Code: [Select]

#include SomethingCalledCustonSoftwareblahblahblah
CustomSoftwareSerial* PZEMSerial;               // Declare serial
  PZEMSerial = new CustomSoftwareSerial(10, 11); // rx, tx or maybe this goes in setup, look at the library examples for clues
void setup() {
  PZEMSerial->begin(9600, CSERIAL_8N2);         // Baud rate: 9600, configuration: CSERIAL_8N2
}

void loop() {

}


from one of the examples
Code: [Select]
#include <CustomSoftwareSerial.h>

CustomSoftwareSerial* customSerial;               // Declare serial

// Init value
void setup() {
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.write("Hello World");

  customSerial = new CustomSoftwareSerial(9, 10); // rx, tx
  customSerial->begin(9600, CSERIAL_8N1);         // Baud rate: 9600, configuration: CSERIAL_8N1
  customSerial->write("Test message");            // Write testing data
}

void loop() {
  if (customSerial->available())
    Serial.write(customSerial->read());
  if (Serial.available())
    customSerial->write(Serial.read());

  delay(1000);
}
Title: Re: Problems with Custom Software Serial I/F
Post by: Delta_G on Oct 18, 2019, 03:28 pm
Google "C++ variable scope" to learn why this happens. 
Title: Re: Problems with Custom Software Serial I/F
Post by: Klausi1967 on Oct 18, 2019, 03:32 pm
If I declare it globaly, the I/F does not work.
Title: Re: Problems with Custom Software Serial I/F
Post by: Delta_G on Oct 18, 2019, 03:56 pm
What is I/F?  What do you mean that it doesn't work?  What happens?  How does that compare with what you wanted?

"it doesn't work" is the single most useless thing you can say here. 
Title: Re: Problems with Custom Software Serial I/F
Post by: Klausi1967 on Oct 18, 2019, 04:29 pm
Thanks Delta G and Thanks Idahowalker.

Everything works now!
Your advice was correct!

The power supply to the PZEM017 has failed just at the wrong point in time.

Murphy was at work.