【问题标题】:Best Way to send strings to Arduino? [closed]向 Arduino 发送字符串的最佳方式? [关闭]
【发布时间】:2011-10-21 05:07:44
【问题描述】:

我正在为我的计算机科学课做一个项目,该项目基本上使用带有 LCD 的 Arduino 板作为“留言板”。我的项目的大规模目标是在计算机上创建一个程序,可以在其中输入一条消息,然后将其显示在 Arduino 屏幕上。目前我最大的症结是如何向设备发送字符串。我查看了一些涉及向 Arduino 发送单个字节的不同内容,还查看了这段代码,这可能是向其发送字符串的某种方式:http://www.progetto25zero1.com/b/tools/Arduino/

有没有人有向 Arduino 板发送字符串的经验,如果有,您愿意分享您的建议吗?稍后我可能会遇到从外部程序(不是 Ardunio IDE)发送这些字符串的问题,但目前对我来说最大的问题是将字符串发送到设备本身。

【问题讨论】:

标签: c++ string arduino


【解决方案1】:

Mitch 的链接应该为您指明正确的方向。

从主机向 Arduino 发送和接收字符串并返回的常用方法是使用 Arduino 的串行库。串行库通过与计算机的连接一次读取和写入一个字节。

以下代码通过附加通过串行连接接收到的字符来形成一个字符串:

// If you know the size of the String you're expecting, you could use a char[]
// instead.
String incomingString;

void setup() {
  // Initialize serial communication. This is the baud rate the Arduino
  // discusses over.
  Serial.begin(9600);

  // The incoming String built up one byte at a time.
  incomingString = ""
}

void loop() {
  // Check if there's incoming serial data.
  if (Serial.available() > 0) {
    // Read a byte from the serial buffer.
    char incomingByte = (char)Serial.read();
    incomingString += incomingByte

    // Checks for null termination of the string.
    if (incomingByte == '\0') {
      // ...do something with String...
      incomingString = ""
    }
  }
}

要发送串行数据 --- 并打印 Arduino 打印的数据 --- 您可以使用 Arduino IDE 中的串行监视器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2014-07-10
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多