是有可能写不stdlib之间安慰? C / C ++ [英] is it possible write to console without stdlibs? c/c++
问题描述
我编程上的ARM微处理器,我试图通过UART使用打印语句进行调试。我不希望添加 stdlib之间
只是为了调试。有没有办法打印到控制台,而不 stdio.h中
/ iostream.h时
?是否有可能对我来说,我自己写的的printf()
?
I am programming on an arm microprocessor and am trying to debug using print statements via UART. I do not want to add stdlibs
just for debugging. Is there a way to print to the console without stdio.h
/iostream.h
? Is it possible for me to write my own printf()
?
另外,我可以用这一个DMA控制器,并直接写入UART做。不过,我想避免这种情况是可能的。使用内置的测试功能回声或
远端环回我知道我有UART配置正确。
Alternatively I can do this using a DMA controller and writing to the UART directly. However I would like to avoid that is possible. Using the built in test function "echo" or "remote loop-back" I know I have the UART configured properly.
推荐答案
简短的回答:是的,这是完全有可能做到既您的解决方案。
Short answer: Yes, it's entirely possible to do both of your solutions.
如果你想支持所有的数据类型和格式的printf函数是相当复杂的。但是,这并不难写一些东西,可以输出字符串或在几个不同的基地一个整数(大多数人只需要十进制和十六进制,但一旦你有十进制和八进制大概只增加了一个3-4线code的十六进制)。
The printf function is quite complex if you want to support all of the data types and formats. But it's not that hard to write something that can output a string or an integer in a few different bases (most people only need decimal and hex, but octal probably only adds another 3-4 lines of code once you have decimal and hex).
通常情况下,printf的是这样写的:
Typically, printf is written like this:
int printf(const char *fmt, ...)
{
int ret;
va_list args;
va_start(args, fmt)
ret = do_xprintf(outputfunc, NULL, fmt, args);
va_end(args);
return ret;
}
和则 do_xprintf()
做了所有的所有变型(具有printf,sprintf的,fprintf中,等等)的辛勤工作。
And then the do_xprintf()
does all the hard work for all variants (printf, sprintf, fprintf, etc)
int do_xprintf(void (*outputfunc)(void *extra, char c), void *extra, const char *fmt, va_list args)
{
char *ptr = fmt;
while(1)
{
char c = *ptr++;
if (c == '%')
{
c = *ptr++; // Get next character from format string.
switch(c)
{
case 's':
char *str = va_arg(args, const char *);
while(*str)
{
count++;
outputfunc(extra, *str);
str++;
}
break;
case 'x':
base = 16;
goto output_number;
case 'd':
base = 10;
output_number:
int i = va_arg(args, int);
// magical code to output 'i' in 'base'.
break;
default:
count++;
outputfunc(extra, c);
break;
}
else
count++;
outputfunc(extra, c);
}
return count;
}
现在,你需要做的是填写在上面code的几个位和写outputfunc(),输出到串口。
Now, all you need to do is fill in a few bits of the above code and write an outputfunc() that outputs to your serial port.
请注意,这是素描,我敢肯定,有在code一些错误 - 如果你想支持浮点或宽,你将不得不在它的工作多一点。 ..
Note that this is rough sketch, and I'm sure there are some bugs in the code - and if you want to support floating point or "widths", you will have to work a bit more at it...
(请注意:额外的参数 - 输出到 FILE *
这将是文件指针,为的sprintf
,你可以通过为缓冲区和位置在缓冲区中,或者类似的东西的结构)
(Note on the extra parameter - for output to a FILE *
that would be the filepointer, for sprintf
, you can pass a structure for the buffer and position in the buffer, or something like that)
这篇关于是有可能写不stdlib之间安慰? C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!