Problems with Custom Software Serial I/F

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.

read function.txt (1.58 KB)

#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

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

If I declare it globaly, the I/F does not work.

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.