【问题标题】:How To Read String from Arduino when am selecting no line ending at arduino serial moniter选择没有以 arduino 串行监视器结尾的行时如何从 Arduino 读取字符串
【发布时间】:2015-08-24 08:15:18
【问题描述】:

How To Read String when am select no line ending at arduino serial monitor.

【问题讨论】:

    标签: arduino embedded


    【解决方案1】:

    您通常使用行尾 (CR+LF) 来标识用户输入的结束。在您的情况下,尚不清楚行结束符将是什么。假设 '。' (句点)比您应该使用串行中的字符,直到您到达行终止符。
    这是一个示例代码:

    #define EOL_TERMINATOR '.'
    
    int inByte = 0;         // incoming serial byte
    String cmdLine;
    
    void setup()
    {
      // start serial port at 9600 bps:
      Serial.begin(9600);
      cmdLine = "";
    }
    
    void loop()
    {
      // if we get a valid byte, read analog ins:
      if (Serial.available() > 0) {
        // get incoming byte:
        inByte = Serial.read();
        if (inByte != EOL_TERMINATOR)
          cmdLine.concat(inByte);
        else {
          userCommand(cmdLine);
          cmdLine = ""; //reset cmdLine for next command
        }
      }
    }
    
    void userCommand(String cmd) {
      Serial.println("User command '"+cmd+"'");
    }
    

    【讨论】:

    • 当然,只需替换 '.'用你的 ASCII 码#define EOL_TERMINATOR 64
    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多