Go Down

Topic: Problems with Custom Software Serial I/F (Read 1 time) previous topic - next topic

Klausi1967

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.

Idahowalker

#1
Oct 18, 2019, 03:04 pm Last Edit: Oct 18, 2019, 03:07 pm by Idahowalker
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);
}

Delta_G

Google "C++ variable scope" to learn why this happens. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Klausi1967

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

Delta_G

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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Klausi1967

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.

Go Up