Serial communication in arduino mega

Hello everybody,

In arduino Uno there is the function Serial.readstring() to read characters from serial buffer into a string.

So now i'm using Arduino Mega 2560 and Serial3, So I'm wondering if there is a similar function because Serial3.readstring() doesn't seem to work!

readString is a member of the Stream class. It's there for every class that inherits from Stream, including every one of the Serial lines on the Mega.

What do you mean when you say it doesn't work? Can you show the code in question? Did it fail to compile? Did it do something you didn't expect? Did it not do something you expected it to do?

Please don't forget to use code tags when you post the code. See the "How to Use This Forum" sticky at the top of the board if you don't know what that means.

void loop() {

        String IncomingString = "";

  while (mySerial.available()) 
 {
   
  IncomingString=mySerial.readString();


 Serial.println("Received String: " + IncomingString);

if (IncomingString == "on")
digitalWrite(13,HIGH);

The above code worked fine for arduino Uno defined software serial pins as 6,7 and it used to receive strings from esp8266 when i send strings from android and it prints the incoming string on the serial monitor.

Now i changed my board to Mega 2560, connected esp8266 to serial3 pins and replaced "mySerial" with "Serial3" as below:

void loop() {

        String IncomingString = "";

 while (Serial3.available()>2) 
 {
   
  IncomingString=Serial3.readString();


 Serial3.println("Received String: " + IncomingString);

if (IncomingString == "on")
digitalWrite(13,HIGH);

There is no compilation errors but i doesn't show the IncomingString from android as it used to do. So i thought there would be a different way to read a string from serials 1,2 and 3 in ardunio Mega!

You may find the examples in Serial Input Basics useful. They are simple, reliable and non-blocking.

...R

The code you posted captures a single byte at a time and then compares that single byte to the two byte "on". You need to capture the incoming bytes into a String, then look for "on". Does the string being sent include an end of data marker?

That's what the function "myserial.readstring" used to do! it captures the entire string. The first code was working perfectly but my problem started when i changed the arduino board to Mega and communication pins to Serial3 instead of serial.

Perhaps you can post all the code that worked on the arduino for a look.

You didn't post all your code, so we can't be sure (this is why we don't like snippets of code - 90% of the time when someone posts a snippet, the problem is elsewhere; which is hardly surprising. If the problem was where they thought it was, they'd have figured it out already!).

My money is on you forgetting to do Serial3.begin() in setup, or something like that.

I guess you owe me money then because i’m defining Serial3.begin .Anyway here is the complete code:

#include <SoftwareSerial.h>
#include <string.h>
#define TIMEOUT 5000 // mS
void setup() {

 Serial.begin(9600);  
 Serial3.begin(9600);
 Serial3.setTimeout(100);

 int CommandStep = 1;
 
 //This initializes the Wifi Module as a server 

 SendCommand("AT+CIPMUX=1","OK",true);
 
 SendCommand("AT+CIPSERVER=1,22","OK",true);
  SendCommand("AT+CIFSR","OK",true);
}

void loop() {

String IncomingString = "";

 while (Serial3.available()>2) 
 {
   
  IncomingString=Serial3.readString();
 

 Serial3.println("Received String: " + IncomingString);

if (IncomingString == "on")
digitalWrite(13,HIGH);}



boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
{
 Serial3.println(cmd); // Send "AT+" command to module

 // Otherwise wait for ack.
 if (!echoFind(ack)) // timed out waiting for ack string
 if (halt_on_fail)
 errorHalt(cmd+" failed");// Critical failure halt.
 else
 return false; // Let the caller handle it.
 return true; // ack blank or ack found
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
 byte current_char = 0;
 byte keyword_length = keyword.length();
 
 // Fail if the target string has not been sent by deadline.
 long deadline = millis() + TIMEOUT;
 while(millis() < deadline)
 {
 if (Serial3.available())
 {
 char ch = Serial3.read();
 Serial.write(ch);
 if (ch == keyword[current_char])
 if (++current_char == keyword_length)
 {
 Serial.println();
 return true;
 }
 }
 }
 return false; // Timed out
}

// Print error message and loop stop.
void errorHalt(String msg)
{
 Serial.println(msg);
 Serial.println("HALT");

 while(true){};
}

Please note that the same code was working fine on arduino Uno but of course with “mySerial” instead of “Serial3” because there is no serial3 on arduino Uno.

Hm, I’m not seeing anything that explains why they would behave differently. I suspect that you’re unknowingly relying on timing of softwareSerial…

Do you mean to have this printing to serial3?

Serial3.println("Received String: " + IncomingString);

I ask because it looks like Serial3 is the one that the ESP8266 is on, but that looks like a debug statement that you’d want on the serial monitor.

I do not know why AutoFormat does not complain.
I assume there is something else wrong.

You are missing closing bracket in loop to start with.

[code]
#include <SoftwareSerial.h>
#include <string.h>
#define TIMEOUT 5000 // mS
void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);
  Serial3.setTimeout(100);
  int CommandStep = 1;
  //This initializes the Wifi Module as a server
  SendCommand("AT+CIPMUX=1", "OK", true);
  SendCommand("AT+CIPSERVER=1,22", "OK", true);
  SendCommand("AT+CIFSR", "OK", true);
}

void loop() {
  String IncomingString = "";
  while (Serial3.available() > 2)
  {
    IncomingString = Serial3.readString();
    Serial3.println("Received String: " + IncomingString);
    if (IncomingString == "on")
      digitalWrite(13, HIGH);
  }
  boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
  {
    Serial3.println(cmd); // Send "AT+" command to module
    // Otherwise wait for ack.
    if (!echoFind(ack)) // timed out waiting for ack string
      if (halt_on_fail)
        errorHalt(cmd + " failed"); // Critical failure halt.
      else
        return false; // Let the caller handle it.
    return true; // ack blank or ack found
  }
  // Read characters from WiFi module and echo to serial until keyword occurs or timeout.
  boolean echoFind(String keyword)
  {
    byte current_char = 0;
    byte keyword_length = keyword.length();
    // Fail if the target string has not been sent by deadline.
    long deadline = millis() + TIMEOUT;
    while (millis() < deadline)
    {
      if (Serial3.available())
      {
        char ch = Serial3.read();
        Serial.write(ch);
        if (ch == keyword[current_char])
          if (++current_char == keyword_length)
          {
            Serial.println();
            return true;
          }
      }
    }
    return false; // Timed out
  }
  // Print error message and loop stop.
  void errorHalt(String msg)
  {
    Serial.println(msg);
    Serial.println("HALT");
    while (true) {};
  }

[/code]

Please note that the same code was working fine on arduino Uno but of course with “mySerial” instead of “Serial3” because there is no serial3 on arduino Uno.

In the code you posted that supposed to have worked, i changed all the “Serial3” instances to “mySerial”, then tried a compile. That resulted in the error"“mySerial” was not declared in this scope". So you don’t seem to be correct with what you are saying/posting. Below is the code you posted with your changes made. Did I miss one?

#include <SoftwareSerial.h>
#include <string.h>
#define TIMEOUT 5000 // mS
void setup() {

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

 int CommandStep = 1;
 
 //This initializes the Wifi Module as a server

 SendCommand("AT+CIPMUX=1","OK",true);
 
 SendCommand("AT+CIPSERVER=1,22","OK",true);
  SendCommand("AT+CIFSR","OK",true);
}

void loop() {

String IncomingString = "";

 while (mySerial.available()>2)
 {
   
  IncomingString=mySerial.readString();
 

 mySerial.println("Received String: " + IncomingString);

if (IncomingString == "on")
digitalWrite(13,HIGH);}



boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
{
 mySerial.println(cmd); // Send "AT+" command to module

 // Otherwise wait for ack.
 if (!echoFind(ack)) // timed out waiting for ack string
 if (halt_on_fail)
 errorHalt(cmd+" failed");// Critical failure halt.
 else
 return false; // Let the caller handle it.
 return true; // ack blank or ack found
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
 byte current_char = 0;
 byte keyword_length = keyword.length();
 
 // Fail if the target string has not been sent by deadline.
 long deadline = millis() + TIMEOUT;
 while(millis() < deadline)
 {
 if (mySerial.available())
 {
 char ch = mySerial.read();
 Serial.write(ch);
 if (ch == keyword[current_char])
 if (++current_char == keyword_length)
 {
 Serial.println();
 return true;
 }
 }
 }
 return false; // Timed out
}

// Print error message and loop stop.
void errorHalt(String msg)
{
 Serial.println(msg);
 Serial.println("HALT");

 while(true){};
}

Robin2: You may find the examples in Serial Input Basics useful. They are simple, reliable and non-blocking.

...R

You WILL for sure also find that your COM monitor is connected to "Serial" and you are printing to Serial3. ( In your original code, I do not see what your "new" code is trying to do - what is mySerial? ) Could you read the data from SerialX and print it to Serial?

Vaclav: You WILL for sure also find that your COM monitor is connected to "Serial"

I don't understand what your comment has to do with my code?

...R

Robin2: I don't understand what your comment has to do with my code?

...R

Same thing your code has to do with problem at hand. If you want to be helpful, which I seriously doubt, you need to quit being so generic all the time. It is getting old, my friend.

PS This of course my opinion, but you can judge for yourself. How may replies have you gotten on your generic post so far? That should an indicator to you , shouldn't it?