PIC to MAX232

bwack

Joined Nov 15, 2011
113
There is a bug in case 20

C:
        case 20:{  // +COPS: 0,2,47201 or +COPS: 0,2,47202
          if(Uart_Read == '1'){ //we have '1', it could be +COPS: 0,2,47201
            response  = GSM_DhiMobile;
             gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
           else if(Uart_Read == '2'){ //we have '2', it could be +COPS: 0,2,47202
            response = GSM_Ooredoo;
            gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
          else
         break;
It must not else the break. This doesn't work. add gsmState=0; between else and break and it works. I don't know if you still use this part of the code anymore. Note that this is only parsing one possible string, and the code will grow for the more state you add, but with string functions, the string functions will be reused over and over. Code compiled to 618 program words, just the statemachine and the intialization..
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@bwack
Thank you for taking time to point out the mistakes.
Although I am not using that I would love to correct the mistakes that I make and it will help if I decided to something similar

For the sake of argument, are you saying this method takes more space then the string function method ?

So far I am doing the @jayanthd method.
I am coding looking at his and so far I manged to use that method in my InitModem(); routine and got it working. :D
I am again still stuck on the "COPS: 0" area..
I spend 3 hrs trying to find an issue I had last night with the LCD not responding to RX. I thought I had a coding bug but it turned out to be a loose jumper cable between PIC and MAX232 :mad:
I even thought of getting rid of the string method.
Cause I still cannot get what @jayanthd code to work which he said it will work, ( still it is not working ), and my method refused to work and I get up so irritated last night that I went back to ISR parsing method, and even it was not working. :confused:
That's when I decided to check the circuit. For once I knew the ISR parsing works up to some point.
Even now @jayanthd code does not work...I do not know why as his code much complex and I do not have time to debug it.

I am trying to figure out a way with string method.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@bwack
Like this ?
C:
        case 20:{  // +COPS: 0,2,47201 or +COPS: 0,2,47202
          if(Uart_Read == '1'){ //we have '1', it could be +COPS: 0,2,47201
            response  = GSM_DhiMobile;
             gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
           else if(Uart_Read == '2'){ //we have '2', it could be +COPS: 0,2,47202
            response = GSM_Ooredoo;
            gsm_state = 247;      // expecting CR+LF CR+LF OK CR+LF
          }
          else
           gsm_state = 0;        // reset state machine
         break;
        }
I got a question...
The string method is used to save the Uart data into a string, right ?
So after saving it, is it possible to use that data to parse in main and get the result ?
 

jayanthd

Joined Jul 4, 2015
945
Yes, gsmBuffer is the uart buffer and it is null terminated in the ISR and hence it is a string and we are using strstr() to check if the buffer contains a particular string.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
@jayanthd
C:
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
   // asm clrwdt

    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));
        DelayXSec(2);
        (*idx) = 0;

        /*
        if((*gsmAttempt)++ == 2) {
            //GSM_RESET();
            //memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
            break;
        }
        */
    }

    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*gsmAttempt) = 0;
    (*idx) = 0;
}
This is a part of your code and I am using this. The commented part is left since you commented them out.
I had an issue with "gsmBuffer" not clearing...tell you later about it.
For now I need to figure out is below
C:
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*gsmAttempt) = 0;
    (*idx) = 0;
The above is used to clear the gsmBuffer, right ?
Here is what I am thinking...I like you to tell if I am right and where I am wrong. Problem is I do not think it is clearing the "gsmBuffer"

1. if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer)); ---> Means if (clr) is zero {(clr) is the last char is "GSM_Send_Const_Command", which is "0")}, statement is true.

2. {gsmBuffer, '\0', } fill gsmBuffer with null '\0' characters,

3.{sizeof(gsmBuffer} what ever the size of "gsmBuffer" is like if "gsmBuffer[30]; then 30 is the size.

4. (*gsmAttempt) = 0; means clear gsmAttempt

5. (*idx) = 0; means clear "idx" which is the "gsmBufferIndex".

Am I right ?
If I am right why is not nulling the gsmBuffer
Is because of the extra ")" in the end ???
if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
 

jayanthd

Joined Jul 4, 2015
945
Yes, that is the piece of code which is used to clear gsmBuffer and it depends on the value of clr. If clr is 1 it clears the buffer when funtion returns. if it is 0 then buffer will not be cleared and you can use strstr(), strcmp() memcmp() to compare the string in the buffer with some known strings.

Yes, 2, 3, 4, 5 are correct.

When you said 2 days back that you got OK on LCD for AT\r then you were using the function properly.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Crap...1 means clear. ?? o_O
That clears a lot:D
so it was not the extra ")" in the end of "if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));"
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
Darn it !
You are right ?
Without the extra ")" it does not compile :oops:

OK .. I think I got this...
Will get back soon.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
It took me a while to figure it was not emptying
so I used this in my code
C:
      memset(GSM_Buffer,'0',30); // Clear GSM Buffer
      memset(buffer1,'0',30);    // Clear Buffer1
which is not needed now.

But I did figure out why and how to put "0" in the array.
it was not a loss since I learned something..EH!
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
One thing I noticed is help file does not always help.
It is different. I see that when I google.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
So to sum it up,

CSS:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 0);
Statement does not clear the buffer before returning

CSS:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt, 1);
Statement clears the buffer before returning

Right ?
 

jayanthd

Joined Jul 4, 2015
945
Wrong.

C:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt,1);
Clears the buffer when it returns

C:
GSM_Send_Const_Command(gsmBuffer, buffer1, atResponseR, atCommand1, &gsmBufferIndex, &gsmAttempt,0);
Doesn't clear the buffer when it returns.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
10,004
How does this work ?
C:
void Extract_Signal_Strength(char *s1, char *s2){
  unsigned int i = 0;
  while(*s1) {
    if(*s1 == '\n')
      ++i;
      *s1++;
    if(i == 2)break;
  }
  while(*s1 != ' '){
    *s1++;
  }
  *s1++;
  while(*s1 != ','){
    *s2++ = *s1++;
  }
  *s2 = '\0';
}
 
Top