Wednesday, 30 September 2015

c - 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]);

}
}
}
}

Transmit and receive funstions:


void serial_Tx(char *str)
{
for (unsigned int i=0;str[i]!=0;i++)
{

UDR=str[i];
while(!(UCSRA&(1< }
}

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

//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<}

Hyperterminal:


enter image description here



Answer



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:


enter image description here


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.


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...