Getting Serial communication data of particular length

Dear all.

I have list of serial statement.I have given delay of 1S to print data every second.

Now my intention is to get data of particular length without delay function. If i send request to read data only i could see serial out of particular length.my question How can modify code to read particular length of data.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial_call();
  delay(1000);
}

void Serial_call()
{
  //  Device_ID=ID_Check();

  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.print(",");

  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.println();


}

How can modify code to read particular length of data.

I am confused. The code that you posted does not read [u]any[/u] serial data.

Now my intention is to get data of particular length without delay function

I don't understand that.

I have serially printing 1s like below. if i say print data from start to end point with define length it should print.

How can i concat the serial data into string with coma separated values and then specify the length from start to end length of string.

If my string its length string[30]={1,2,3,4,5,6,7,8,9,9,8,7,4,5,6,1,2,3,5,6,8,6,7,6,7,5,3,4,5,6,6};
i wanted to print data from start to end with length 20 . first 20 length should be serially printed.

IN my below code; in real the reading from analog sensor and digital Io status. I wanted to make and strore as string (Mystring[50]);

whenever i send request of particular length .I need to print particular length of data.
There should not be delay in function; Request for particular length and print particular length of data

Serial.print(1);
** Serial.print(2);**
** Serial.print(3);**

void Serial_call()
{
  //  Device_ID=ID_Check();

  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.print(",");

  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(1);
  Serial.print(",");
  Serial.print(2);
  Serial.print(",");
  Serial.print(3);
  Serial.print(",");
  Serial.print(4);
  Serial.print(",");
  Serial.print(5);
  Serial.println();


}

I have attached compete code below. As fig 2 the marked region is valid data. where i have connected digital pin7 alone and not connected digital io 6 pins. Inspite of that its reading 1

I have attached compete code below

With your experience of the forum you know better than to attach pictures of your code. I will not provide any help until it is in a format that I can copy into an editor.

I am going to put code in parts.

void setup()
{
  wdt_enable(WDTO_8S);

  //analogReference(INTERNAL);

  pinMode(3, OUTPUT);
  pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
  pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
  pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
  //  pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
  pinMode(A0, INPUT) ;
  pinMode(A5, INPUT) ;
  //spd & DC STATUS
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);

  if (millis() < 10000)
  {
    Device_ID = ID_Check();
  }

  Serial.begin(57600);
}

void loop() {

  Device_ID = 2;
  Take_Reading();
  Serial_call();
  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();
  // delay(5000);
}

Serial call function look like this

void Serial_call()
{
  //  Device_ID=ID_Check();
  temp_int = Take_Temp_Reading();
  temp_int = (uint16_t)temp_int;

  int HV_value= Take_HV_Reading();
  
  New_String=Old_string+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value;                                                                                                       
 
 if (New_String.startsWith("1")) 
 {
    Serial.println(New_String);
  }else
  {
    Serial.println("Error device ID not match");
  
  }

  /* 
   Serial.print(Device_ID);
   Serial.print(",");
   Serial.print(Mod_current[0]);
   Serial.print(",");
   Serial.print(Mod_current[1]);
   Serial.print(",");
   Serial.print(Mod_current[2]);
   Serial.print(",");
   Serial.print(Mod_current[3]);
   Serial.print(",");
   Serial.print(Mod_current[4]);
   Serial.print(",");
   Serial.print(Mod_current[5]);
   Serial.print(",");
   Serial.print(Mod_current[6]);
   Serial.print(",");
   Serial.print(Mod_current[7]);
   Serial.print(",");
   Serial.print(Mod_current[8]);
   Serial.print(",");
   Serial.print(Mod_current[9]);
   Serial.print(",");
   Serial.print(Mod_current[10]);
   Serial.print(",");
   Serial.print(Mod_current[11]);
   Serial.print(",");
   
   Serial.print(Mod_current[23]);
   Serial.print(",");
   Serial.print(Mod_current[22]);
   Serial.print(",");
   Serial.print(Mod_current[21]);
   Serial.print(",");
   Serial.print(Mod_current[20]);
   Serial.print(",");
   Serial.print(Mod_current[19]);
   Serial.print(",");
   Serial.print(Mod_current[18]);
   Serial.print(",");
   Serial.print(Mod_current[17]);
   Serial.print(",");
   Serial.print(Mod_current[16]);
   Serial.print(",");
   Serial.print(Mod_current[15]);
   Serial.print(",");
   Serial.print(Mod_current[14]);
   Serial.print(",");
   Serial.print(Mod_current[13]);
   Serial.print(",");
   Serial.print(Mod_current[12]);
   Serial.print(",");
   
   Serial.print(temp_int);
   Serial.print(",");
   Serial.print(SPD);
   Serial.print(",");
   Serial.print(DISCONNECTOR);
   Serial.print(",");
   HV_Reading=Take_HV_Reading();
   Serial.println(HV_Reading);
   //  Serial.print(",");
   */

}

