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

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

问题描述

我要2 Arduinos莱昂纳多通讯,发送例如一个字符串,所以我必须使用接口Serial1 引脚0(RX)和1 TX通过RS232通信( )。

我需要在该引脚写二进制数据,问题是我怎么能使用 Serial1.write 发送一个字符串。 Serial1.print 工作没有错误,但我认为它不会做我想做的。任何建议?

无效设置(){
  Serial.begin(9600);
  Serial1.begin(9600);
  而(串行!); //而不是开放的,什么也不做。需要莱昂纳多只
}空隙环(){
  串outMessage =; //字符串来保存输入  而(Serial.available()0){//检查是否至少有一个字符可用
    焦炭INCHAR = Serial.read();
    outMessage.concat(INCHAR); //添加其余字outMessage(串连)
  }  如果(outMessage!=){
    Serial.println(已发送+ outMessage); //看到串行监视器
    Serial1.write(outMessage); //发送到其他的Arduino
  }
}

这行 Serial1.write(outMessage); 是给我的错误


  

呼叫为HardwareSerial ::写(字符串&安培;)没有匹配的功能



解决方案

您正在使用String对象(配线/ C ++)。该功能是使用C字符串:Serial.write(字符*)。为了把它变成一个C字符串,可以使用toCharArray()方法。

 的char * CString的=(的char *)malloc的(的sizeof(字符)*(outMessage.length()+ 1);
outMessage.stoCharArray(CString的,outMessage.length()+ 1);
Serial1.write(CString的);

如果我们不使用malloc分配内存为我们的C字符串,我们会得到一个错误。下面code会崩溃。

 无效设置(){
  Serial.begin(9600);  字符串的myString =这是一些新文本;
  字符* BUF;  Serial.println(使用toCharArray);
  myString.toCharArray(BUF,myString.length()+ 1); // ** ** CRASH BUF未分配!  Serial.println(BUF);
}空隙环(){
  //把你的主要code在这里,重复运行:}

在串行监视器,我们将得到的唯一信息是:用toCharArray。在这一点上停止执行。现在,如果我们纠正问题,使用malloc()来为我们的缓冲区分配内存,还可以使用免费的()完成时....

 无效设置(){
  Serial.begin(9600);  字符串的myString =这是一些新文本;
  字符* BUF =(字符*)malloc的(的sizeof(char)的* myString.length()+ 1);  Serial.println(使用toCharArray);
  myString.toCharArray(BUF,myString.length()+ 1);  Serial.println(BUF);  Serial.println(释放内存);
  免费(BUF);
  Serial.println(不漏!);
}空隙环(){
  //把你的主要code在这里,重复运行:}

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

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).

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
  }
}

this line Serial1.write(outMessage); is giving me the error

"no matching function for call to 'HardwareSerial::write(String&)'"

解决方案

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);

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: 

}

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: 

}

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天全站免登陆