save serial data to a char and convert it to a string

hi guys.
i connect arduino to nodemcu and getting word "test" from arduino in shape ascii code array.

i want to

  1. convert that ascii array to char. (in code i did it)

  2. then save each char to a char array .(means in first i get "t" and store it in a char a[] then i get "e" from arduino and store it in char a array till end.)

  3. in last part i want to convert that char array a to a string .
    this is my code:

#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);

char inchar;
char a[];
String string;

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

void loop()
{

while (Serial.available())
{
    inchar  = s.read();
    // in this part i want 2 and 3.
}

Serial.println(string);

  while (!s.available())
  {
    delay(5000);
    Serial.print("not available");
  }
}

A couple of things:

  1. Use of String is discouraged because of memory fragmentation
  2. Single character identifiers are discouraged in code
  3. Your receive buffer will have to be declared with a maximum size of message you expect to receive (including terminating characters)
  4. How will you be sure you have received the complete message? You will be reading the serial buffer faster than data can be received. You need some way of determining you have received the whole message.

The code below should get you started:

const int MAX_RX_CAHRS = 20;
char rx_buf[MAX_RX_CAHRS];

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

void loop()
{
  static int idx = 0;
  while (Serial.available())
  {
    rx_buf[idx++]  = s.read();
  }
  
  // Determine if you have received all characters.  If so, process rx_buf and reset
  // idx to 0.  Otherwise, return and get rest of message.

  if (/* message received */)
  {
     // process receive message
  }
  else
  {
     // return or do other stuff
  }
}

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

ToddL1962:
A couple of things:

  1. Use of String is discouraged because of memory fragmentation
  2. Single character identifiers are discouraged in code
  3. Your receive buffer will have to be declared with a maximum size of message you expect to receive (including terminating characters)
  4. How will you be sure you have received the complete message? You will be reading the serial buffer faster than data can be received. You need some way of determining you have received the whole message.

The code below should get you started:

const int MAX_RX_CAHRS = 20;

char rx_buf[MAX_RX_CAHRS];

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

void loop()
{
  static int idx = 0;
  while (Serial.available())
  {
    rx_buf[idx++]  = s.read();
  }
 
  // Determine if you have received all characters.  If so, process rx_buf and reset
  // idx to 0.  Otherwise, return and get rest of message.

if (/* message received */)
  {
    // process receive message
  }
  else
  {
    // return or do other stuff
  }
}

ToddL1962:
A couple of things:

  1. Use of String is discouraged because of memory fragmentation
  2. Single character identifiers are discouraged in code
  3. Your receive buffer will have to be declared with a maximum size of message you expect to receive (including terminating characters)
  4. How will you be sure you have received the complete message? You will be reading the serial buffer faster than data can be received. You need some way of determining you have received the whole message.

The code below should get you started:

const int MAX_RX_CAHRS = 20;

char rx_buf[MAX_RX_CAHRS];

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

void loop()
{
  static int idx = 0;
  while (Serial.available())
  {
    rx_buf[idx++]  = s.read();
  }
 
  // Determine if you have received all characters.  If so, process rx_buf and reset
  // idx to 0.  Otherwise, return and get rest of message.

if (/* message received */)
  {
    // process receive message
  }
  else
  {
    // return or do other stuff
  }
}

i got the message completly. but i need to convert it to string cuz i want to send it completly to localhost.

1. Please, post complete codes for both Arduino UNO/NANO/MEGA Transmitter and NodeMCU Receiver.

2. You are sending the string "test" from Transmitter and receiving by NodemCU and then saving into this array (for example): char rx_buf[MAX_RX_CAHRS]; which contains the received message as a string. Then, what have wanted to mean by the following statement:?

'in last part i want to convert that char array a to a string.'

char a[]; Don't try storing anything to that.