|
|
View previous topic :: View next topic |
Author |
Message |
webbone
Joined: 20 Sep 2004 Posts: 14 Location: Sacramento, CA
|
printf to a function - possible to return error? |
Posted: Sun Mar 20, 2005 3:49 pm |
|
|
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
|
|
Posted: Mon Mar 21, 2005 5:16 am |
|
|
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 |
|
|
|
|
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
|