Closed. This question needs to be more focused。它当前不接受答案。












想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。

1年前关闭。



Improve this question




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

有没有人有任何将字符串发送到Arduino板的经验,如果是,您是否愿意分享有关如何进行处理的建议?稍后从外部程序(而不是Ardunio IDE)发送这些字符串时,我可能会遇到问题,但是对我而言,目前最大的问题只是将字符串发送至设备本身。

最佳答案

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

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

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

// 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中使用串行监视器。

关于c++ - 将字符串发送到Arduino的最佳方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7845196/

10-16 09:23
查看更多