Functions look like this

int  Take_Temp_Reading()
{

  Temp_Total = Temp_Total - Temp_readings[Temp_index];
  Temp_readings[Temp_index] = analogRead(A5);
  Temp_Total = Temp_Total + Temp_readings[Temp_index];
  Temp_index = Temp_index + 1;

  if (Temp_index >= numReadings)
  {
    Temp_index = 0;
    Temp_Average = Temp_Total / numReadings;
  }
  temp = (Temp_Average * 5.0) / 1023.0;


  //  temp = float(analogRead(A5)) * 5.0 / 1024.0;
  temp_int = (int)(temp * 100.0);
  return (temp_int);
  // Serial.print("Temp"); Serial.println(temp);
}

int SPD_Check()
{

  SPD_STATUS = digitalRead(SPD_STATUS_PIN);
  return (SPD_STATUS);
}

int  DC_Status()
{

  DC_STATUS = digitalRead(DC_STATUS_PIN);
  return (DC_STATUS);

}



int   Take_HV_Reading()
{

  //int analog_int= analogRead(A4);

  total = total - readings[index];
  // read from the sensor:
  readings[index] = analogRead(A4);
  // add the reading to the total:
  total = total + readings[index];
  // advance to the next position in the array:
  index = index + 1;

  // if we're at the end of the array...
  if (index >= numReadings1)
  {
    index = 0;
    average = total / numReadings1;
  }

  HV_voltage = (average * 5.0) / 1023.0;

  if (HV_voltage <= 0.25)
  {

    Conv_HV_voltage = 0.0;
  }
  else
  {
    Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
  }
  HV_Reading = (uint16_t)(Conv_HV_voltage * 10);

  return(HV_Reading);
}



int array[8][3] = {
  {
    0, 0, 0
  }
  , {
    0, 0, 1
  }
  , {
    0, 1, 0
  }
  , {
    0, 1, 1
  }
  , {
    1, 0, 0
  }
  , {
    1, 0, 1
  }
  , {
    1, 1, 0
  }
  , {
    1, 1, 1
  }
};
void Take_Reading() {

  for (int row = 0; row < 8; row++)
  {
    //   // get rid of me:
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    delay(100);
    analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
    analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
    analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
    if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value1[row] =  0.0;
    }
    else
    {
      Current_Value1[row] =  (analog_1_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value2[row] =  0.0;
    }
    else
    {
      Current_Value2[row] =  (analog_2_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value3[row] =  0.0;
    }
    else
    {
      Current_Value3[row] =  (analog_3_pv[row] * 12.5) - 31.25;

    }


    Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
    Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
    Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);


  }



}

Rather than posting a zillion lines of code, most of which is not relevant, why not create a small example program that shows what you want to do ? Perhaps set a variable (is it a String or a string ?) to a value and a function to print it or some of it.

Here is simple code written for understanding. NewString is my printable string requirement:

Looking for sending serial command; based on which i get reply.

1) I should get data based on request send. If I say read length of 20 Its should always print length of 20 string. length can be defined using Serial.command

2) Getting data continuously with defined time delay. delay time can set/reset using Serial command.

String NewString;
String Oldstring;
int sensorValue;
int voltage;
int Device_ID;
static int SPD_STATUS_PIN = 6;
static int DC_STATUS_PIN = 7;
int SPD;
int DISCONNECTOR;

unsigned  int DC_STATUS = 0;
unsigned int SPD_STATUS = 0;

