Printf阻止终端工作

时间:2012-03-21 10:25:30

标签: c uart

我正在尝试通过输入UART事件处理程序捕获输入数字,并使用该数字的一些乘法将其打印回来。它打印字符串就好了,但在打印完之后,程序不再对任何输入作出反应。可以打印alfabet或显示错误消息,但是当我使用printf函数时,终端停止响应输入,光标放在下一行的中间位置。

这是C代码:

#include "mss_uart.h"
#include <stdio.h>

#define RX_BUFF_SIZE    64
#define MSS_UART_57600_BAUD     57600
uint8_t g_rx_buff[RX_BUFF_SIZE];
uint8_t g_rx_idx;

void uart0_rx_handler( mss_uart_instance_t * this_uart )
{
MSS_UART_get_rx( &g_mss_uart0, &g_rx_buff[g_rx_idx], sizeof(g_rx_buff) );
if(g_rx_buff[g_rx_idx] > 96 && g_rx_buff[g_rx_idx] < 123)
{
    uint8_t message[55] = "De letter was: x, de uppercase letter van : x is y.\n\r";
    message[15] = g_rx_buff[g_rx_idx];
    message[44] = g_rx_buff[g_rx_idx];
    message[49] = g_rx_buff[g_rx_idx] - 32;

    MSS_UART_polled_tx( &g_mss_uart0, message, sizeof(message) );
}
else if(g_rx_buff[g_rx_idx] > 64 && g_rx_buff[g_rx_idx] < 91)
{
    uint8_t message[55] = "De letter was: x, de lowercase letter van : x is y.\n\r";
    message[15] = g_rx_buff[g_rx_idx];
    message[44] = g_rx_buff[g_rx_idx];
    message[49] = g_rx_buff[g_rx_idx] + 32;

    MSS_UART_polled_tx( &g_mss_uart0, message, sizeof(message) );
}
else if(g_rx_buff[g_rx_idx] > 47 && g_rx_buff[g_rx_idx] < 58)
{
    int number = g_rx_buff[g_rx_idx] - '0';
            int number2 = number * number;
            int number3 = number2 * number;
            int number4 = number3 * number;

    printf("Getallenreeks: %d, %d, %d, %d.\n\r", number, number2, number3, number4);

}
else
{
    uint8_t message[10] = "Error.\n\r";
    MSS_UART_polled_tx( &g_mss_uart0, message, sizeof(message) );
}
}

int main(void)
{
MSS_UART_init
(
        &g_mss_uart0,
        MSS_UART_57600_BAUD,
        MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
);

MSS_UART_set_rx_handler( &g_mss_uart0, uart0_rx_handler, MSS_UART_FIFO_SINGLE_BYTE    );

while ( 1 )
{}
return(0);
}

void uart0_rx_handler 是intterupt处理程序, MSS_UART_get_rx 将输入放入g_rx_buff [g_rx_idx]。

我尝试使用相同的 MSS_UART_polled_tx 功能打印数字,但我没有运气。它打印错误的ascii值:

if(g_rx_buff[g_rx_idx] > 47 && g_rx_buff[g_rx_idx] < 58)
{
    int number = g_rx_buff[g_rx_idx] - '0';
    int number2 = number * number;
    int number3 = number2 * number;
    int number4 = number3 * number;

    uint8_t message[15] = "Getallenreeks: ";
    uint8_t komma[2] = ", ";
    uint8_t end[5] = ".\n\r";

    char numberstring2[2];
    char numberstring3[3];
    char numberstring4[4];

    sprintf(numberstring2, "%d", number2);
    sprintf(numberstring3, "%d", number3);
    sprintf(numberstring4, "%d", number4);

    uint8_t messagenumber[1];
    uint8_t messagenumber2[1];
    uint8_t messagenumber3[1];
    uint8_t messagenumber4[1];

    messagenumber[0] = '0' + number;
    messagenumber2[0] = '0' + number2;
    messagenumber3[0] = '0' + number3;
    messagenumber4[0] = '0' + number4;      

    http://imageshack.us/photo/my-images/843/testlan.jpg/( &g_mss_uart0, message, sizeof(message) );
    MSS_UART_polled_tx( &g_mss_uart0, messagegetal, sizeof(messagenumber) );
    MSS_UART_polled_tx( &g_mss_uart0, komma, sizeof(komma) );
    MSS_UART_polled_tx( &g_mss_uart0, messagegetal2, sizeof(messagenumber2) );
    MSS_UART_polled_tx( &g_mss_uart0, komma, sizeof(komma) );
    MSS_UART_polled_tx( &g_mss_uart0, messagegetal3, sizeof(messagenumber3) );
    MSS_UART_polled_tx( &g_mss_uart0, komma, sizeof(komma) );
    MSS_UART_polled_tx( &g_mss_uart0, messagegetal4, sizeof(messagenumber4) );
    MSS_UART_polled_tx( &g_mss_uart0, end, sizeof(end) );       
 }

