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

printf to a function - possible to return error?

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



Joined: 20 Sep 2004
Posts: 14
Location: Sacramento, CA

View user's profile Send private message

printf to a function - possible to return error?
PostPosted: Sun Mar 20, 2005 3:49 pm     Reply with quote

I know you can use printf to send output a character at a time to a function:

Code:
printf(MyFunction,"%03LX",myvar);


If MyFunction() in the above example can return an error value (0=success, 1=failure, etc.) is there any way to either

A) 'break out of' the printf() on the first error

B) return the error code from the printf() rather than having to use a global variable

The function in question will be accessing hardware which may or may not respond; in the case of non-response there is a timeout in the function. It would be nice to not keep trying further characters from the printf() if the hardware is not responding.

If nothing else I can sprintf() and pass a pointer to the resulting string, but I figured I would ask in case a more elegant solution is possible.
Ttelmah
Guest







PostPosted: Mon Mar 21, 2005 5:16 am     Reply with quote

There are some 'kludge' ways you could do this. If (for instance), you declare a global 'timeout' variable, and have the routine, set this when a timeout occurs, but also stop actually printing the characters, when it is set, then the code can check for this, and retry. So something like:
Code:


int1 timeout=false;

void output_char(int8 c) {
   if (timeout) return;
   //Here try to print the character

   //Set 'timeout' if there is a problem
   if (fault) timeout=true;
}

while {
   printf(output_char,"Whatever you want");
   if (!timeout) break;
   //Here have an error 'handler' if timeout is set

   timeout=false;
   //I show the code as retrying to send, if a timeout has occurred
}


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