int SPD_Check()
{

  SPD_STATUS = digitalRead(SPD_STATUS_PIN);
  return (SPD_STATUS);
}

int  DC_Status()
{

  DC_STATUS = digitalRead(DC_STATUS_PIN);
  return (DC_STATUS);

}


void setup()
{
  // put your setup code here, to run once:
  Serial.begin(57600);
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Device_ID=1;
  Serial_call();

  delay(1000);
}

void Serial_call()
{
  //  Device_ID=ID_Check();
  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();
  
 /* Serial.print("First:");
  NewString=Oldstring+Device_ID+","+123+","+568+","+789+","+845+","+1024+","+365+","+852+","+258+","+785+","+9875+","+6589;
  Serial.println(NewString);*/
  sensorValue  = analogRead(A0);
  voltage  = (sensorValue * (5.0 / 1023.0))*100;
  
   NewString=Oldstring+Device_ID+","+568+","+SPD+","+DISCONNECTOR+","+voltage;
  
//Serial.print("Second:");
  
  
 if (NewString.startsWith("1")) 
 {
    Serial.println(NewString);
  }else
  {
    Serial.println("Error device ID not match");
  
  }

}

In your example NewString ends up holding 1,568,0,0,356 but I am not clear what you want to do with it.

It seems that you want to send a command via a serial interface to return part or all of it on demand and also to be able to send a command to send some or all of it continuously with a variable time delay. Is that what you want ?

We might find some other info in this thread; I suspect crosspost.

@AMPS-N Let me try to rephrase in an attempt to understand

On command (received from serial port), you want to execute the reading of your 'sensors' and send e.g. the fields 1-10 or 5-15 or 30-100 or ... back to the serial monitor. One line, specified fields, comma separated.

Is that correct?

UKHeliBob:
In your example NewString ends up holding 1,568,0,0,356 but I am not clear what you want to do with it.

It seems that you want to send a command via a serial interface to return part or all of it on demand and also to be able to send a command to send some or all of it continuously with a variable time delay. Is that what you want ?

This is data i am sending serially over serial port.Serial data is send every particular frame of time from loop. I have data like this

1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,4324
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,4324
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,4324
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,4324
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,4324
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,1,1,3840
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3840
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3840
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3840
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3840
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3560
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3560
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3560
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,194,0,1,3560
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,0,1,3560
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,0,1,4188

The data being received serially every time execute loop. I am looking for without using delay function. By serial write function request for Frame of data to print.

#include <avr/wdt.h>
#include"glob.h"
String Old_string;
String New_String;

int array[8][3] = {
  {
    0, 0, 0
  }
  , {
    0, 0, 1
  }
  , {
    0, 1, 0
  }
  , {
    0, 1, 1
  }
  , {
    1, 0, 0
  }
  , {
    1, 0, 1
  }
  , {
    1, 1, 0
  }
  , {
    1, 1, 1
  }
};

void setup()
{
  wdt_enable(WDTO_8S);

  //analogReference(INTERNAL);

  pinMode(3, OUTPUT);
  pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
  pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
  pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
  //  pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
  pinMode(A0, INPUT) ;
  pinMode(A5, INPUT) ;
  //spd & DC STATUS
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);

  /*  if (millis() < 10000)
   {
   Device_ID = ID_Check();
   }
   */
  Serial.begin(57600);
}



void loop() {
  wdt_reset();
  Device_ID = 1;
  int HV_value= Take_HV_Reading();
  Take_Reading();
  Serial_call();
  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();


}

Serial call function . Here i am checking weather device ID start with 1 or different . if 1 ; receive valid data else show error print out.

This operation should be carried out serially like modbus

Like we requesting with same function should be done serially.

void Serial_call()
{
  //  Device_ID=ID_Check();
  temp_int = Take_Temp_Reading();
  temp_int = (uint16_t)temp_int;

  int HV_value= Take_HV_Reading();

  New_String=Old_string+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value;                                                                                                       

  if (New_String.startsWith("1")) 
  {
    Serial.println(New_String);
  }
  else
  {
    Serial.println("Error device ID not match");

  }

}

In simple word need to explain. I have connected with multiple slave device. Data i am taking serially .

