Software Serial

Hello,
I am trying to implement the Software Serial on Arduino MEGA 2560. I just am executing the example code that is provided in in the Learning section and no data is printing. Obviously, I removed the Serial.write to Serial.print for displaying. This is the code I used.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(50, 51); // RX, TX

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);

// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
Serial.println(“Goodnight moon!”);
mySerial.write(“Hello, world?”);
}

void loop() // run over and over
{

if (mySerial.available())
Serial.println(mySerial.read());
if (Serial.available())
mySerial.println(Serial.read());
}

It should have printed “Hello, world?” but nothing is printing other than “Goodnight moon” :frowning: :frowning: Please help me.

Thank you

Software Serial is half duplex, which means you can transmit data or receive data, but not both at the same time. Is that what you are trying to do?

Circuit?

@mauried: No, I am just implementing the code that is given the arduino. As you can see in the code, the data is sent to both Serial0 and Software serial in void setup once. The sent data will be repeatedly interchanged from serial0 to software serial.

@JimboZA: I am just connecting arduino to my laptop. No, external circuit is used.

RanjithShanbhag: @JimboZA: I am just connecting arduino to my laptop. No, external circuit is used.

So nothing connected to your soft Rx and Tx pins.....

Y cant you print directly Instead of the code

just in loop

void loop()
{
Serial.println("Serial print working");
 mySerial.println("Hello, world?");
}

Set baudarated

void setup() {

Serial.begin(9600); mySerial.begin(9600);

}

I wonder if you are mistakenly assuming that what is written to SoftwareSerial will appear on the Arduino Serial Monitor? It won't. Only output from HardwareSerial (i.e. Serial.print() ) will appear on the Serial Monitor.

The purpose of Software Serial is to communicate with something else (such as a bluetooth device) connected (in your example) to pins 50 and 51.

It is unlikely to be necessary to use Software Serial on a Mega because it has 4 Hardware Serial ports which work much better than SoftwareSerial, which was written to allow users of the Uno to communicate with a second device.

...R