微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 向Arduino发送字符串的最佳方式?

我是Arduino开发的新手,我正在为我的计算机科学课程开发一个项目,它基本上使用带有LCD的Arduino板作为“留言板”.我的项目的大规模目标是在计算机上安装一个可以输入消息的程序,然后将其显示在Arduino屏幕上.我现在最关键的一点是如何将字符串发送到设备.我查看了几个不同的事情,包括将单个字节发送到Arduino,还查看了这个代码,这可能是向它发送字符串的一些方法http://www.progetto25zero1.com/b/tools/Arduino/

有没有人有任何向Arduino板发送字符串的经验,如果有的话,你愿意分享你的建议吗?我可能会在以后从外部程序(而不是Ardunio IDE)发送这些字符串时出现问题,但此时我遇到的最大问题就是将字符串发送到设备本身.

解决方法

米奇的链接应该指向正确的方向.

从主机向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中的串行监视器.

原文地址:https://www.jb51.cc/c/110431.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