Assume these are out format Of mystring. First bit 1,2,3 are the device ID

With above my code i am getting data for every 1S or loop get executes.

My intention is if send serial request ( if device ID and DeviceName) matched then only Get data. This request has to be send extenally

Device out1

1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,0,1,4188

device2 out

2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,0,1,4188

device 3out

1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,0,1,4188

My code look like this

#include <avr/wdt.h>
#include"glob.h"
String Old_string;
String New_String;

int array[8][3] = {
  {
    0, 0, 0
  }
  , {
    0, 0, 1
  }
  , {
    0, 1, 0
  }
  , {
    0, 1, 1
  }
  , {
    1, 0, 0
  }
  , {
    1, 0, 1
  }
  , {
    1, 1, 0
  }
  , {
    1, 1, 1
  }
};

#include"glob.h"

void Serial_call()
{
  //  Device_ID=ID_Check();
  temp_int = Take_Temp_Reading();
  temp_int = (uint16_t)temp_int;

  int HV_value= Take_HV_Reading();

  New_String=Old_string+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value;                                                                                                       

  if (New_String.startsWith("1")) 
  {
    Serial.println(New_String);
  }
  else
  {
    Serial.println("Error device ID not match");

  }

}


void Take_Reading() {

  for (int row = 0; row < 8; row++)
  {
    //   // get rid of me:
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    delay(100);
    analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
    analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
    analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
    if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value1[row] =  0.0;
    }
    else
    {
      Current_Value1[row] =  (analog_1_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value2[row] =  0.0;
    }
    else
    {
      Current_Value2[row] =  (analog_2_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value3[row] =  0.0;
    }
    else
    {
      Current_Value3[row] =  (analog_3_pv[row] * 12.5) - 31.25;

    }


    Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
    Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
    Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);


  }

}









int ID_Check() {

  int ID_value;
  for (int row = 0; row < 8; row++)
  {
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    Status_Out[row] = digitalRead(Output_Read);

  }

  ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2] + 64 * Status_Out[1] + 128 * Status_Out[0];
  return (ID_value);

}




int  Take_Temp_Reading()
{

  Temp_Total = Temp_Total - Temp_readings[Temp_index];
  Temp_readings[Temp_index] = analogRead(A5);
  Temp_Total = Temp_Total + Temp_readings[Temp_index];
  Temp_index = Temp_index + 1;

  if (Temp_index >= numReadings)
  {
    Temp_index = 0;
    Temp_Average = Temp_Total / numReadings;
  }
  temp = (Temp_Average * 5.0) / 1023.0;


  //  temp = float(analogRead(A5)) * 5.0 / 1024.0;
  temp_int = (int)(temp * 100.0);
  return (temp_int);
  // Serial.print("Temp"); Serial.println(temp);
}

int SPD_Check()
{

  SPD_STATUS = digitalRead(SPD_STATUS_PIN);
  return (SPD_STATUS);
}

int  DC_Status()
{

  DC_STATUS = digitalRead(DC_STATUS_PIN);
  return (DC_STATUS);

}



int   Take_HV_Reading()
{

  //int analog_int= analogRead(A4);

  total = total - readings[index];
  // read from the sensor:
  readings[index] = analogRead(A4);
  // add the reading to the total:
  total = total + readings[index];
  // advance to the next position in the array:
  index = index + 1;

  // if we're at the end of the array...
  if (index >= numReadings1)
  {
    index = 0;
    average = total / numReadings1;

  }
  else
  {

    HV_voltage = (average * 5.0) / 1023.0;
  }
  if (HV_voltage <= 0.25)
  {

    Conv_HV_voltage = 0.0;
  }
  else
  {
    Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
  }

  HV_Reading = (uint16_t)(Conv_HV_voltage * 10);

  //  HV_Reading = (uint16_t)(Conv_HV_voltage * 10);

  return(HV_Reading);
}



void setup()
{
  wdt_enable(WDTO_8S);

  //analogReference(INTERNAL);

  pinMode(3, OUTPUT);
  pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
  pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
  pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
  //  pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
  pinMode(A0, INPUT) ;
  pinMode(A5, INPUT) ;
  //spd & DC STATUS
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);

  /*  if (millis() < 10000)
   {
   Device_ID = ID_Check();
   }
   */
  Serial.begin(57600);
}



