-->

How to transmit a String on Arduino?

2020-03-26 07:46发布

问题:

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&)'"

回答1:

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!



回答2:

Use toCharArry(), write() uses char*, not string, here is what i mean:

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  while (!Serial);
} 

void loop() {
  String outMessage = "";

  while (Serial.available() > 0) {
    char inChar = Serial.read();
    outMessage.concat(inChar);
  }

  if (outMessage != "") {
    Serial.println("Sent:  " + outMessage);
    char* CharString;                                    //
    outMessage.toCharArray(cString, outMessage.length()) // My Changes Are Here
    Serial1.write(CharString);                           //
  }
}