I am trying to send data through GPRS via SIM300 modem. The logic of my program should be. 1) it should send the data till it receives "dieman" from the server. 2) the modem is such that after every command there is a reply. 3) all the communication takes place serially.
Firstly the GPRS commands as tried in hyperterminal
AT OK AT+CGATT=1 OK AT+CGDCONT=1,"IP","airtelgprs.com" OK AT+CSTT="airtelgprs.com","","" OK AT+CIICR OK AT+CIFSR 212.22.22.22 //EG OF AN IP ADDRESS AT+CIPSTART="TCP",""80"">http://www.google.com","80" OK CONNECT OK AT+CIPSEND >THIS IS THE DATA I WANNA SEND // YOU CAN SEND THE DATA VIA GPRS NOW [CTRL+Z] SEND OK
this is what i want to implement in C in 8052. here OK are the response from the modem.
now i implemented the C version but my code is very inefficient. I could have ignored the response from the modem by simply inserting delays but i want my code such that if the reply from the modem is not "OK" it should transmit the command again.
the C code is as follows
#include<reg52.h> #include<string.h> unsigned char msg1[100],x=0; unsigned char *s1="AT"; unsigned char *s2="AT+CGATT=1"; unsigned char *s3="AT+CGDCONT=1,\”airtelgprs.com\”"; unsigned char *s4="AT+CSTT=\”airtelgprs.com\”,\""\",\"""\"; unsigned char *s5="AT+CIICR"; unsigned char *s6="AT+CIFSR"; unsigned char *s7="AT+CIPSTART=\”TCP\”,\”www.google.com\”,\”80\””; unsigned char *s8="AT+CIPSEND"; unsigned char *s9="AT+CLOSE"; unsigned char *s10="AT+SHUT"; void send(unsigned char *x) { while(*x!='0') tx(*x); } void tx(unsigned char x) { SBUF=x; while(TI==0); TI=0; } void delay(void) { unsigned int i; for(i=0,i<=65535;i++); } void serialinit() { IE=0X90; TMOD=0X20; TH1=0XFD; SCON=0X50H; TRI=1; } void serialrx(void) interrupt 4 { msg1[x]=SBUF; RI=0; x++; } void main() { char ch; serialinit(); do { send(s1); //send AT delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s2); //send AT+CGATT=1 delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s3); //send AT+CGDCONT=1,"IP","airtelgprs.com" delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s4); //send AT+CSTT="airtelgprs.com","","" delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s5); //send AT+CIICR delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s6); //send AT+CIFSR delay(); if(msg[3]>=48 || msg<=57) // its an ip address so it has to be a no. so ascii value compared ch=1; else ch=0; } while(ch==0); send(s7); // SEND AT+CIPSTART="TCP",""80"">http://www.google.com","80" delay(); // now here there are 2 responses OK CONNECT OK i dont how to compare // and hence i am just waiting for sometime send(s8); while(msg(0)!='>'); while(strncmp(msg,"\r\ndieman")!=0) send("this is what i want to send"); do { send(s9); //send AT+CIPCLOSE delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0); do { send(s10); //send AT+CIPSHUT delay(); if(!strncmp(msg,"\r\nOK",4)) //compare if OK is received ch=1; else ch=0; } while(ch==0);
pls help me find the correct code
Time for you to improve your coding style a bit.
Why do you have variables named s1 to s10? Do you really think that is good names?
Don't use busyloops as delays. They can change their speed very, very much depending on compiler version, optimization levels, linker optimizations etc.
Don't you see a problem when you have code like:
send(s5); //send AT+CIICR
Doesn't it feel silly that first you have code using the non-meaningful name "s5" and then you need to add a comment AT+CIICR just to remember what you are sending?
Why do you have so many code sequences containing a call to send() a call to delay() and then checking the result? Why haven't you combined this into a single function call?
delay(); // now here there are 2 responses OK CONNECT OK i dont how to compare // and hence i am just waiting for sometime
Do you really think the above is acceptable? It's just time you do learn how to receive and process data. Just relying in a delay does not work. I repeat - it does not work! You have already been told that in a different thread, so why do you still do it?
Have you thought about the difference in answers if you let the modem send an echo back or not? What have you done about it?
Similarly for your functions 'send' and 'tx' - do those names clearly describe precisely what those functions do and, in particular, the difference between the two functions?