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 support@ccsinfo.com

fprintf problem

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







fprintf problem
PostPosted: Thu Apr 02, 2009 2:15 am     Reply with quote

Hi

I'm trying to send an sms message, using this:
Code:


char   a = 0x22;
char   b = 0x0D;
char   d = 0x1A;
fprintf(GSM_MODEM,"AT+CMGS=%c%s%c%c%s%c",a,current_number,a,b,cc,d);



where current number is declared like: char current_number[12];

the problem is that i'm getting this kind of string:
AT+CMGS="+71234567890 "
[string cc here]

instead of AT+CMGS="+71234567890"

why are the extra characters in it and how to get rid of them?
tnanks.

PS: sorry for my english Smile
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 2:24 am     Reply with quote

First of all you don't show what cc is defined as or set to.
Do you need to enclose it in qoutes ?
What is 0x1A for ?
Don't you mean to be sending cr/lf 0x0D 0x0A
kisslove
Guest







PostPosted: Thu Apr 02, 2009 2:57 am     Reply with quote

Wayne_ wrote:
First of all you don't show what cc is defined as or set to.
Do you need to enclose it in qoutes ?
What is 0x1A for ?
Don't you mean to be sending cr/lf 0x0D 0x0A


0x1a is a <ctrl+z> - end of text marker.
cc - is a string and there's no problem with it - it's transferred correctly.
it's declared in function from where fprintf is called.
Code:

void   send_sms(char *cc)
{
   char   a = 0x22;
   char   b = 0x0D;
   char   d = 0x1A;

       fprintf(GSM_MODEM,"...",...)
}

4 example cc may look like this $GPRMC,075103.903,V,5953.0527,N,03021.6326,E,0.0,0.00,020409,,,N*48

current_number is global.
kisslove
Guest







PostPosted: Thu Apr 02, 2009 3:12 am     Reply with quote

The question is why do i get 14 characters between quotation marks instead of 12?
Guest








PostPosted: Thu Apr 02, 2009 3:21 am     Reply with quote

i've tryed it without the quotes:

AT+CMGS=+71234567890
$GPRMC,090417.837,V,5953.0527,N,03021.6326,E,0.0,0.00,020409,,,N*45


and i'm still getting one or two random characters in the end of the number
nostep



Joined: 04 Mar 2009
Posts: 16

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 6:13 am     Reply with quote

Typically and end of string mark is a zero (0x00). What character shows up after your number? Make sure it's a zero like

number[12]=0x00.

If that doesn't work then you can alsways do it the ugly way like:

fprintf(... "%c%c%c%c%c%c%c...",0x22,num[0],num[1],num[2],num[3],etc,0x22);
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Apr 02, 2009 7:06 am     Reply with quote

Need to see the definition and assignment of current_number.

Also, what are you using to view the extra 2 chars and what values are they ?
It appears that 00 and 02 are the 2 chars, 00 may be the string termination char which fprintf would not have sent. they may be control signals ?
Guest








PostPosted: Fri Apr 03, 2009 2:37 pm     Reply with quote

If you are using the PCD compiler for the pic24 series, then there are still many problems with fprintf/sprintf/printf even in the latest version 4.090.

I have recently submitted about 16 errors with fprintf formatting to CCS and am awaiting a response.

I have similar problems in particular with:
printing floats (with no decimal places) - getting extra chars inserted in the string
int32's where the value is zero, - it prints nothing.
int16's value of 0 error was fixed in v4.090.

The only way I have found to get around most of these problems is to specify the width/precision, and the only way to work out which ones work is to write your own fprintf test program and then avoid the ones that dont work until CCS fixes them.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Apr 03, 2009 3:45 pm     Reply with quote

First, as it was stated above, all strings, repeat ALL strings MUST be terminated with a NULL character (0x00). You have declared current_number as: char current_number[12]. Well, you have 12 characters inside of that string "+71234567890". If you want 12 characters stored then you need to declare it as: char current_number[13] and make sure you have current_number[13] = 0x00. If you don't have the NULL character in the last position you will get garbage at the end. This goes with cc also. You ALWAYS need to declare a string at least one place larger than the size you want to store, to accomodate the NULL character.

Try this and see if it helps get rid of the garbage.

Ronald
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 6:02 am     Reply with quote

Apart from using sufficient string storage, as suggested, you may want to simplify your fprintf() statement by utilizing standard C escape sequences for control characters. A double quotation mark can be inserted as \" and arbitrary control characters as \xnn, eg. \x1a for CTRL-Z. They work in format strings and other string constants as well.
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