Hello Paul....
Thanks a lot for your help. Moving forward to your suggestion here is my final code.. Its heavily amateurish on my part cause i am not a programmer.. Seems to be working now somehow.. please suggest what more can be done to optimize this.
/*
Example to receive data from serial port in format <a,123456> and parse it to store in three eeprom location 10,11,12 by receiving as string then converting to integer.
Maximum number size to be received from serial port is six digits. for longer number add mode Cyclesint , epri and eprs.
*/
#define SOP '<'
#define EOP '>'
#include <EEPROM.h>
bool started = false;
bool ended = false;
int commaPosition;
String text;
char inData[10]; //Complete data recieved from Serial port
byte index;
String cyclesStr; // Value part of the recieved string
String command; // Command part of the recieved string
int Cyclesint; //First two characters of value integer to be stored in EEPROM location 10
int Cyclesint1;//First two characters of value integer to be stored in EEPROM location 11
int Cyclesint2;//First two characters of value integer to be stored in EEPROM location 12
long int epri1;//EEPROM location 10 Value
long int epri2;//EEPROM location 11 Value
long int epri3;//EEPROM location 12 Value
long int eepr; //Concatinated integer from 10~12 EEPROM locations
String eprs1; //First two characters of value string
String eprs2; //Next two characters of value string
String eprs3; //Last two characters of value string
void setup()
{
Serial.begin(9600);
epri1 = EEPROM.read(10);
epri2 = EEPROM.read(11);
epri3 = EEPROM.read(12);
eepr = epri1*10000+epri2*100+epri3; // Numbers Concatenation
Serial.println(eepr); //Print final number to be read from eeprom after concatenation of integers
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read(); //RAW serial DATA
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
if(started && ended) // The end of packet marker arrived. Process the packet
{
text = inData;
String message = text; // holds text not yet split
// the position of the next comma in the string
do
{
commaPosition = message.indexOf(',');
if(commaPosition != -1)
{
command = message.substring(0,commaPosition) ;
message = message.substring(commaPosition+1, message.length());
}
else
{ // here after the last comma is found
if(message.length() > 0)
cyclesStr = message ;
}
}
while(commaPosition >=0);
eprs1 = cyclesStr.substring(0,2) ; //parse first two charachters
eprs2 = cyclesStr.substring(2,4) ; //parse next two charachters
eprs3 = cyclesStr.substring(4,6) ; // parse last two charachters
Serial.println(command);
Serial.println(cyclesStr);
char tarray[3];
eprs1.toCharArray(tarray, sizeof(tarray));
Cyclesint = atoi(tarray);
char tarray1[3];
eprs2.toCharArray(tarray1, sizeof(tarray));
Cyclesint1 = atoi(tarray1);
char tarray2[3];
eprs3.toCharArray(tarray2, sizeof(tarray));
Cyclesint2 = atoi(tarray2);
EEPROM.write(10,Cyclesint); //Write first two characters to location 10
EEPROM.write(11,Cyclesint1); //Write next two characters to location 11
EEPROM.write(12,Cyclesint2); //Write last two characters to location 12
delay(100); //delay for EEPROM write
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Data needs to be sent as <command,123456>