Virtual Wire, need to be flushed each time?

I just playing with 315Mhz kit, communication goes well, but after second vm_send() message looks like if the receiver buffer goes hang, my question is, need i to flush the receiver buffer each time i do TX?

example: when i send (in loop section) after reset it sends data OK

Sensor Value:
456

BUT the second loop, it only show 3 chars

Received: Sen
Received: 362

the expected will be just as above

Received: Sensor Value
Received: 362

but it keep looping in the first 3 bytes, here is the TX code

/*
 
connect the Transmitter data pin to Arduino pin 12
*/
int j=0;
int analogPin=3;
#include <VirtualWire.h>

void setup()
{
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
Serial.begin(9600);
Serial.println("Starting");
}
void loop()
{
char msgtx[10];  
char prefix[]="Sensor Value:";
Serial.println(prefix);
Serial.println(strlen(prefix));  
vw_send((uint8_t *)prefix,strlen(prefix));
vw_wait_tx();
delay(100);
j=analogRead(analogPin);
sprintf(msgtx,"%i",j);
Serial.println(msgtx);
Serial.println(strlen(msgtx));
vw_send( (uint8_t *)msgtx,strlen(msgtx));
vw_wait_tx();

delay(1000);
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone

}

Here is the RX code

/*
 
*/
#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
Serial.println();
}
}

any idea?, comments are welcome!

thank you

Try this:

TX code:

#include <VirtualWire.h>

void setup() {
  Serial.begin(9600);
  vw_setup(1500);
  vw_set_tx_pin(12); //Set your tx pin
}

void loop() {
  char charMsgToSend[19] = "Sensor Value: ";
  char sensorValue[5];
  itoa(analogRead(A3), sensorValue, 10);
  strcat(charMsgToSend, sensorValue);
  Serial.println(charMsgToSend);
  vw_send((uint8_t *)charMsgToSend, strlen(charMsgToSend)+1);
  delay(2000);
}

RX Code:

#include <VirtualWire.h>

void setup()
{
  Serial.begin(9600);
  vw_setup(1500);
  vw_set_rx_pin(7); //Set your rx pin
  vw_rx_start();
}

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

No, it is not necessary to flush the receive buffer.

However, it is necessary to set the received message length to the maximum possible value, every time before listening, as shown in the example above.

  uint8_t buflen = VW_MAX_MESSAGE_LEN;

After a successful reception, buflen will be reset to the number of characters in the message.

Byork:
Try this:

TX code:

#include <VirtualWire.h>

void setup() {
  Serial.begin(9600);
  vw_setup(1500);
  vw_set_tx_pin(12); //Set your tx pin
}

void loop() {
  char charMsgToSend[19] = "Sensor Value: ";
  char sensorValue[5];
  itoa(analogRead(A3), sensorValue, 10);
  strcat(charMsgToSend, sensorValue);
  Serial.println(charMsgToSend);
  vw_send((uint8_t *)charMsgToSend, strlen(charMsgToSend)+1);
  delay(2000);
}




RX Code:



#include <VirtualWire.h>

void setup()
{
  Serial.begin(9600);
  vw_setup(1500);
  vw_set_rx_pin(7); //Set your rx pin
  vw_rx_start();
}

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

it works ok, so may be the diference between how do you declare the constants @t first time in the RX code (i just change the RX code part) as global?

as Jremington also point, if you put the declarations inside the loop thi can solve the buffer’s problem.

Tnx a lot