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

Send AT<cr> to GM862

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Send AT<cr> to GM862
PostPosted: Wed Jun 18, 2008 9:03 am     Reply with quote

Hi
I have a stupid beginnger question according to the GM862
I want to send AT<CR> to my GM862. As an answer should return OK.
But it doesn't work. I have two hardware UART.

Below my code
Code:
#include <18f1320.h>
#use delay(internal=8M)
#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use rs232(baud=2400,xmit=PIN_B3,rcv=PIN_B2,BRGH1OK,stream=TB)
#use rs232(baud=2400,xmit=PIN_B1,rcv=PIN_B4,BRGH1OK,stream=GM)

void main(){
char c;
char n;

while(TRUE){

while(!kbhit()){
}

c = fgetc(TB);
fputc(c,GM);

while(!kbhit()){
}
n = fgetc(GM);
fputc(n,TB);
}
}

Thanks.
Ttelmah
Guest







PostPosted: Wed Jun 18, 2008 10:10 am     Reply with quote

As written, it'll wait for a character to arrive on the 'GM' string, and when it does, try to read a character from the 'TB' stream.
You need to include the stream name in the kbhit test as well, otherwise it'll default to waiting on the last stream defined.

Best Wishes
Guest








PostPosted: Fri Jun 20, 2008 10:24 am     Reply with quote

Thanks for your advise but it doesn't help. I changed my code as follows:
Code:
#include <18f1320.h>
#use delay(internal=8M)
#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use rs232(baud=2400,xmit=PIN_B3,rcv=PIN_B2,BRGH1OK,stream=TB)
#use rs232(baud=2400,xmit=PIN_B1,rcv=PIN_B4,BRGH1OK,stream=GSM)

#include <input3.c>

#define  STRING_SIZE    10             
#define  RCV_SIZE     10

void main()
{
   char input_str[STRING_SIZE];
   char rcv_str[RCV_SIZE];

   delay_ms(1000);
   output_high(PIN_A0);
   delay_ms(4000);
   fprintf(TB,"GSM ready");

    while(TRUE)
    {
   fprintf(TB,"AT\r");
   fprintf(GSM,"AT\r");
   
   get_string_GSM(rcv_str,RCV_SIZE);

   fprintf(TB,rcv_str);
    }
}

The relevant file input3.c is that one:
Code:
#include <ctype.h>
void get_string_GSM(char* st, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc(GSM);

     if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         st[len++]=c;
       }
   } while(kbhit(GSM));
   st[len]=13;
   st[len+1]=0;
}

My problem is that i can send "AT<cr>" but what I get back from GM862 is "A<cr>" instead of "OK".
Is there any problem in my code?
Thanks!
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 23, 2008 2:25 am     Reply with quote

The GM862 is echoing back your command before it sends OK.
So the first string you recieve back when using get_string_GSM is "AT<cr>"

Call get_string_GSM again and it should return "OK<cr>"
Guest








PostPosted: Mon Jun 23, 2008 6:23 am     Reply with quote

Thanks for your answer but it doesn't work.
After sending AT<cr> i got back <NUL> A and the second time it waits for getting a character.
I have no idea what happens. Any idea?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 23, 2008 7:35 am     Reply with quote

As the code is not interrupt driven it checks and retrieves the first char recieved 'A' but then returns because it thinks there are no more chars.
The code then adds a '\r' to the end of the array folowed by the null terminating char '\0' before returning.

Either it runs too fast and does a check on kbhit before the next char is recieved (As the second time round it just sits and waits for a char I suspect this is NOT the case) OR it is not processing it fast enough and all chars after 'A' are dropped.
Guest








PostPosted: Mon Jun 23, 2008 9:23 am     Reply with quote

I also think that it is not processing it fast enough and all chars after 'A' are dropped. But what can I do against it?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 23, 2008 10:00 am     Reply with quote

I am not so sure. try the following code.
What this does is wait for the '\r' to be recieved before carrying on.
If this works then your code is not waiting long enough for more chars to come in. You then send more out which may be messing up your rs232 which is why you don't actually see anymore and it hangs.

if you are definately recieving '\r' after every response then you can stick with this.
Another option is to use interrupts to recieve the chars into a buffer.
You can also impliment a timeout just incase there is spurious data.
You can impliment it so it waits for a char but the getc will timeout, this adds delays to the routine which I don't like.

What I would impliment depends on how closed the connection is and how defined the command/response is. If I know what to expect then I will just look for that.

Code:

#include <ctype.h>
void get_string_GSM(char* st, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc(GSM);

     if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         st[len++]=c;
       }
   } while(c != '\r');
   st[len]=13;
   st[len+1]=0;
}

