C++ UART Infite loop with the TI-Nspire

C++ UART Infite loop with the TI-Nspire

本文关键字:the TI-Nspire with loop UART Infite C++      更新时间:2023-10-16

这是由于表中的编译错误的延续C++

所以这是我的程序:

#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 %sn",__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;
}

程序在 TI 屏幕和串行输出上持续发送一个字母。例如,如果我在串行监视器中写 lol,它将无限发送 l,如果我发送一个新字符串,字母不会改变。

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

PS:我是法国人

让我们看看你的代码的这一部分

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' 数组的地址进行比较。 我假设您真正想要的是比较这两个字符数组的内容,因为"oldinput"和"input"将始终不相等。

你真正想要的是这个:

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

这将比较这些字段的实际内容。但请注意,此函数假定字符串末尾有一个 null 终止符!!下一个"如果"也是如此。

尝试解决此问题,它可能会帮助您解决问题。

C 语言中的字符串

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

在 github 上放置问题后,我使一切正常 链接: GitHub 问题

相关文章: