2 Simultaneous Serial Software

I have an Arduino MEGA and I am trying to use 2 devices composed of RX and TX pins in it, however I am having difficulties.

The devices are:

  • Nextion LCD Display

  • VR Recognizer V3 (Voice Recognition Module)

When I use the code according to their library, I have the following problem ... Both receive Arduino commands, the Arduino manages to send commands to both, BUT only 1 module can send commands to the Arduino (in this case always the voice module ).

I discovered that it is possible to use several pins of communication with the Arduino MEGA, I tried to do this but the commands are sent "raw" to the Arduino, plus I have to try to figure out how to read them, I also have the problematic to use their library facilities ...

#include <SoftwareSerial.h> // (DISPLAY)
#include <Nextion.h> // (DISPLAY)
SoftwareSerial nextion(10, 11); // (DISPLAY)
Nextion myNextion(nextion, 9600); // (DISPLAY)

#include "VoiceRecognitionV3.h" // (VOICE MODULE)
SoftwareSerial VR myVR(53, 52); // (VOICE MODULE)

This is the default configuration (according to each library), but when I use this, only one module can communicate 100% with Arduino, the other only receives data ....

What can I do to fix this? Is there anyway to use them without using Rx1,Tx1,Rx2 and Tx2 pins?

An Arduino Mega has 3 spare Hardware Serial ports. Why would you want to use SoftwareSerial?

...R

Robin2:
An Arduino Mega has 3 spare Hardware Serial ports. Why would you want to use SoftwareSerial?

...R

As I said, the problem is that when I use the hardware ports, my devices send "raw" codes.. I mean, they don't send "Display.open.door" they send "0x00321f" (just an example) and if I do that, I could not use "myNextion.sendCommand("Hello"), I've to change all my code...

Hbadotti:
As I said, the problem is that when I use the hardware ports, my devices send "raw" codes.. I mean, they don't send "Display.open.door" they send "0x00321f" (just an example) and if I do that, I could not use "myNextion.sendCommand("Hello"), I've to change all my code...

They send whatever you want. Just like SoftwareSerial. The only difference is that Serial# uses a hardware UART that can reliably send and receive data, independently of what the CPU and the other UARTs are doing, while you can only send or receive on one SoftwareSerial port at any given time, and you can't do anything else at the same time.
Both HardwareSerial and SoftwareSerial inherit from the same Stream class, and the write, print and read functions should do exactly the same thing. HardwareSerial just does it more reliably and efficiently.

Pieter

Hbadotti:
As I said, the problem is that when I use the hardware ports, my devices send "raw" codes..

Nonsense. The nature of the code has nothing to do with the nature of the ports. If you write bad code, using Software serial will not save you, and using software serial on a Mega is a particularly dumb idea. I guess it is probably slack serial input code, in which case you might look at
http://forum.arduino.cc/index.php?topic=396450.0
done by the other guy.

It is even possible that using Software serial (gasp!) twice is actually the cause of your problem. I guess, just as an intellectual exercise, you can prove this by successfully using the devices by themselves

PieterP:
They send whatever you want. Just like SoftwareSerial. The only difference is that Serial# uses a hardware UART that can reliably send and receive data, independently of what the CPU and the other UARTs are doing, while you can only send or receive on one SoftwareSerial port at any given time, and you can't do anything else at the same time.
Both HardwareSerial and SoftwareSerial inherit from the same Stream class, and the write, print and read functions should do exactly the same thing. HardwareSerial just does it more reliably and efficiently.

Pieter

Awesome, I've to figure out how to adapt my code...

Nick_Pyner:
Nonsense. The nature of the code has nothing to do with the nature of the ports. If you write bad code, using Software serial will not save you, and using software serial on a Mega is a particularly dumb idea. I guess it is probably slack serial input code, in which case you might look at
Serial Input Basics - updated - Introductory Tutorials - Arduino Forum
done by the other guy.

It is even possible that using Software serial (gasp!) twice is actually the cause of your problem. I guess, just as an intellectual exercise, you can prove this by successfully using the devices by themselves

Yea, I know the cause is the use of 2 Software serial, this's my first project, I'm still learning... I took a look about the hardware serial, I made some tests, but they were uncucessful, I was getting annoying values... Maybe the problem was the code... I'm excited to get in home today and try again... I got in the link which you sent, seens to be helpful.

Thanks a lot... I'll post the results later, maybe tomorrow.

So, I'm still having problem with it... But got some progress...

I'm trying to make my Nextion display to work in RX2, TX2 ports. I was taking a look in it library ( GitHub - bborncr/nextion: A simple Nextion HMI library for Arduino ) and I saw this:

However, Hardware Serial can be used also:

In the file Nextion.h

Comment out the following code (line 47):

#define USE_SOFTWARE_SERIAL

Remove the lines:

#include <SoftwareSerial.h>

SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino

Add the following define to assign a hardware serial port to "nextion":

#define nextion Serial1

Note that you can replace "Serial1" with Serial or Serial2 or whatever Serial ports your particular Arduino has.

I changed the .h archive, but I still can't figure out how to make it work...

I uploaded this code

#include <Nextion.h> // (DISPLAY)
#define nextion Serial2


void setup()
{
  Serial.begin(9600); // (Arduino)
  Serial2.begin(9600); // (Display)
}

void loop()
{
  String message = nextion.listen(); // nextion, myNextion or simply Serial2, all doesn't work
  Serial.println(message);
}

In software serial examples, the autor of library uses this command to read everything that is sent from display, but when I use it, I get this error:

Hardware_Serial:25: error: 'class HardwareSerial' has no member named 'listen'

String message = nextion.listen();

^

exit status 1
'class HardwareSerial' has no member named 'listen'

That's an exemple of working code using Software Serial...

#include <SoftwareSerial.h>
#include <Nextion.h>

SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino

Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

void setup() {
  Serial.begin(9600);
  myNextion.init();

}

void loop() {
  String message = myNextion.listen(); //check for message
  if(message != ""){ // if a message is received...
    Serial.println(message); //...print it out
  }

  
}

If I could send Hex commands to my Nextion, it may can work, how can I do that? Or even better, how can I make this "listen" commands work properly?

Ok, I found the problem hahahaha. I got in contact with the owner of library, it was missing

Nextion myNextion(nextion, 9600);
myNextion.init();

from my code... :smiley:

It's working 100% now. Thanks for support and tips guys.

Now I've to figure out how to make my Voice Module work in Tx3,Rx3 pins.