如何在 Arduino 上传输字符串? [英] How to transmit a String on Arduino?

查看:35
本文介绍了如何在 Arduino 上传输字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要 2 个 Arduinos Leonardo 进行通信,例如发送一个字符串,所以我必须使用 Serial1 通过引脚 0 (RX) 和 1 (TX) 上的 RS232 进行通信.

I want 2 Arduinos Leonardo to communicate, send a string for instance, so I have to use Serial1 to communicate via RS232 on pins 0 (RX) and 1 (TX).

我需要在该引脚中写入二进制数据,问题是如何使用 Serial1.write 发送字符串.Serial1.print 可以正常工作,但我认为它不能满足我的要求.有什么建议吗?

I need to write binary data in that pins, the problem is how can I send a String using Serial1.write. Serial1.print works without errors but I think it does not do what I want. Any suggestion?

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  while (!Serial);  // while not open, do nothing. Needed for Leonardo only
} 

void loop() {
  String outMessage = "";  // String to hold input

  while (Serial.available() > 0) {  // check if at least one char is available
    char inChar = Serial.read();
    outMessage.concat(inChar);  // add Chars to outMessage (concatenate)
  }

  if (outMessage != "") {
    Serial.println("Sent:  " + outMessage); // see in Serial Monitor
    Serial1.write(outMessage); // Send to the other Arduino
  }
}

这一行 Serial1.write(outMessage); 给了我错误

"没有匹配的函数调用'HardwareSerial::write(String&)'"

推荐答案

你正在使用 String 对象(Wiring/C++).该函数使用 C 字符串:Serial.write(char*).要将其转换为 C 字符串,请使用 toCharArray() 方法.

You're using the String object(Wiring/C++). The function is using C strings: Serial.write(char*). To turn it into a C string, you use the toCharArray() method.

char* cString = (char*) malloc(sizeof(char)*(outMessage.length() + 1);
outMessage.stoCharArray(cString, outMessage.length() + 1);
Serial1.write(cString);

如果我们不使用 malloc 为 C 字符串分配内存,就会出错.以下代码将崩溃.

If we do not allocate the memory for our C string with malloc, we will get a fault. The following code WILL crash.

void setup() {
  Serial.begin(9600);

  String myString = "This is some new text";
  char* buf;

  Serial.println("Using toCharArray");
  myString.toCharArray(buf, myString.length()+1); // **CRASH** buf is not allocated!

  Serial.println(buf);
}

void loop() {
  // put your main code here, to run repeatedly: 

}

在串行监视器中,我们将得到的唯一消息是:使用 toCharArray.此时执行停止.现在,如果我们纠正问题并使用 malloc() 为我们的缓冲区分配内存,并在完成后使用 free() ......

In the Serial Monitor the only message we will get is: Using toCharArray. At that point execution stops. Now if we correct the problem and use malloc() to allocate memory for our buffer and also use free() when done....

void setup() {
  Serial.begin(9600);

  String myString = "This is some new text";
  char* buf = (char*) malloc(sizeof(char)*myString.length()+1);

  Serial.println("Using toCharArray");
  myString.toCharArray(buf, myString.length()+1);

  Serial.println(buf);

  Serial.println("Freeing the memory");
  free(buf);
  Serial.println("No leaking!");
}

void loop() {
  // put your main code here, to run repeatedly: 

}

我们在串行监视器中看到的输出是:使用 toCharArray这是一些新的文本释放内存没有泄漏!

The output we see in the Serial Monitor is: Using toCharArray This is some new text Freeing the memory No leaking!

这篇关于如何在 Arduino 上传输字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