void loop() {
  wdt_reset();
  Device_ID = 1;
  int HV_value= Take_HV_Reading();
  Take_Reading();
  Serial_call();
  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();


}

Here is code where i send Serial request to turn on and turn off relay.

Similarly i would like to send request for above code; Where i should read my string data when i send external command

1)command 1:
if(<device_ID>,<Device_name>)matched then only print Mystring

  1. command 2:

If send request Delay,time
based on time choose i should get read or print myString data

  1. Serial command to switching from Command 1 and command 2
#include <MsTimer2.h>


int Analog_Pin=5;
int newaverage;
float Output_Current;
#define RELAY1  7
#define MAX_TRIP_COUNT  5
float Current_Limit=0.6;
static int Trip_Count=0;
static int Tripped_Flag=1;
int Serial_Status=0;



void TakeReading()
{
  newaverage = analogRead(A5);
  
//  Serial.print("newaverage:");
  //Serial.println(newaverage);
  Output_Current = 0.0336666666667*newaverage - 17.17; 
}


void Chk_Relay_Tripped()
{
  if(Output_Current>=Current_Limit)
  {
    Trip_Count=Trip_Count+1; 
    if((Trip_Count>=MAX_TRIP_COUNT) &&(Serial_Status==0))
    {

      Trip_Count=0;
     Relay_Activate();
    }
    else
      if(Serial_Status==1)
      {
       // Serial.println("> MODE");
        Relay_Deactivate(); 
      }

  } 
  else
  {
    Trip_Count=Trip_Count-1;
    if(Trip_Count<0)
    {
      Trip_Count=0;
    }

    if(Trip_Count<MAX_TRIP_COUNT && Serial_Status==1)
    {
      Relay_Deactivate();
      Serial_Status=0;
    }

  }

}




void Relay_Activate()
{
   // MsTimer2::stop();
   
   for (unsigned long start = millis(); millis() - start < 10000;)
  {
    digitalWrite(RELAY1,HIGH);
    Serial_Status=1;
  }
   /*
    digitalWrite(RELAY1,HIGH);
    Serial_Status=1;
  */
}

void  Relay_Deactivate()
{
  digitalWrite(RELAY1,LOW);
 // Serial_Status=0;
}

void Relay_Intialize()
{
  digitalWrite(RELAY1,LOW);
}

void setup() {
  Serial.begin(9600); // set serial speed
  pinMode(RELAY1, OUTPUT); // set LED as output
  //digitalWrite(RELAY1, LOW); //turn off LED

  pinMode(Analog_Pin,INPUT);
  Relay_Intialize();
  MsTimer2::set(1000, Print_Result); // 500ms period
  MsTimer2::start();
  
  
}




void loop(){
  TakeReading();
  Chk_Relay_Tripped();
  
   //while (Serial.available() == 0); // do nothing if nothing sent
  int val = Serial.read() - '0';
  if (val == 1) { // test for command 1 then turn on LED
Serial.println("RELAY on");
    digitalWrite(RELAY1, LOW); // turn on LED
    Serial_Status=1;

  }
  else if (val == 0) // test for command 0 then turn off LED
  {
   Serial.println("RELAY OFF");
    digitalWrite(RELAY1, HIGH); // turn off LED
    Serial_Status=0;
  }
  delay(500);
}


void Print_Result()
{
  Serial.println(Output_Current);
  //Serial.flush();
 }

My intention is if send serial request ( if device ID and DeviceName) matched then only Get data. This request has to be send extenally

So what your requirement boils down to is that what you want to do is receive a message via Serial, parse it and act on its contents.

Have you looked at Serial input basics

Based on the code in reply #11. Is this basically want you need?

void loop() {
  wdt_reset();
  Device_ID = 1;
  int HV_value = Take_HV_Reading();
  Take_Reading();

  if(Serial_received() == true)
  {
    Serial_call();
  }

  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();
}

And you need help with the implementation of serial_received? As stated by UKKHeliBob, check the Serial Input Basics thread.

I have tested code. I would like to read several data from monitor

