# Error getting data from GSM SIM900

I am interfacing with GSM SIM900. I wrote a program so that I can read the SMS in SIM900. I have made a function to transmit and receive. Whenever msg comes, GSM responds with +CMT along with slot number.I am trying to extract that slot number which is in digits, but I am getting garbage values

Here is the code:

while(1)
{
if (serial_Rx() != NULL) // SOMETHING PRESENT TO BE READ
{
serial_Tx("done"); // print done
PORTC = 0x01; // turn on led
for (int i=0;i<=14;i++)
{
data[i] = serial_Rx(); //storing received in data

if(isdigit(data[i]))
{
serial_Tx("enter");
PORTC = 0x02;
data1[i] = data[i];
serial_Tx(data1[i]);
}
}
}
}


void serial_Tx(char *str)
{
for (unsigned int i=0;str[i]!=0;i++)
{
UDR=str[i];
while(!(UCSRA&(1<<UDRE)));
}
}

char serial_Rx()
{
//Wait untill a data is available
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}

//Now USART has got data from host and is available is buffer
return UDR;
}

void uart_put(char data)
{
UDR=data;
while(!(UCSRA&(1<<UDRE)));
}


Hyperterminal:

• If I remeber correctly your serial_TX functions expects a string (char*) as param but I suppose k is a single char variable, try with the uart_put function. You should have got a warning about this when you have built the code. – Bence Kaulics Jul 29 '15 at 8:30
• @BenceKaulics Hey I have made few changes in the code. I have removed the second for loop and included the isdigit function in first loop. I am getting enter on output that means control is entering my isdigit function and has found that digit but serial_Tx(data1[i]) is not running. Can you tell me whats wrong in this.? – anna carolina Jul 29 '15 at 12:17
• You get the "enter" but nothing else? – Bence Kaulics Jul 29 '15 at 12:32
• @BenceKaulics yeah and even i checked it further by putting it in a if condition like if (data[i] >=5) {serial_Tx("match");else (serial_Tx("NO MATCH"), so I was getting match on output. I used >=5 because I didnt know the exact number and I assumed it to be greater than 5 because i know it may be 7 or 8 – anna carolina Jul 29 '15 at 12:40
• Okey so you should definetly read about Strings, chars, and char array, char pointers in C. Serial_TX is for sending a string like "enter" or "match" but k and data[i] is a single char like 'a', 'b', 'c'. Try with uart_put(data[i]). – Bence Kaulics Jul 29 '15 at 13:00

In your code the following function call is wrong:

serial_Tx(data1[i]);


Because data1[i] has a type of char but void serial_Tx(char *str) expects a char*, this function is for sending strings, not single characters. This way it causes runtime error, hence you won't get the expected output.

Though, it won't generate a compilation error, but the compiler will give a warning like this:

It is recommended to check and fix these warnings, because as you could see they could cause troubles in runtime.

So, replace the function above with the void uart_put(char data) function, like:

uart_put(data[i]);


and it will work.

• it keep on showing the same value untill I press the reset switch. After reset, it shows new updated value, is it because the data in data1 is not getting cleared? – anna carolina Jul 30 '15 at 4:17