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

发布于 2024-12-11 12:38:20 字数 1435 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-12-18 12:38:20

米奇的链接应该会为您指明正确的方向。

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

Mitch's links should point you in the right direction.

A common way of sending and receiving strings from the host computer to the Arduino and back is using the Arduino's Serial library. The Serial library reads and writes a byte at a time over the connection to the computer.

The below code forms a String by appending chars received over the Serial connection:

// 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 = ""
    }
  }
}

To send the serial data --- and to print out the data the Arduino prints --- you can use the Serial Monitor in the Arduino IDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文