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

receiving a string

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



Joined: 25 Nov 2005
Posts: 56
Location: Porto - Portugal

View user's profile Send private message Visit poster's website

receiving a string
PostPosted: Wed Dec 14, 2005 1:17 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 14, 2005 1:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 14, 2005 2:40 pm     Reply with quote

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
PostPosted: Wed Dec 14, 2005 3:22 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Dec 15, 2005 7:48 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Dec 15, 2005 8:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 15, 2005 3:34 pm     Reply with quote

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:
#define size 50 ;



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.
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