Serial Port reding garbage Value

I had started using Arduino Uno Boards and following tutorials, I watched the tutorial in GSM shield for Arduino which is very interesting. I used the code to send SMS it is working fine.

I did little modification from the example program available from Arduino forum to accept mobile number and SMS message during execution. After uploading the code and enter serial monitor and switching on the GSM module, I am getting garbage value printing in the serial port.

I am attaching the code for your kind reference.

/int timesTosend =1;
int count = 0;
void setup() {
Serial.begin(9600);
delay(2000);
Serial.println("AT+CMGF=1");
delay(200);
  
}

void loop() {
while(count<timesTosend) {
delay(1500);

Serial.print("Enter a mobile number: ");
char remoteNum[20];  // telephone number to send sms
char phone_no[20];  // telephone number to send sms

readSerial(remoteNum);
Serial.println(remoteNum);

Serial.print("Now, enter SMS content: ");
 char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);
  
Serial.print("AT+CMGS=\"");
Serial.print(phone_no);
Serial.println("\"");

while(Serial.read()!='>');
  {
Serial.print(txtMsg);
delay(500);
Serial.write(0x1A);
Serial.write(0x0D);
Serial.write(0x0A);
  }

count++;
}

}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int i = 0;
   while (1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
     
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}[code]

In your function readSeria(), you don't do any check for input buffer overrun. I'm sure this isn't a problem, but one never knows when an idiot is going to run the program. Also, you might simplify the function a bit:

/*****
     Read input from Serial and place in input buffer. 

   Argument List:
      char result[]           the input buffer
       int maxChar          the input buffer size

   Return value:
      int                         0 on error, >0 = length of input stream

   CAUTION: Waits for input stream to read a newline char ('\n') for input termination
*****/

int readSerial(char result[], int maxChar)
{
  int index = 0;

  while (Serial.available() > 0)
  {
      index = Serial.readBytesUntil('\n', result, maxChar - 1);       // Look for newline...
      result[index] = '\0';                                           // Now it's a string
  }
  return index;    // Length of input, or 0 if nothing read
}

This code is untested. Also, before you post your code, try using Ctrl-T to reformat it in a standard style. Also, it would help us if you tell us what you input via Serial and what you're getting back.

myuino: I am attaching the code for your kind reference.

Some of the code is missing. Please post a complete program.

Is your SMS device connected to the same pins as you are using to send data to the Serial Monitor - that may well be the problem.

What do you see on the Serial Monitor and what should you see ?

Have you checked that the Serial Monitor is using the same baud rate as your program ?

The examples in serial input basics are simple and reliable ways to receive data. There is a user input function in planning and implementing a program.

...R

Ribin I will be receiving receiver phone number and text message through serial port. I think what jack is telling is correct, I will try and update. jack do we need to give some delay for hardware response in the serial monitor

I am getting the following error now

In function ‘void loop()’:
GSM_First_Code:25: error: ‘readSerial’ was not declared in this scope
GSM_First_Code.ino: At global scope:
GSM_First_Code:57: error: ISO C++ forbids declaration of ‘readSerial’ with no type [-fpermissive]
‘readSerial’ was not declared in this scope

int timesTosend = 1;
int count = 0;
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.println("AT+CMGF=1");
  delay(200);

}

void loop()

{
  while (count < timesTosend)

  {
    delay(1500);

    Serial.print("Enter a mobile number: ");
    int index;
    char remoteNum[20];  // telephone number to send sms
    char phone_no[20];  // telephone number to send sms

    readSerial(remoteNum);
    Serial.println(remoteNum);

    Serial.print("Now, enter SMS content: ");
    char txtMsg[200];
    readSerial(txtMsg);
    Serial.println("SENDING");
    Serial.println();
    Serial.println("Message:");
    Serial.println(txtMsg);

    Serial.print("AT+CMGS=\"");
    Serial.print(phone_no);
    Serial.println("\"");

    while (Serial.read() != '>');
    {
      Serial.print(txtMsg);
      delay(500);
      Serial.write(0x1A);
      Serial.write(0x0D);
      Serial.write(0x0A);
    }

    count++;
  }

}

/*
  Read input serial
 */
readSerial(char result[])
{
  int index = 0;

  while (Serial.available() > 0)
  {

    index = Serial.readBytesUntil('\n', result, 24);
    result[index] = '\0';

  }

  return index;
}

This line

readSerial(char result[])

should be

int readSerial(char result[])

because it returns an int. If it returned nothing it would use void rather than int as in void loop()

Ribin I will be receiving receiver phone number and text message through serial port

Please give a specific example along with the actual garbage that is being received. The garbage can be very informative.

...R

I am using GSM shield with Arduino Uno, written code for accepting mobile number and text message, The serial monitor reading junk value, it is not waiting for user to enter, the condition is
Serial.available() > 0 is not working.

int timesTosend = 1;
int count = 0;
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.println("AT+CMGF=1");
  delay(200);

}

void loop()

{
  while (count < timesTosend)

  {
    delay(1500);

    Serial.print("Enter a mobile number: ");
    int index;
    char remoteNum[20];  // telephone number to send sms
    char phone_no[20];  // telephone number to send sms

    readSerial(remoteNum);
    Serial.println(remoteNum);

    Serial.print("Now, enter SMS content: ");
    char txtMsg[200];
    readSerial(txtMsg);
    Serial.println();
    Serial.println("SENDING");
    Serial.println();
    Serial.println("Message:");
    Serial.println(txtMsg);

    Serial.print("AT+CMGS=\"");
    Serial.print(phone_no);
    Serial.println("\"");

    while (Serial.read() != '>');
    {
      Serial.print(txtMsg);
      delay(500);
      Serial.write(0x1A);
      Serial.write(0x0D);
      Serial.write(0x0A);
    }

    count++;
  }

}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int index = 0;

  while (Serial.available() > 0) :confused: 
  {
    index = Serial.readBytesUntil('\n', result, 24);
    result[index] = '\0';

  }

  return index;
}

