2017-10-14 117 views
1

这是Compilation error due to table in C++C++ UART Infite循环与TI-Nspire

延续所以这是我的计划:

#include <os.h> 
#include <nspireio/uart.hpp> 
#include <nspireio/console.hpp> 

int key_already_pressed = 0; 
char oldinput[100] = {0}; 
char voidlist[100] = {0}; 
/* 
void messagel(void) { 
    if(messagemode){ 
    if(isKeyPressed(KEY_NSPIRE_A) && !key_already_pressed) { 
     nio_printf("A"); 
     uart_printf("A"); 
     //strcat(message,"A"); 
     key_already_pressed = 1; 
    } 
    if(isKeyPressed(KEY_NSPIRE_B) && !key_already_pressed) { 
     nio_printf("B"); 
     uart_printf("B"); 
     //strcat(message,"B"); 
     key_already_pressed = 1; 
    } 
     if(isKeyPressed(KEY_NSPIRE_ENTER) && messagemode && !key_already_pressed) { 
      messagemode = 0; 
     key_already_pressed = 1; 
     } 
    if(!any_key_pressed()) 
     key_already_pressed = 0; 
    } 
}*/ 


int main(void) 
{ 
    assert_ndless_rev(874); 
    //clrscr(); 
    nio_console csl; 
    nio_init(&csl,NIO_MAX_COLS,NIO_MAX_ROWS,0,0,NIO_COLOR_WHITE,NIO_COLOR_BLACK,TRUE); 
    nio_set_default(&csl); 
    nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE); 
    nio_printf("Nspire UART Chat by Samy. Compiled the %s At %s\n",__DATE__,__TIME__); 
    nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK); 
    nio_puts("Press any ESC to exit and CTRL to send msg...\n"); 
    while(!isKeyPressed(KEY_NSPIRE_ESC)){ 
    if(isKeyPressed(KEY_NSPIRE_CTRL) && !key_already_pressed){ 
    nio_printf(">"); 
    char input[100] = {0}; 
     nio_getsn(input,100); 
    uart_printf(input); 
    key_already_pressed = 1; 
    } 
    if(!any_key_pressed()) 
     key_already_pressed = 0; 
    if(uart_ready()) { 
    char input[100] = {0}; 
    getline(input,100); 
    if(oldinput != input) { 
     if(input != voidlist) { 
      nio_puts(input); 
      strcpy(oldinput,input); 
      strcpy(input,voidlist); 
     } 
    } 
    } 
    } 
    nio_puts("Closing the programm."); 
    nio_free(&csl); 

    return 0; 
} 

的PROGRAME不停地发送一个字母continualy TI的屏幕上和串行输出。例如,如果我在串口监视器上写入lol,它将发送l,如果我发送一个新字符串,字母不会改变。

我真的希望这个程序在周末结束时全面工作,所以告诉我我做错了什么?

PS:我是法国人

+0

你可能还没有完全理解弦背后的概念在C/C++中可能会有所帮助了解更多关于它们的信息,如果你编写的程序如此接近实际的硬件,只是说;) – ExOfDe

+0

我没有选择它是唯一的方法来与calc进行通信 – TurtleForGaming

+0

不要担心,但对于这种情况下,它可以帮助你,我给了我的答案ght帮助你进一步。 – ExOfDe

回答

0

让我们看看你的代码

if(uart_ready()) { 
    char input[100] = {0}; 
    getline(input,100); 
    if(oldinput != input) { 
     if(input != voidlist) { 
      nio_puts(input); 
      strcpy(oldinput,input); 
      strcpy(input,voidlist); 
     } 
    } 
} 

要检查的这部分,如果UART已准备好,如果这样你用100元声明一个字符ARRY。到这里罚款。但你在做什么:

if(oldinput != input) { 

你比较数组'oldinput'的地址与前面声明的'input'数组的地址。我假定你真正想要的是比较这两个char数组的内容,因为'oldinput'和'input'总是不相等。

你其实想是这样的:

if(strcmp(oldinput,input) != 0){ 

这这些领域的实际内容进行比较。但请注意,这个函数假设在字符串末尾有一个空终止符。下一个'如果'也是如此。

试着解决这个问题,它可能会帮助你解决你的问题。

Strings in C

PS:我是德国人,但谁在乎XP

+0

你能给我答案我学到了很多东西,工作 – TurtleForGaming

+0

如果我可以我会,但我没有测试代码的手段,对不起。我在家里使用stdperiph库,没有使用您的库的经验。我只能指出可能是问题的最明显的错误。 但是,如果你仔细阅读我的答案,我相信它会解决重复编写相同内容的问题。只是想一想。在你现在的版本中,你的条件将永远是真实的。那是你不想要的东西。你只需要他们有时候是真的! – ExOfDe