gpt4 book ai didi

C++ UART 无限循环与 TI-Nspire

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:49 24 4
gpt4 key购买 nike

这是 Compilation error due to table in 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 %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;
}

程序在 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 个元素的 char 数组。到这里为止很好。但是你在做什么呢:

 if(oldinput != input) {

您将数组“oldinput”的地址与先前声明的“input”数组的地址进行比较。我假设您真正想要的是比较这两个 char 数组的内容,因为 'oldinput' 和 'input' 总是彼此不相等。

你真正想要的是:

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

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

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

Strings in C

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

关于C++ UART 无限循环与 TI-Nspire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com