View previous topic :: View next topic |
Author |
Message |
foodwatch
Joined: 18 Apr 2006 Posts: 66
|
Count characters in Printf |
Posted: Mon Jun 11, 2007 7:28 pm |
|
|
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
|
|
Posted: Mon Jun 11, 2007 7:51 pm |
|
|
You can format your printf using %3u for example and know how long the string will be. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 12, 2007 3:17 am |
|
|
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.
Best Wishes |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Tue Jun 12, 2007 3:56 am |
|
|
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
|
|
Posted: Tue Jun 12, 2007 5:25 am |
|
|
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 |
|
|
|