Arduino的串行监视器输入?

时间:2012-11-22 15:30:38

标签: c arduino

这只是一个简单的测试程序。我试图让Arduino打印出来"收到"在连接到它的液晶显示屏上。我认为这是我的if语句导致错误,任何想法?

目前"发送"在串口监视器中没有任何反应。

以下是代码:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char serialinput;   // for incoming serial data

void setup() {
    Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
    }

void loop() {

    // send data only when you receive data:
    if (Serial.available() > 0) {
            // read the incoming byte:
            serialinput = Serial.read();

            if (serialinput == 'send')
            {
            lcd.print("received");
            }
    }
}

2 个答案:

答案 0 :(得分:3)

您正在从串口读取一个字节(C中为char),但您尝试将其与字符串进行比较:

如果您想阅读4 char并将其与"send"进行比较,那么您必须执行以下操作:

#include <LiquidCrystal.h>
#include <string.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char serialinput [5] = {0};   // for incoming serial data
                              // 4 char + ending null char

void setup() {
    Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
    // send data only when you receive data:
    if (Serial.available() > 0) {
        memmove (serialinput, &serialinput[1], 3); // Move 3 previous char in the buffer
        serialinput [3] = Serial.read(); // read char at the end of input buffer

        if (0 == strcmp(serialinput, "send")) // Compare buffer content to "send"
        {
            lcd.print("received");
        }
    }
}

假设<string.h>标头在Arduino SDK中有效

PS:C代码中的小写字符串写在"(双引号)之间。 '适用于角色。

答案 1 :(得分:1)

上传到arduino时有什么错误?

相关问题