1

I'm using a library that, by default, wants to use Serial2 as the interface to a display, however, I'm working with an UNO which only has one serial interface, so the compile fails. I'm trying to use a SoftwareSerial instead, but I can't figure out how to override the #define statement in the library without editing the library.

I'm guessing since the software serial port isn't generated at compile time, I can't.

#include <SoftwareSerial.h>
#define nexSerial HMI
#include <Nextion.h>

SoftwareSerial HMI(8,9);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

I haven't started writing code yet, can't get past the attempt to redirect the serial interface to a software serial.

1

You're allowed to modify the header file. The version I looked at says

#define USE_SOFTWARE_SERIAL //Comment this line for use  HardwareSerial

...but I think he meant "comment this line out to use HardwareSerial". Or conversely, "uncomment this line to use SoftwareSerial."

Then you just pass your SoftwareSerial instance into the Nextion constructor:

SoftwareSerial HMI( 8,9 );
Nextion nextion( HMI );

void setup()
{
   HMI.begin( 9600 ); // or whatever speed...

However, because you are using pins 8 & 9, I would strongly recommend using AltSoftSerial instead. It is much more reliable and efficient. MUCH.

You would have to modify the Nextion files to either (1) search and replace all occurrences of SoftwareSerial with AltSoftSerial, or (2) add a third choice to the author's #ifdef/#endif sections. For example:

#define USE_SOFTWARE_SERIAL // uncomment this line to use SoftwareSerial
#define USE_ALTSOFT_SERIAL  // uncomment this line to use AltSoftSerial
                            // comment both above  to use HardwareSerial

#if defined(USE_SOFTWARE_SERIAL)
#include <SoftwareSerial.h>
#elsif definied(USE_ALT_SOFTSERIAL)
#include <AltSoftSerial.h>
#endif

class Nextion{
 private:
  void flushSerial();
#if defined(USE_SOFTWARE_SERIAL)
  SoftwareSerial *nextion;
#elsif defined(USE_ALTSOFT_SERIAL)
  AltSoftSerial *nextion;
#else
  HardwareSerial *nextion;
#endif

Do either one of those things in all files that refer to the Nextion serial member; Nextion.CPP certainly needs it.

  • Thank you. I'll have to go get the newest version of the library, as the one I have doesn't show a software serial option. I'll also look into AltSoftSerial, as I'm having trouble with baud rates above 19200. – Prdufresne Feb 9 '16 at 18:15
0

You can't.

All you are doing in your code is providing a #define to the instance of the header file that is included in your sketch file. You have no way of modifying the instance that is included in the library's source files.

  • I was afraid of that. Fortunately, it looks like a new version of the library will work with software serial. – Prdufresne Feb 9 '16 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.