Guest








PostPosted: Tue Jun 24, 2008 8:58 am     Reply with quote

Hi,
first I tried your code but it doesn't work. After that I took your code and added some fprintf funtcions to see what happens.
Code:
#include <ctype.h>

void get_string_GSM(char* st, unsigned int8 max) {
   unsigned int8 len;
   char c;
   
   --max;
   len=0;
   free(st);                //free space for the characters
   do {
     fprintf(TB,"point 2\t");
     c=fgetc(GSM);             

     if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         fprintf(TB,"len = %u  ", len);
         st[len++]=c;
         fprintf(TB,"letter %u: %u, %c\t",len-1,c,st[len-1]);
       }
   } while(c != '\r');
   st[len]=13;                   
   st[len+1]='\0';
   fprintf(TB,"point 3\t");
   fprintf(TB,"s1: %u, s2: %u, s3: %u, s4: %u\n",st[0],st[1],st[2],st[3]);
}


As result I got the following:
point 2<HT> point 2<HT> len = 0 letter 0: 65, A<HT> point 2<HT>
and then it stops.
Quote:
If I know what to expect

I don't know exactly what you mean with that. I just want to test if the GM862 works and then to take some adjustments.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 9:06 am     Reply with quote

Connect a PC to the GSM device with CCS Serial Port monitor and confirm it works as expected.

You type
AT<cr>
You get
AT<cr>
OK<cr>

Please note that each char will be echoed directly back, so you press A and A is displayed. you can confirm echo by enabling "Local Echo" in the serial port monitor configuration page and then you should get

AATT<cr><cr>
OK<cr>
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 9:09 am     Reply with quote

If I know every response will end with <cr> then I just keep reading in to a buffer until <cr> is recieved, I then know I have a line!

if it is possible that communication fails before <cr> is recieved you may need to have a timeout for the getc.

If I don't know how many chars or an end of line char is not present then I would keep reading in until a time has passed where I don't recieve anything. I would then assume I have the data.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jun 24, 2008 9:17 am     Reply with quote

I think I know what the problem may be.

Try

Code:

  fprintf(GSM, "A");
  fprintf(TB, "%c", fgetc(GSM));
  fprintf(GSM, "T");
  fprintf(TB, "%c", fgetc(GSM));
  fprintf(GSM, "\r");
  fprintf(TB, "%c", fgetc(GSM));
  fprintf(TB, "%c", fgetc(GSM));
  fprintf(TB, "%c", fgetc(GSM));
  fprintf(TB, "%c", fgetc(GSM));


in your main.
Guest








PostPosted: Tue Jun 24, 2008 11:14 am     Reply with quote

Hi
I tried it with your code and extended it with with 5 more
Quote:
fprintf(TB, "%c", fgetc(GSM));
and I got the following:

<NUL>AT<CR>
<CR><LF>
OK<CR><LF>

Sometimes I didn't get the first <NUL>.

As you mentioned you have an idea what the problem is. Can you help me again?
Thanks a lot!
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Jun 25, 2008 2:32 am     Reply with quote

So far it looks like it is using a software serial interface rather than the hardware USART. Looking at the PIC data sheet you have assigned the correct pins for hardware, according to CCS help the hardware USART should have a 3 char capability which would mean recieving more than just the 'A' back.

From the CCS help:-
"If a built-in USART is used the hardware can buffer 3 characters "
and
"Note that in the case of software RS232 this function should be called at least 10 times the bit rate to ensure incoming data is not lost."

Options are to
1. Fix it so it uses hardware (which it may already be doing). It may turn out that hardware can only buffer one char so you still have a problem, Also if you have more than 3 char command then it will fail!

2. write a gsm send routine which poles for a recieved char after every char sent :-

Code:

void GSM_Send(char* outBuffer, char* inBuffer)
{
  do {
    fputc(GSM, *outBuffer);
    if (kbhit(GSM))
     *inBuffer++ = fgetc(GSM);
  } while (*buffer++ != '\r');
  while(kbhit(GSM)) {
    *inBuffer++ = fgetc(GSM);
  }
  *inBuffer = '\0';
}

Untested example!
This also needs some work to strip out seperate lines from a multiple line response and allow for a delay between incomming chars!

3. Use interrupts, This in my opinion is a better option but requires more work from you. #INT_RDA rda_isr() {...}. buffer the incomming chars in the interrupt routine and use another function to retrieve them. There are many examples and explanations on this forum if you do a search.

4. It is something else that I have not spotted Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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