# RF virtual wire sending string issue

**URL:** <https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017>\
**Category:** Programming\
**Created:** [February 25, 2016, 11:02am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017 "2016-02-25T11:02:44Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:02am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/1 "2016-02-25T11:02:44Z")

</div>

I want to send a String written through Serial.read() to the arduino via RF virtual wire to another arduino but i get the error message:  
invalid cast from type 'String' to type 'uint8\_t\* {aka unsigned char\*}'

so what do i need to change in order to be able to send variable strings which i send to the arduino via Serial?

TRANSMITTER

```cpp
#include <VirtualWire.h>

#undef int
#undef abs
#undef double
#undef float
#undef round

String msg;

void setup() {
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for RF Link module
  vw_setup(2000);                // Bits per sec
  vw_set_tx_pin(7);                // pin 3 is used as the transmit data out into the TX Link module, change this as per your needs
}

void loop() {

  delay(1000);

  if (Serial.available() > 0) {

    msg = "";
    
    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }
    
    

    vw_send((uint8_t *)msg, strlen(msg) );
    vw_wait_tx();                             
    delay(200);
  }

}

```

RECEIVER (working fine)

```cpp
#include <VirtualWire.h>    // you must download and install the VirtualWire.h to your hardware/libraries folder

#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  Serial.begin(9600);

  // Initialise the IO and ISR
  vw_set_ptt_inverted(true);    // Required for RX Link Module
  vw_setup(2000);                  // Bits per sec
  vw_set_rx_pin(6);          // We will be receiving on pin 4 i.e the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver
}

void loop() {
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)){
   
    for (int i = 0; i < buflen; i++) {

      Serial.print((char)buf[i]); 
    }
    
    Serial.println("");
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 11:05am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/2 "2016-02-25T11:05:11Z")

</div>

Convert your String to a char buffer (check the [documentation](https://www.arduino.cc/en/Reference/StringToCharArray)), or better still, don't use String in the first place.

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:22am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/3 "2016-02-25T11:22:07Z")

</div>

```cpp
if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char *tosend[strlen(msg) + 1];

    msg.toCharArray(tosend[strlen(msg) + 1], strlen(msg + 1));

    vw_send((uint8_t *)tosend, strlen(msg) + 1 );
    vw_wait_tx();
    delay(200);
  }

```

gives me following error:  
cannot convert 'String' to 'const char\*' for argument '1' to 'size\_t strlen(const char\*)'  
cannot convert 'String' to 'const char\*' for argument '1' to 'size\_t strlen(const char\*)'

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 11:25am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/4 "2016-02-25T11:25:22Z")

</div>

```cpp
char *tosend[strlen(msg) + 1];

```

What's that?

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:28am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/5 "2016-02-25T11:28:53Z")

</div>

> AWOL:
> 
> ```arduino
> char *tosend[strlen(msg) + 1];
> 
> ```
> 
> What's that?

a char buffer ? named tosend with the length of the message (msg) plus 1 to hold the NULL.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 11:31am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/6 "2016-02-25T11:31:03Z")

</div>

By my reading, it's an array of pointers to char.

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:34am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/7 "2016-02-25T11:34:08Z")

</div>

okay, so how do i convert the string msg into something which the line

```cpp
vw_send((uint8_t *)

```

can work with?

all i am trying to do is send the string msg via virtual wire.......

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 11:39am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/8 "2016-02-25T11:39:39Z")

</div>

```cpp
if (Serial.available() > 0) {

    msg = "";
   
    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

```

Before we go on, I need to ask how many times you expect that while loop to iterate?  
i.e. how many characters do you think "msg" will contain?

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:41am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/9 "2016-02-25T11:41:59Z")

</div>

it is variable because it depends on the length of the message. let's start with 100 characters maximum.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 11:44am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/10 "2016-02-25T11:44:13Z")

</div>

No, let's start with one character.

I asked how many time you thought the while loop would run, not how many character in total you would receive.

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 11:55am UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/11 "2016-02-25T11:55:19Z")

</div>

okay, starting with one character. means i have one byte and that means the while loop will run one time?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 12:02pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/12 "2016-02-25T12:02:13Z")

</div>

You extract data from a String like so:

```cpp
  String pissingAwayResources = "Do NOT use Strings!";

  char doThis[80];
  pissingAwayResources.toCharArray(doThis, sizeof(doThis));

```

A good read:  
[https://forum.arduino.cc/index.php?topic=288234.0](https://forum.arduino.cc/index.php?topic=288234.0)

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 12:10pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/13 "2016-02-25T12:10:58Z")

</div>

```cpp
if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char *tosend[msg.length() + 1];

    msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));

    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

```

this code still doesn't work. the receiver prints out only questionmarks, if anything.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 12:18pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/14 "2016-02-25T12:18:03Z")

</div>

> this code still doesn't work.

Of course not, because you are NOT paying attention.

You are creating an array of pointers to chars, NOT an array of chars. Why the hell are you doing that?

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 12:21pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/15 "2016-02-25T12:21:41Z")

</div>

if i create an array of chars i get "invalid conversion from 'char' to 'char\*'"

```cpp
if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char tosend[msg.length() + 1];

    msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));

    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

```

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 12:29pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/16 "2016-02-25T12:29:01Z")

</div>

problem solved. thank you!!!

```cpp
if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char tosend[msg.length() + 1];

    msg.toCharArray(tosend, sizeof(tosend));

    

    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 25, 2016, 12:31pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/17 "2016-02-25T12:31:41Z")

</div>

> TobiasCrackz:  
> if i create an array of chars i get "invalid conversion from 'char' to 'char\*'"
> 
> ```arduino
> if (Serial.available() > 0) {
> 
> ```

msg = "";

while (Serial.available()) {  
&nbsp; &nbsp; &nbsp;char received = Serial.read();  
&nbsp; &nbsp; &nbsp;msg += received;  
&nbsp; &nbsp;}

char tosend[msg.length() + 1];

msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));

vw\_send((uint8\_t \*)tosend, msg.length() + 1);  
&nbsp; &nbsp;vw\_wait\_tx();

delay(200);  
&nbsp;}

> ```arduino
> 
> ```

If you do it right, you won't. Look at your call to toCharArray() and my call to toCharArray() in reply #11. The first argument to toCharArray() is NOT the first element beyond the array (which is a char). It is the array.

---

<div class="post-metadata">

**Author:** ![TobiasCrackz](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TobiasCrackz](https://forum.arduino.cc/u/TobiasCrackz)\
**Post date:** [February 25, 2016, 12:36pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/18 "2016-02-25T12:36:05Z")

</div>

yupp, works. thanks pauls!!!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:45pm UTC](https://forum.arduino.cc/t/rf-virtual-wire-sending-string-issue/368017/19 "2021-05-05T21:45:02Z")

</div>


