首页 文章

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

提问于
浏览
6

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

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

2 回答

  • 3
  • 8

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

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

相关问题