【问题标题】:Sending integers through serial moniter in arduino通过arduino中的串行监视器发送整数
【发布时间】:2016-10-26 10:18:00
【问题描述】:

我正在使用 Wifi 连接做一个 Arduino 项目。我想知道如何通过串行监视器发送整数。这是我到目前为止所拥有的:

      if(Serial.available()) {
        while(Serial.available()) {

        char c = Serial.read();

        if(c == '\n') {
          send_message(client, tx_buffer);
          tx_buffer = "";
      } else tx_buffer += c;
    }
  }

这是通过串口监视器发送一个字符。对于一个整数,你会怎么做?

【问题讨论】:

  • 以文本形式发送如何?你知道 123 => '1' '2' '3' (或 "123\n")

标签: arduino


【解决方案1】:

您在串行监视器中键入的任何内容都将转换为其等效的 ASCII,因此键入 3 实际上会发送 '3' == 51。这应该适用于获取无符号整数:

unsigned long tx_buffer = 0;

if (Serial.available()) {
   while(Serial.available()) {
     char c = Serial.read();
     if(c == '\r') {
       send_message(client, tx_buffer);
       tx_buffer = 0;
     } 
     else
       tx_buffer = (tx_buffer * 10) + (c - '0');  
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多