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

Count characters in Printf

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



Joined: 18 Apr 2006
Posts: 66

View user's profile Send private message Yahoo Messenger

Count characters in Printf
PostPosted: Mon Jun 11, 2007 7:28 pm     Reply with quote

I am using printf to output about 100-120 bytes of data on the rs232 port of an 18f4550. Many of these bytes are signed integers that are converted to positive/negative integers via the printf command with %d. My dilemna is that I need an exact byte count of what is being sent out the rs232 port BEFORE I send it. I understand it is possible to redirect printf output to a function, but I have no idea how to do this. I figured I could copy it to a string and simply count the characters, then send it out the serial port. Anyone shed some light on this for me? Thanks...
_future
Guest







PostPosted: Mon Jun 11, 2007 7:51 pm     Reply with quote

You can format your printf using %3u for example and know how long the string will be.
Ttelmah
Guest







PostPosted: Tue Jun 12, 2007 3:17 am     Reply with quote

One method:
Code:

int8 count;

#define RESET_COUNT() count=0

void putc_count(int8 chr) {
   count++;
   putc(chr);
}

//Then in your code:

RESET_COUNT();
printf(putc_count,"String to count %u %f/n/r",unsignedval,floatval);

//Now, 'count' will contain the total number of characters sent.

Obviously, if more than 255 characters may be sent, then 'count' will have to change to an int16.
This takes advantage of the CCS 'printf' ability, to send it's output to another routine, routing the data to the normal 'putc', but counting the characters as they go. Smile

Best Wishes
Pret



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

PostPosted: Tue Jun 12, 2007 3:56 am     Reply with quote

True... but this is not "BEFORE". The only posibility is using fixed format... like _future said. Ofcourse... you can also use an aditional buffer... but is not efficient.
Ttelmah
Guest







PostPosted: Tue Jun 12, 2007 5:25 am     Reply with quote

Obviously though, exactly the same approach, copying the data to a buffer will work. However sprintf, is then easier (just measure the length of the string).

Best Wishes
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