Tested below code ; i could not able to read any data

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(57600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

I tried first part . thats reading single parts. i can read single string

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
    if (Serial.available() > 0) {
        receivedChar = Serial.read();
        newData = true;
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChar);
        newData = false;
    }
}

To test the second example (first one in your reply #15), you need to set up serial monitor to send a newline (bottom right of the serial monitor gives you that option using a drop down box).

This part seems working . This what i am looking only need to map with MY existing string

If user send command <HelloWorld, 12, 24.7> from PC . parse below string and display output. Can someone help out to do it

New_String=Old_string+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ “,”+Mod_current[23]+ “,”+Mod_current[22]+ “,”+Mod_current[21]+ “,”+Mod_current[20]+ “,”+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ “,”+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value;

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
    Serial.begin(57600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
}

The function that is generally used to parse a string is called strtok(); the Serial Input Basics thread contains examples how to use it.

The alternative can be the use of strchr() as its use allows non-destructive parsing and hence no copy of the input data is required.

I have modified code bit.I need String compare function.
Where i can compare 2 parts of string and serial print result.

The out put file attached before comparison.I would like to compare first 2 string send from pc with
New_String first 2 string of csv.

If string comaprison matched then only send data else give error

#include <avr/wdt.h>
#include"glob.h"
String Old_string;
String New_String;
String Received_String;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
char messageFromPC[numChars] = {
  0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;




int array[8][3] = {
  {
    0, 0, 0
  }
  , {
    0, 0, 1
  }
  , {
    0, 1, 0
  }
  , {
    0, 1, 1
  }
  , {
    1, 0, 0
  }
  , {
    1, 0, 1
  }
  , {
    1, 1, 0
  }
  , {
    1, 1, 1
  }
};

void setup()
{
  wdt_enable(WDTO_8S);
  Serial.begin(57600);
  //analogReference(INTERNAL);

  pinMode(3, OUTPUT);
  pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
  pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
  pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
  //  pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
  pinMode(A0, INPUT) ;
  pinMode(A5, INPUT) ;
  //spd & DC STATUS
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);

  /*  if (millis() < 10000)
   {
   Device_ID = ID_Check();
   }
   */
  Serial.println("Enter data in this style <Device_ID, 12, 24.7>  ");

}



void loop() {
  wdt_reset();
  recvWithStartEndMarkers() ;

  if (newData == true)
  {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
  }


  Device_ID = 1;
  int HV_value= Take_HV_Reading();
  Take_Reading();
  //  Serial_call();
  SPD = SPD_Check();
  DISCONNECTOR =  DC_Status();
  //  Serial_Command();

}








void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;

    }
  }

}

//============

void parseData() {      // split the data into its parts

  Serial.println(receivedChars);
  char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  Received_String=Received_String+messageFromPC+","+integerFromPC;



}

//============

void showParsedData() {

  Serial.print("data send from PC:");
  Serial.println(Received_String);
  Serial.println();
  Serial.println();

  Serial.print("Device_name: ");
  Serial.println(messageFromPC);
  Serial.print("Device_ID: ");
  Serial.println(integerFromPC);
  Serial.println();
  Serial.println();

  temp_int = Take_Temp_Reading();
  temp_int = (uint16_t)temp_int;

  int HV_value= Take_HV_Reading();

  New_String=Old_string+"Device_Name"+","+Device_ID+","+Mod_current[0]+","+Mod_current[1]+","+Mod_current[2]+","+Mod_current[3]+","+Mod_current[4]+","+Mod_current[5]+","+Mod_current[6]+","+Mod_current[7]+","+Mod_current[8]+","+Mod_current[9]+","+Mod_current[10]+","+Mod_current[11]+ ","+Mod_current[23]+ ","+Mod_current[22]+ ","+Mod_current[21]+ ","+Mod_current[20]+ ","+Mod_current[19]+","+Mod_current[18]+","+Mod_current[17]+","+Mod_current[16]+","+Mod_current[15]+","+Mod_current[14]+","+Mod_current[13]+","+Mod_current[12]+ ","+ temp_int +","+ SPD+","+ DISCONNECTOR+","+ HV_value;                                                                                                       


  Serial.println(New_String);
 
 Received_String="";


}