代码输出示例:http://imageshack.us/photo/my-images/843/testlan.jpg/ 左侧终端显示使用 printf 功能,右侧终端显示 MSS_UART_polled_tx 功能用于数字(显示在第二个代码块中)。

2 个答案:

答案 0 :(得分:1)

我认为最大的错误是将“\ r \ n”的长度计为4(它是2),另一个是使用`sizeof stringarray',其中包括终止NUL字节使用的空间。

  /** added */
#include <stdint.h>
#include <stdio.h>

struct xx;
typedef struct xx mss_uart_instance_t;

void MSS_UART_get_rx( mss_uart_instance_t * the_uart, uint8_t buff[] , size_t len );
void MSS_UART_polled_tx( mss_uart_instance_t * the_uart, uint8_t buff[] , size_t len );
    /** End added */

#define RX_BUFF_SIZE    64
#define MSS_UART_57600_BAUD     57600

uint8_t g_rx_buff[RX_BUFF_SIZE];
uint8_t g_rx_idx;

void uart0_rx_handler( mss_uart_instance_t * this_uart )
{
    MSS_UART_get_rx( this_uart, &g_rx_buff[g_rx_idx], sizeof g_rx_buff );
    if(g_rx_buff[g_rx_idx] >= 'a' && g_rx_buff[g_rx_idx] <= 'z')
    {
        uint8_t message[] = "De letter was: x, de uppercase letter van : x is y.\n\r";
        message[15] = g_rx_buff[g_rx_idx];
        message[44] = g_rx_buff[g_rx_idx];
        message[49] = g_rx_buff[g_rx_idx] - ('a' - 'A');

        MSS_UART_polled_tx( this_uart, message, strlen(message) ); /* 52 */
    }
    else if(g_rx_buff[g_rx_idx] >= 'A' && g_rx_buff[g_rx_idx] <= 'Z' )
    {
        uint8_t message[] = "De letter was: x, de lowercase letter van : x is y.\n\r";
        message[15] = g_rx_buff[g_rx_idx];
        message[44] = g_rx_buff[g_rx_idx];
        message[49] = g_rx_buff[g_rx_idx] + ('a' - 'A');

        MSS_UART_polled_tx( this_uart, message, strlen(message) ); /* 52 */
    }
    else if(g_rx_buff[g_rx_idx] >= '0' && g_rx_buff[g_rx_idx] <= '9')
    {
        uint8_t bigbuff[70] ;
        size_t buflen;
        int number = g_rx_buff[g_rx_idx] - '0';
                int number2 = number * number;
                int number3 = number2 * number;
                int number4 = number3 * number;

        buflen = sprintf(bigbuff, "Getallenreeks: %d, %d, %d, %d.\n\r", number, number2, number3, number4);
        MSS_UART_polled_tx( this_uart, bigbuff, buflen );

    }
    else
    {
        uint8_t message[] = "Error.\n\r";
        MSS_UART_polled_tx( this_uart, message, strlen(message) ); /* 8 */
    }
}

答案 1 :(得分:0)

我认为你有两个问题。

一个是你在一个中断处理程序中调用printf()并且它正在挂起。也许在你的平台上无法从中断上下文调用printf(),因为(猜测)它试图使用中断驱动的输出并等待一个永远不会发生的完成中断,因为你已经禁用了中断。

第二个问题是你对第二个例子的输出的期望。他们看起来对我很好。您将一些相当大的数字相乘,然后将它们添加到char值。这将导致单个字符,可能是您不想要的字母或符号。例如

'0' + 16 == '@'

您在其中一个示例中看到的内容。如果你想输出“16”,即两个字符'1'和'6',你需要做更多的工作来计算个别字符(涉及除以10)。