View previous topic :: View next topic |
Author |
Message |
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
receiving a string |
Posted: Wed Dec 14, 2005 1:17 pm |
|
|
Hello
I'm trying to connect the 628A to a cell phone and i need to receive a string that i dont know the lenght.
I'm doing something like this in the main
char temp[];
and in the interrupt
temp[x]=getc;
x++;
but in the end i do
printf(temp);
i get a lot of char that i didt'n receive.
What should i do?
Jaime |
|
|
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
|
Posted: Wed Dec 14, 2005 1:25 pm |
|
|
the problem is that i need to send AT and then receive OK.
Who can i do it? thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 14, 2005 2:40 pm |
|
|
Even if you don't know the length, you still need to set the maximum
array size.
#define BUFFER_SIZE 80
char temp[BUFFER_SIZE];
Then, presumably the string that you're receiving from the cell phone
has some character that shows the end of the message. Perhaps it's
a Carriage Return ? That would be 0x0d in hex. So check each
incoming character to see if it's the end-of-message character.
If it is, then write a 0x00 byte to the "temp" buffer, at the current
index. Remember, a string must end with a 0x00 byte. That's how
other functions know where the end of the string is.
Also, when you're putting characters into the buffer, you should check
if the current index will exceed the limits of the array. If so, don't put
any more chars in the array. If the array can hold 80 bytes, the last
available index value is 79 (in decimal). (The first index is 0). |
|
|
KamPutty Guest
|
Re: receiving a string |
Posted: Wed Dec 14, 2005 3:22 pm |
|
|
jaime wrote: |
char temp[];
and in the interrupt
temp[x]=getc;
x++;
Jaime |
Jaime,
As the previous writer said, at some point you do need to know the max array size, just to make sure you do not run over!
.
.
.
if(x<MAX_SIZE) // remember to add 1 more in the size to account for null
{
temp[x]=getc();
temp[x+1]=0; // null so printf knows when to stop!
x++;
}
You could assign space by initting the variable
char temp[MAX_SIZE+1]; // adding 1 more for null
or
char *temp;
temp=(char*)malloc(sizeof(char)*(MAX_SIZE+1));
~Kam (^8* |
|
|
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
|
Posted: Thu Dec 15, 2005 7:48 am |
|
|
I'm trying all morning and nothing..
here's my program... I can get out of my state 1. The program don't receive data. Can anyone help me??
Other problem is that i make
#define size 50 ;
char temp, buffer[size];
and the compiler said that is expecting a ] ?!?!?!?!?
Thanks just for read this...
---------------------------------------------------------------------------------
#include <16F628A.h>
#include <string.h>
#use delay (clock=20000000)
#use rs232(baud=9600, xmit=pin_b2, rcv=pin_b1)
#fuses HS, nowdt, put, nobrownout, mclr, nolvp
#define size 50 ;
short int ok=false, ring=false, busy=false, error=false, no_dial=false;
short int start_pack=false, stop_pack=false, middle_pack=false, ok_pack=false;
short int command_send=false, receive_pack=false;
int x=0, state=0;
char temp, buffer[50];
////////////////////////////////////////////////////////////////////////////////
#int_rda
void trata_rda()
{
/*
two examples that we can receive
LF CR O K CR LF
LF CR + C P B R: * * * CR LF O K CR LF
the program must detect only the LF if it's not CR or LF thw program puts in the buffer[x]
*/
temp=getc();
if(temp==0x0A)
{
if(!start_pack)
{
start_pack=true; //start of message
x=0;
}
else if(buffer[0]=='+')
{
if(!middle_pack) middle_pack=true;
else if(!ok_pack) ok_pack=true;
}
else
{
stop_pack=true; //end of message
receive_pack=true; //we got the message
buffer[x]=0;
}
}
else if((temp==!0x0D || temp==!0x0A) && (x<50))
{
buffer[x]=temp;
x++;
}
}
////////////////////////////////////////////////////////////////////////////////
void debug_data()
{
if (buffer[0]=='O' && buffer[1]=='K')
ok=true;
if (buffer[0]=='R' && buffer[1]=='I')
ring=true;
}
main()
{
while(1)
{
switch(state)
{
case 0: //state 0. led on. enable int
output_high(pin_b5);
delay_ms (2000);
enable_interrupts(int_rda);
enable_interrupts(global);
state=1;
break;
case 1: //state 1. ATE0. waits OK
if(!command_send)
{
printf("ATE0\r");
command_send=true;
}
else if(receive_pack)
{
disable_interrupts(int_rda);
disable_interrupts(int_timer1);
debug_data();
if(ok)
{
state=2;
command_send=false;
}
else if(error || busy || no_dial)
command_send=false;
}
break;
case 2: //state 2. AT+CALM=0. waits OK
if(!command_send)
{
printf("AT+CALM=0\r");
command_send=true;
}
else if(receive_pack)
{
disable_interrupts(int_rda);
disable_interrupts(int_timer1);
debug_data();
if(ok)
{
state=3;
command_send=false;
}
else if(error || busy || no_dial)
command_send=false;
}
break;
case 3: //state 3. AT+CVIB=0. waits OK
if(!command_send)
{
printf("AT+CVIB=0\r");
command_send=true;
}
else if(receive_pack)
{
disable_interrupts(int_rda);
disable_interrupts(int_timer1);
debug_data();
if(ok)
{
state=4;
command_send=false;
}
else if(error || busy || no_dial)
command_send=false;
}
break;
case 4: //state 4. led off.
output_low(pin_b5);
disable_interrupts(int_timer1);
while(!receive_pack)
{
#asm
nop
#endasm
}
while(receive_pack)
{
disable_interrupts(int_rda);
debug_data();
if(ring)
output_high(pin_a3); //only for teste
}
break;
}
}
} |
|
|
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
|
Posted: Thu Dec 15, 2005 8:04 am |
|
|
this is why the CCS Forum is so good.
I post my program and i found my errors!!!!!!
well i still want to make the project so id anyone is interessed in help i apreciate!!!
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 15, 2005 3:34 pm |
|
|
Quote: | I post my program and i found my errors!!!!!! |
What you mean is, you found this little error where you
stuck a semi-colon at the end of a #define statement:
Quote: | I'm trying all morning and nothing.. |
You shouldn't do a "core dump" of your program source code on
the forum. You should make a small test program and focus on
the problem.
Anyway, in your int_rda() function, you have this line:
Quote: | else if((temp==!0x0D || temp==!0x0A) && (x<50)) |
This is not correct coding. The ! is a "logical not" operator.
Anything that is non-zero, such as 0x0D or 0x0A, will be made into a 0.
So your code above is just doing this:
Quote: | else if((temp == 0 || temp == 0) && (x<50)) |
You need to re-write your code, and you need to do small test
programs to prove to yourself that each routine is working correctly. |
|
|
|