CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Please help! Modem <-> RS232 <-> 18F452
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 8:44 am     Reply with quote

I'm still getting no luck with this thing... :'( It seems to print OK all the time, but when I issue a command which returns something else such as CONNECT it just hangs.

THis is my HyperTerminal output (copied and pasted)
Code:

***Program started***           

AT&K0     
GPRS response - OK                 
Clearing buffer...                 
Cleared buffer!               
at+cgdcont=1,"IP","pp.vodafone.co.uk","0.0.0.0",0,0                                                   
GPRS response - OK                 
Clearing buffer...                 
Cleared buffer!               
at#userid="wap"               
GPRS response - OK                 
Clearing buffer...                 
Cleared buffer!               
at#passw="wap"             
GPRS response - OK                 
Clearing buffer...                 
Cleared buffer!               
at#SKTSET= 0,80,"www.inden.co.uk",0,0;
GPRS response - OK
Clearing buffer...
Cleared buffer!
at#sktto=0
GPRS response - OK
Clearing buffer...
Cleared buffer!
at#sktsav
GPRS response - OK
Clearing buffer...
Cleared buffer!
at#sktop
GPRS Timeout - no valid response
GET /qleyo/gprs.php?u=1 HTTP/1.1
Connection: keep-alive
Response received
+++
Response received


And heres the code

Code:

/*
 * @DESCRIPTION Creates connection with server and sends data
 * @RETURN: none
 * @ALGORITHM:  none
 * @NOTES: 
*/

#include <18F452.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=4000000)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C6,RCV=PIN_C7,ERRORS,bits=8,stream=GPRS)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C5,ERRORS,bits=8,stream=PC)

#ifndef NULL
  #define NULL      0
#endif


void delay_sec (void)
{
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
}

void flash()
{
   delay_sec();
   output_low(PIN_B2);
   delay_sec();
   output_high(PIN_B2);
   delay_sec();
   output_low(PIN_B2);
}


/*
 * @DESCRIPTION Waits for a string for the specified timeout
 * @RETURN: none
 * @ALGORITHM:  none
 * @NOTES:  Passing a 0 for the timeout will wait forever
*/

void clear_buffer (char *buf)
{
   int index = 0;

   fprintf(PC,"\r\nClearing buffer...");

   while (index < sizeof(buf))
   {
      buf[index] = NULL;
      index++;
   }

   fprintf(PC,"\r\nCleared buffer!");
}

static int *gprs_response(int16  timeout)    /* How long to wait in miliseconds */
{
  int index = 0;
  char  *p;
  char buf[25];       /* increase the size of the buffer to suit your needs */
  char character;

  char ok[] = { "OK" };
  char connect[] = { "CONNECT" };
  char error[] = { "ERROR" };
  char nocarrier[] = { "NO CARRIER" };

  while (TRUE)
  {
    if (kbhit())
    {
      buf[index] = 0;
      buf[index] = getc();
      buf[index+1] = '\0';
      index++;

      if (index >= sizeof(buf)-1)
      {
         fprintf(PC,"\r\nError reading GPRS data");
         //clear_buffer(buf);
         return(NULL);
      }

      else if (strstr(buf, ok))
      {
         fprintf(PC,"\r\nGPRS response - OK");
         clear_buffer(buf);
         return (TRUE); //drop out of function
      }

      else if (strstr(buf, connect))
      {
         fprintf(PC,"GPRS response - CONNECT");
         clear_buffer(buf);
         return (TRUE); //drop out of function
      }

      else if (strstr(buf, error))
      {
         fprintf(PC,"GPRS response - ERROR");
         clear_buffer(buf);
         return (TRUE); //drop out of function
      }

      else if (strstr(buf, nocarrier))
      {
         fprintf(PC,"GPRS response - NO CARRIER");
         clear_buffer(buf);
         return (TRUE); //drop out of function
      }

    }

    delay_ms(1);

    if (timeout)
    {
      timeout--;
      if (!timeout)
      {
         fprintf(PC,"\r\nGPRS Timeout - no valid response");
         //clear_buffer(buf);
         return (NULL);
      }
    }
  }
}

static int wait_for_response (int16 timeout)
{
   while (!kbhit() || (timeout > 0))
      timeout--;

   fprintf(PC,"\r\nResponse received");
   return (TRUE);
}



void main()
{

  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  setup_spi(FALSE);
  setup_wdt(WDT_OFF);

  /***start test sequence***/
   while (1)
   {
     fprintf(PC,"\r\n***Program started***\n");
     delay_sec();

      /* no handshaking */
     fprintf(PC,"\r\nAT&K0");
     fprintf(GPRS,"AT&K0\r");
     (void)gprs_response(8000);
     flash();

     fprintf(PC,"\r\nat+cgdcont=1,\"IP\",\"pp.vodafone.co.uk\",\"0.0.0.0\",0,0");
     fprintf(GPRS,"at+cgdcont=1,\"IP\",\"pp.vodafone.co.uk\",\"0.0.0.0\",0,0\r");
     (void)gprs_response(4000);
     flash();

     /* set gprs username */
     fprintf(PC,"\r\nat#userid=\"wap\"");
     fprintf(GPRS,"at#userid=\"wap\"\r");
     (void)gprs_response(4000);
     flash();

     /* set gprs password */
     fprintf(PC,"\r\nat#passw=\"wap\"");
     fprintf(GPRS,"at#passw=\"wap\"\r");
     (void)gprs_response(8000);
     flash();

     /* set socket data */
     fprintf(PC,"\r\nat#SKTSET= 0,80,\"www.inden.co.uk\",0,0;");
     fprintf(GPRS,"at#SKTSET= 0,80,\"www.inden.co.uk\",0,0;\r");
     (void)gprs_response(8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktto=0");
     fprintf(GPRS,"at#sktto=0\r");
     (void)gprs_response(8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktsav");
     fprintf(GPRS,"at#sktsav\r");
     (void)gprs_response(8000);
     flash();

     /* open socket connection returns CONNECT*/
     fprintf(PC,"\r\nat#sktop");
     fprintf(GPRS,"at#sktop\r");
     (void)gprs_response(16000);
     flash();

     /* send get request to domain */
     fprintf(PC,"\r\nGET /qleyo/gprs.php?u=1 HTTP/1.1");
     fprintf(GPRS,"GET /qleyo/gprs.php?u=1 HTTP/1.1\r\n");
     delay_ms(1);

      /* keep connection alive if server supports */
     fprintf(PC,"\r\nConnection: keep-alive");
     fprintf(GPRS,"Connection: keep-alive\r\n\r\n");
     (void)wait_for_response(60000);
     delay_ms(1);

     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     /* disconnect */
     fprintf(PC,"\r\n+++");
     fprintf(GPRS,"+++");
     (void)wait_for_response(60000);
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();

   }

}

qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 9:13 am     Reply with quote

Hmm I think I spotted something, well my supervisor and I did, the clear_buffer function I made is slightly useless.

At the same time if kbhit returns falls I added a 1ms delay which might cause the program to lose characters. I've made some amendments and will update in a few minutes (once PIC is done flashing).
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Mar 30, 2006 9:43 am     Reply with quote

You are doing a lot of different compares inside the gprs_response() function. You should pass in a pointer to the string (see my original function) that you are looking for so that it will only check for that string (more efficient). You are most likely missing chars. For debug purposes, print out what's in your buffer when you get an invalid response.
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 10:36 am     Reply with quote

Still no luck, i'll mod the program back to your original function (i.e. send an argument of the string to look for into the function) and also add the print of whats in the buffer after an invalid response. And update you.

However irregardless of timing. The program should still make a connection to the server which it doesn't. :(
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Fri Mar 31, 2006 8:30 am     Reply with quote

Still no luck

Here is hyperterminal output
Code:

***Program started***

AT&K0
at#SKTSET= 0,80,"www.inden.co.uk",0,0;
at#sktto=0
at#sktsav
timeout Buffer contains
v
at#sktop
timeout Buffer contains
p
GET /qleyo/gprs.php?u=1 HTTP/1.1
Connection: keep-alive
Response received
+++
Response received


And here is code
Code:

#include <18F452.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=4000000)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C6,RCV=PIN_C7,ERRORS,bits=8,stream=GPRS)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C5,ERRORS,bits=8,stream=PC)

#ifndef NULL
  #define NULL      0
#endif


void delay_sec (void)
{
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
   delay_ms(200);
}

void flash()
{
   delay_ms(200);
   output_low(PIN_B2);
   delay_ms(200);
   output_high(PIN_B2);
   delay_ms(200);
   output_low(PIN_B2);
}


/*
 * @DESCRIPTION Waits for a string for the specified timeout
 * @RETURN: none
 * @ALGORITHM:  none
 * @NOTES:  Passing a 0 for the timeout will wait forever
*/

char *gprs_response(
  char    *s,         /* String that we are looking for */
  int16  timeout)    /* How long to wait in miliseconds */
{

  int  *p;
  int index = 0;
  int buf[20];       /* increase the size of the buffer to suit your needs */

  while (TRUE)
  {
    if (kbhit())
    {
      buf[index] = 0; 
      buf[index] = getc();
      buf[index+1] = '\0';
      index++;
     
      if (index >= sizeof(buf)-1)
      {
         return(NULL);
      }
     
      p = strstr(buf, s);
     
      if (p)
      {
         return (p); //drop out of function
      }     
    }
   
    if (timeout)
    {   
      timeout--;
      if (!timeout)
      {
        fprintf(PC,"\r\ntimeout Buffer contains %s", buf);
        return (NULL);
      }
    }
  }
}



static int wait_for_response (int16 timeout)
{
   while (!kbhit() || (timeout > 0))
      timeout--;

   fprintf(PC,"\r\nResponse received");
   return (TRUE);
}

void main()
{

   /**declare variables**/
  char ok[] = { "OK" };
  char connect[] = { "CONNECT" };
  char error[] = { "ERROR" };
  char nocarrier[] = { "NO CARRIER" };
 
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  setup_spi(FALSE);
  setup_wdt(WDT_OFF);

  /***start test sequence***/
   
   while (1)
   {
     fprintf(PC,"\r\n***Program started***\n");
     delay_sec();

     fprintf(PC,"\r\nATE0");
     flash();
     
      /* no handshaking */
     fprintf(PC,"\r\nAT&K0");
     fprintf(GPRS,"AT&K0\r");
     gprs_response(ok, 8000);
     flash();

     /* set socket data */
     fprintf(PC,"\r\nat#SKTSET= 0,80,\"www.inden.co.uk\",0,0;");
     fprintf(GPRS,"at#SKTSET= 0,80,\"www.inden.co.uk\",0,0;\r");
     gprs_response(ok, 8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktto=0");
     fprintf(GPRS,"at#sktto=0\r");
     gprs_response(ok, 8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktsav");
     fprintf(GPRS,"at#sktsav\r");
     gprs_response(ok, 8000);
     flash();

     /* open socket connection */
     fprintf(PC,"\r\nat#sktop");
     fprintf(GPRS,"at#sktop\r");
     gprs_response(connect, 100000);
     flash();

     /* send get request to domain */
     fprintf(PC,"\r\nGET /qleyo/gprs.php?u=1 HTTP/1.1");
     fprintf(GPRS,"GET /qleyo/gprs.php?u=1 HTTP/1.1\r\n");
     delay_ms(1);

      /* keep connection alive if server supports */
     fprintf(PC,"\r\nConnection: keep-alive");
     fprintf(GPRS,"Connection: keep-alive\r\n\r\n");
     wait_for_response(0);
     delay_ms(1);

     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     /* disconnect */
     fprintf(PC,"\r\n+++");
     fprintf(GPRS,"+++");
     (void)wait_for_response(0);
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();

   }

}

qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Fri Mar 31, 2006 8:50 am     Reply with quote

I modded my code!!! I got connect!!!! but look I got some more mess too...

Code:

ATE0   
AT&K0AT0       
OK 
at#SKTSET= 0,80,"www.inden.co.uk",0,0;                                     
;
OK 
at#sktto=0         
0
OK 
at#sktsav         
v
OK 
at#sktop       
p
CONNECT
GET /qleyo/gprs.php?u=1 HTTP/1.1
Connection: keep-alive
Response received
+++
Response received
***Program started***

ATE0
H0&K0
OK
at#SKTSET= 0,80,"www.inden.co.uk",0,0;
;
OK
at#sktto=0
0
OK
at#sktsav
v
OK
at#sktop
p
CONNEUH


I modded the function

Code:

char *gprs_response(
  char    *s,         /* String that we are looking for */
  int16  timeout)    /* How long to wait in miliseconds */
{

  int  *p;
  int index = 0;
  int buf[20];       /* increase the size of the buffer to suit your needs */

  while (TRUE)
  {
    if (kbhit())
    {
   
       while ((buf[index] = getc()))
       {
            fprintf(PC,"%c",buf[index]);
            buf[index+1] = '\0';
            index++;
           
            if (index >= sizeof(buf)-1)
            {
               return(NULL);
            }
           
            p = strstr(buf, s);
           
            if (p)
            {
               return (p); //drop out of function
            }     
          }
         
          if (timeout)
          {   
            timeout--;
            if (!timeout)
            {
              fprintf(PC,"\r\ntimeout Buffer contains %s", buf);
              return (NULL);
            }
          }
      }
  }
}

Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 31, 2006 1:38 pm     Reply with quote

Try this:
Code:

#include <18F452.h>
#include <string.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=4000000)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C6,RCV=PIN_C7,ERRORS,bits=8,stream=GPRS)
#use rs232 (BAUD=9600,parity=N,XMIT=PIN_C5,bits=8,stream=PC)

#ifndef NULL
  #define NULL      0
#endif

#define BUFFER_SIZE 32
char buffer[BUFFER_SIZE+1];
int8 buf_index = 0;

#int_rda
void serial_isr() {

   // Don't exceed the size of the buffer
   if (buf_index < BUFFER_SIZE)
   {
     buffer[buf_index]=getc();
     buf_index++;
     // Terminate our string
     buffer[buf_index] = 0;
   }
}

void clear_buffer(void)
{
  disable_interrupts(INT_RDA);
  buf_index = 0;
  buffer[0] = 0;
  enable_interrupts(INT_RDA);
}
#inline
void delay_sec (void)
{
   delay_ms(1000);
}

void flash()
{
   delay_ms(200);
   output_low(PIN_B2);
   delay_ms(200);
   output_high(PIN_B2);
   delay_ms(200);
   output_low(PIN_B2);
}


/*
 * @DESCRIPTION Waits for a string for the specified timeout
 * @RETURN: none
 * @ALGORITHM:  none
 * @NOTES:  Passing a 0 for the timeout will wait forever
*/

char *gprs_response(
  char    *s,         /* String that we are looking for */
  int16  timeout)    /* How long to wait in miliseconds */
{

  int  *p;

  while (TRUE)
  {
    p = strstr(buffer, s);
   
    if (p)
    {
       return (p); //drop out of function
    }     
   
    if (timeout)
    {   
      timeout--;
      if (!timeout)
      {
        fprintf(PC,"\r\ntimeout Buffer contains %s", buffer);
        return (NULL);
      }
    }
  }
}



static int wait_for_response (int16 timeout)
{
   while (buf_index == 0)
   {
      delay_ms(1);
      // if 0 is passed in, then we wait forever or until we receive a char
      if (timeout)
      {
        timeout--;
        if (!timeout)
        {
          fprintf(PC,"\r\nwait_for_response() timed out");
          return (FALSE);
        }
      }
   }
   fprintf(PC,"\r\nwait_for_response() got data");
   return (TRUE);
}

void main()
{

   /**declare variables**/
  char ok[] = { "OK" };
  char connect[] = { "CONNECT" };
  char error[] = { "ERROR" };
  char nocarrier[] = { "NO CARRIER" };
 
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  setup_spi(FALSE);
  setup_wdt(WDT_OFF);

  enable_interrupts(INT_RDA);
  enable_interrupts(GLOBAL);
 
  /***start test sequence***/
   
   while (1)
   {
     fprintf(PC,"\r\n***Program started***\n");
     delay_sec();

     fprintf(PC,"\r\nATE0");
     flash();
     
      /* no handshaking */
     fprintf(PC,"\r\nAT&K0");
     clear_buffer();
     fprintf(GPRS,"AT&K0\r");
     gprs_response(ok, 8000);
     flash();

     /* set socket data */
     fprintf(PC,"\r\nat#SKTSET= 0,80,\"www.inden.co.uk\",0,0;");
     clear_buffer();
     fprintf(GPRS,"at#SKTSET= 0,80,\"www.inden.co.uk\",0,0;\r");
     gprs_response(ok, 8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktto=0");
     clear_buffer();
     fprintf(GPRS,"at#sktto=0\r");
     gprs_response(ok, 8000);
     flash();

     /* save socket settings */
     fprintf(PC,"\r\nat#sktsav");
     clear_buffer();
     fprintf(GPRS,"at#sktsav\r");
     gprs_response(ok, 8000);
     flash();

     /* open socket connection */
     fprintf(PC,"\r\nat#sktop");
     clear_buffer();
     fprintf(GPRS,"at#sktop\r");
     gprs_response(connect, 100000);
     flash();

     /* send get request to domain */
     fprintf(PC,"\r\nGET /qleyo/gprs.php?u=1 HTTP/1.1");
     fprintf(GPRS,"GET /qleyo/gprs.php?u=1 HTTP/1.1\r\n");
     delay_ms(1);

      /* keep connection alive if server supports */
     fprintf(PC,"\r\nConnection: keep-alive");
     clear_buffer();   
     fprintf(GPRS,"Connection: keep-alive\r\n\r\n");
     wait_for_response(0);
     delay_ms(1);

     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     /* disconnect */
     fprintf(PC,"\r\n+++");
     fprintf(GPRS,"+++");
     (void)wait_for_response(0);
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();
     delay_sec();

   }

}
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Sun Apr 09, 2006 7:44 am     Reply with quote

After some mods and a bit more optimising, I got it working :D

Thank you Mark you've been very helpful and everyone else. I'm now working on adding more code to make the system more independent.

As well as a PIC crude XML parser which is being a nightmare (perhaps yet another new topic coming shortly LOL).

Me.x
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group