简体   繁体   English

Arduino从Serial读取字符串

[英]Arduino read string from Serial

#include <stdio.h>

#define LED 13

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int i;
  char command[5];
  for (i = 0; i < 4; i++) {
    command[i] = Serial.read();
  }
  command[4] = '\0';

  Serial.println(command);

  if (strcmp(command, "AAAA") == 0) {
    digitalWrite(LED, HIGH);
    Serial.println("LED13 is ON");
  } else if (strcmp(command, "BBBB") == 0) {
    digitalWrite(LED, LOW);
    Serial.println("LED13 is OFF");
  }
}

I am trying to read a 4 characters long string with Arduino's Serial, and when it is AAAA turn on a LED, when it is BBBB turn off the serial. 我试图用Arduino的Serial读取一个4个字符长的字符串,当它是AAAA时打开一个LED,当它是BBBB时关闭串口。

However, when I enter "AAAA" it reads "AAAÿ" with lots of "ÿ"'s along the way. 但是,当我输入“AAAA”时,它会显示“AAAÿ”,其中包含许多“ÿ”。

I think I'm reading everything correctly, but it's not working so well, any idea of what I'm doing wrong? 我认为我正在正确地阅读所有内容,但它运作得不好,对我做错了什么的想法?

String txtMsg = "";  
char s;

void loop() {
    while (serial.available() > 0) {
        s=(char)serial.read();
        if (s == '\n') {
            if(txtMsg=="HIGH") {  digitalWrite(13, HIGH);  }
            if(txtMsg=="LOW")  {  digitalWrite(13, LOW);   }
            // Serial.println(txtMsg); 
            txtMsg = "";  
        } else {  
            txtMsg +=s; 
        }
    }
}

You should check is there is something available to read. 您应该检查是否有可供阅读的内容。 If not, then the read() will return -1. 如果没有,那么read()将返回-1。 You can use Serial.available() to check the read buffer. 您可以使用Serial.available()来检查读取缓冲区。

#define numberOfBytes 4
char command[numberOfBytes];

    void serialRX() {
      while (Serial.available() > numberOfBytes) {
        if (Serial.read() == 0x00) { //send a 0 before your string as a start byte
          for (byte i=0; i<numberOfBytes; i++)
            command[i] = Serial.read();
        }
      }
    }

It reads 'ÿ' because there is no char to read in the buffer. 它读取'ÿ',因为缓冲区中没有要读取的字符。 It takes some time for the others characters to unstack from the uart buffer. 其他字符从uart缓冲区中取出堆栈需要一些时间。 So, you cannot do a loop to read chars. 所以,你不能循环读取字符。 You have to wait that another character is available before reading it. 在阅读之前,您必须等待另一个角色可用。

Also, this way of waiting characters is not the best way because it blocks the main loop. 此外,这种等待字符的方式不是最好的方法,因为它会阻止主循环。

Here is what I do in my programs: 这是我在我的程序中做的事情:

String command;

void loop()
{
    if(readCommand())
    {
        parseCommand();
        Serial.println(command);
        command = "";
    }
}

void parseCommand()
{
  //Parse command here
}

int readCommand() {
    char c;
    if(Serial.available() > 0)
    {
        c = Serial.read();
        if(c != '\n')
        {       
            command += c;
            return false;
        }
        else
            return true;

    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM