This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem in writing code which performs GPRS support action.

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.

Parents
  • 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

Reply
  • 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

Children