• 嵌入式处理器通过UART实现scanf和printf


    #include <stdint.h>
    #include <stdarg.h>
    
    extern int vsscanf(const char *, const char *, va_list);
    extern int vsprintf(const char *, const char *, va_list);
    
    int uart_scanf(const char *fmt, ...)
    {
        int i = 0;
        unsigned char ch;
        char buf[80];
        va_list args;
    
        while(1) {
            ch = uart_getchar();
            uart_putchar(ch);
            if(ch == '
    ' || ch == '
    ') {
                buf[i] = '';
                break;
            } else {
                buf[i++] = ch;
            }
        }
    
        va_start(args, fmt);
        i = vsscanf(buf, fmt, args);
        va_end(args);
        uart_putchar('
    ');
        uart_putchar('
    ');
        return i;
    }
    
    int uart_printf(const char *fmt, ...)
    {
        int i = 0;
        unsigned int n = 0;
        char buf[80];
        va_list args;
    
        va_start(args, fmt);
        n = vsprintf(buf, fmt, args);
        va_end(args);
    
        for(i = 0; i < n; ++i) {
            uart_putchar(buf[i]);
        }
        return n;
    }
    
    int main(void)
    {
        char str[10];
        int a, b, c;
        int n;
        while((n = uart_scanf("%s%d%d%d", str, &a, &b, &c)) != -1) {
            uart_printf("%d Arguments. str = %s, a = %d, b = %d, c = %d
    ", n, str, a, b, c);
        }
        return 0;
    }
  • 相关阅读:
    Thread与Handler
    开始机顶盒的生涯
    解决布局被键盘顶的难题
    自动滚动的Textview
    2-解决粘包问题
    1-socket编程
    zipfile模块
    subprocess模块
    day31-异常处理
    collections模块
  • 原文地址:https://www.cnblogs.com/lyuyangly/p/11261619.html
Copyright © 2020-2023  润新知