View Full Version : Printf
For some reason none of my attempts to send data back with the printf function seem to be working. Regular text seems to be working just fine but when I try to output variables it all just goes downhill.
Is there a simple explanation as to how to get printf working correctly?
Thanks
Pat Fairbank
18-01-2006, 00:40
Seems like the printf header file with the handy usage instructions has disappeared along with the function's C code, into FRC_library.lib I would think.
Anyway, if the the syntax from last year's code still applies it should be something like:
printf("Myvariable = %d\r", (int)myvariable);
TimCraig
18-01-2006, 01:41
Is there a simple explanation as to how to get printf working correctly?
Unless it's changed this year and I haven't had a chance to check, the printf function supplied is a partial implementation. For instance, no %f or %ld specifiers. And you have to be careful to make sure the passed argument list exactly matches the size of the specifiers in the string or very funny things can happen. Since printf takes a variable number of arguments, the compiler can't do strict type checking for you. All it can do is make sure the first argument passed is a string. For instace if you want to print the numerical value contained in a char, you need to cast it to an int if you specify %d.
BradAMiller
18-01-2006, 10:06
There's a copy of printf in the C library supplied by Microchip. In fact you can look at the source code in the src directory in the mcc18 install.
It implements most of the standard printf features except floating point. And does support longs and some of the other data types.
Be sure that the abridged version of printf that was supplied in the past is not part of your project, otherwise the linker will give you that one instead of the Microchip version.
The documentation for printf is in section 4.7 of the MPLab C18 Libraries PDF document. Kevin Watson has been making that easily available to teams on his web site (http://kevin.org/frc/) that you can download.
eugenebrooks
20-01-2006, 02:36
There's a copy of printf in the C library supplied by Microchip. In fact you can look at the source code in the src directory in the mcc18 install.
It implements most of the standard printf features except floating point. And does support longs and some of the other data types.
You can also get the extended integer formatting code referred to in the white paper: "Introduction to C programming for first robotics applications" that will format signed and unsigned longs and will also format fixed point numbers, if you are using them. The white paper indicates where to download this code.
Finally got it working, I don't know why I was having so much trouble before...
Thanks a lot for all your help.
On a somewhat related note - When I connect via the program port to download code I get the window IFI> and any of my printf's are displayed. Is there anyway to read this from the dashboard port or by method other than downloading code?
Thanks
eugenebrooks
20-01-2006, 20:10
We are getting spurious newlines in code used last year to print the new values of digital and analog inputs when values change. Is anyone seeing spurious linebreaks from printf?
BradAMiller
20-01-2006, 21:10
We are getting spurious newlines in code used last year to print the new values of digital and analog inputs when values change. Is anyone seeing spurious linebreaks from printf?
If you are using "\n" for new lines, try changing them to "\r".
JBotAlan
20-01-2006, 21:21
On a somewhat related note - When I connect via the program port to download code I get the window IFI> and any of my printf's are displayed. Is there anyway to read this from the dashboard port or by method other than downloading code?
You can do this a few ways. If you just need to see the feedback from the bot and that's it, open IFI Loader, click Options > Controller verification, make sure it finds the RC okay, and click Options > Terminal window. Another solution is to use HyperTerminal (bundled with Windows, post back if you need instructions making this work).
Good luck!
JBotAlan
eugenebrooks
21-01-2006, 14:03
If you are using "\n" for new lines, try changing them to "\r".
Great call! If you use '\n' to end the lines in the printf routine spurious new lines show up in the middle of the line, for some reason. We switched them all to '\r' and this problem evaporated.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.