Serial Monitor Output:

AT+CMGF=1
Enter a mobile number: OK

Now, enter SMS content: 
SENDING

Message:
°‰‰‰‰‰€
AT+CMGS="Í÷ï_å¿¿ý.Ïg÷ž»ñ««"

Image.png

  while (Serial.available() > 0) :confused:

I'm guessing the compiler barfed on that.

Let's imagine for a moment that the ":confused:" isn't there.

Let's also imagine that nothing has been sent yet from the the serial monitor. Serial.available returns zero, so "readSerial" returns zero.

What happens to that value zero? Oh dear.

int readSerial(char result[])
{
  int index = 0;

while (Serial.available() > 0)   {
    index = Serial.readBytesUntil('\n', result, 24);
    result[index] = '\0';

  }

I don’t know the semantics of available() but I’d say do this:

int readSerial(char result[])
{
  int index = 0;

  // Wait for data
  while (Serial.available() <= 0); 

  // Read the data
  index = Serial.readBytesUntil('\n', result, 24);
  result[index] = '\0';
  }

The garbage IMHO is because you did not init your buffer. Put a \0 as 1st character in setup().

Hi, Have you got monitor set to 9600?

Tom.... :)

Why have you started a new Thread when this project was already moving along nicely in your other Thread?

I have suggested to the Moderator to merge them.

...R

Don't you feel really used when you've wasted your time answering a question that has already been answered?

Does this make you feel inclined to further help the questioner?

No, I thought not.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

Hello Every one

I am trying to send SMS text message using GSM shield using Arduino, this is in continuation with previous post I had, the link given below

https://forum.arduino.cc/index.php?topic=333312.msg2300286#msg2300286

This time I am successful in reading data from Serial port without garbage value, thanks for the forum for helping. The code is posted below.

According to the code two inputs 1) Phone Number 2) Message

I am forced to remove the Rx(pin 0) and Tx (Pin 1) in Arduino connecting to GSM shield still the user input the data, otherwise the data is getting lost some were, Once I received the input I had given delay(5000) by the time I will connect back the two pin, the message is successfully sent is their any other way.

 int count = 0;
char Input[20];  
String Phone_num; // Mobile number to send sms
String txtMsg; // SMS Text Message;
int Flag = 0;


void setup() {
  Serial.begin(9600);
  delay(1000);
  
}

void loop()
{
  if(Flag==0)
  {
    Serial.print("Enter Phone Number: ");
    Flag = 2;

  } 
  else if(Flag==1) {

    Serial.print("Enter Message: ");
    Flag = 3;

  } 

  if (readSerial(Input)) 
  {
    if(Flag==2) {

      Phone_num = String(Input);
      Serial.println(Phone_num);
      Flag = 1;

    } 
    else if(Flag==3) {

      txtMsg = String(Input); 
      Serial.println(txtMsg);
      delay(5000);
      Send_SMS();
      Flag = 0;

    }


  }  

}

void Send_SMS()
{

  Serial.println("Sending Message Pls Wait...");
  Serial.println("AT+CMGF=1"); // Activate for SMS Mode
  delay(1700);
  Serial.print("AT+CMGS=\"");
  Serial.print(Phone_num);
  Serial.println("\"");
  
  while(Serial.read()!='>');
  {
    Serial.print(txtMsg);
    delay(500);
    Serial.write(0x1A); // Sends Ctrl+z end of message
    Serial.write(0x0D); // Carriage Return in Hex
    Serial.write(0x0a); // Line feed in Hex
    delay(5000);
  }

}

/*
  Read input serial
 */
boolean  readSerial(char result[])
{
  int index = 0;

  while (Serial.available() > 0)
  {
    index = Serial.readBytesUntil('\n', result, 20);
    result[index] = '\0';

  }

  return index;
}

-- Thanks in Advance

This is your THIRD thread on the same subject, even though you were warned about cross posting already.

Have you looked at Reply #2 in your SECOND thread ?

...R

Moderator: discussion/posts merged as they are about same project.

I apologizes for starting for new thread, I will continue with same thread,

How to over come the data lost problem in Serial port as requested in previous query, can some one help.

-- Thanks in Advance

myuino: I am forced to remove the Rx(pin 0) and Tx (Pin 1) in Arduino connecting to GSM shield still the user input the data, otherwise the data is getting lost some were

This leads me to think (as I asked in Reply #2) that you are trying to use Pins 0 and 1 for two different purposes - connecting to your SMS device and communicating with your PC via USB.

Is that the case ?

...R

Robin you are correct, exactly I am using Pin 0 and Pin 1 for two different purpose,

1) communicating with PC through USB interface with Pin 0 and Pin 1

2) Connecting to GSM through pins 0 and Pin 1

is their any problem pls address this issue

You can’t one serial port do two things at the same time.
If both things transmit, Arduino gets garbled data.
If Arduino transmits, both things need to somehow ignore what is not meant for them.

Easier way might be to add some external hardware so that only one device connects to the Arduino at a time -of course, that’s a little harder to do with the onboard serial connection to the USB/Serial chip.

Easier method - Software serial to GSM, or a uC with two hardware serial ports, like Atmega1284P.

Easier method - Software serial to GSM, or a uC with two hardware serial ports, like Atmega1284P

Sounds great, I understand Other pins of Arduino Uno can be reconfigured using software serial but, dynamically is it possible to change to default pins 0 & 1 during execution.

this looks very interesting problem for beginner like me.