这是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,并且如果我发送一个新的字符串,字母也不会改变。
我真的希望这个程序在周末完全正常工作,那么告诉我我做错了什么?
附言:我是法国人
发布于 2017-10-15 01:17:37
让我们来看一下这部分代码
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’数组的地址进行比较。我假设你真正想要的是比较这两个字符数组的内容,因为'oldinput‘和'input’总是不相等的。
你真正想要的是:
if(strcmp(oldinput,input) != 0){这将比较这些字段的实际内容。但请注意,此函数假定字符串末尾有一个空的结束符!下一个“if”也是如此。
试着解决这个问题,它可能会帮助你解决问题。
附言:我是德国人,但谁在乎XP
发布于 2017-10-15 21:57:26
在github上发布这个问题后,我让一切都正常工作链接:GitHub Issue
https://stackoverflow.com/questions/46747124
复制相似问题