![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest
|
Send AT<cr> to GM862 |
Posted: Wed Jun 18, 2008 9:03 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jun 18, 2008 10:10 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Fri Jun 20, 2008 10:24 am |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 23, 2008 2:25 am |
|
|
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>" |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Jun 23, 2008 6:23 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 23, 2008 7:35 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Jun 23, 2008 9:23 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 23, 2008 10:00 am |
|
|
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;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Jun 24, 2008 8:58 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 24, 2008 9:06 am |
|
|
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> |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 24, 2008 9:09 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 24, 2008 9:17 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Jun 24, 2008 11:14 am |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jun 25, 2008 2:32 am |
|
|
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](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|