Saturday 11 April 2015

Parsing Commands


/*
Author : Kunchala Anil
*/
#define max_size 40
char data_buffer[max_size];
char data_cpy[max_size];
char data_cmd[max_size];
char data_val[max_size];
char question_1[] = "Please enter the command with value";
char cmd_1[] = "anil";
char cmd_2[] = "sunil";
char end_char = '@';
void setup(){
Serial.begin(9600);
Serial.println("\t\t Arduino Parsing commmands");
}//end of setup()

void loop(){
ask_question(question_1);
serial_check();
serial_output();
parse_commands();

}//end of loop()

void ask_question(char* question){
Serial.println(question);
}//end of ask_question() function

void serial_check(){
while(!Serial.available()){
//wait until user enters the data
}//end of while
while(!received()){
//loop until full data received
//Serial.println("received loop In serial_check() function");
}//end of while(received) function

}//end of serial_check() function

boolean received(){
static byte index = 0;
char input;
if(Serial.available()){
input = Serial.read();
if(input == '\r'){
data_buffer[index] = end_char;//Null terminating the string
data_buffer[index+1] = 0;//Null terminating the string
index = 0;//reset the index variable
return true;
}
else{
data_buffer[index] = input;
index = index + 1;
}//end of If-Else condition
}//end of IF condition
return false;
}//end of received() function

void serial_output(){
Serial.println("the data you enteres is");
Serial.println(data_buffer);
Serial.print("having a length of\t-->");
int length = strlen(data_buffer);
Serial.print(length);
Serial.print("\n");
Serial.print("\tindex \tchar\n");
for(int i=0;i<length;i++){
Serial.print("\t ");
Serial.print(i);
Serial.print("\t ");
Serial.print(data_buffer[i]);
Serial.print("\n");
}//end of for loop
}//end of serial_output() function


//FUNCTION TO PARSE COMMANDS
void parse_commands(){
byte index_cpy = 0;

int cmd_no = 0;

int length = strlen(data_buffer);
for(int i =0;i<length+1 ;i++){
if(data_buffer[i] == end_char){
cmd_no = cmd_no+1;
Serial.print("command no \t");
Serial.print(cmd_no);
Serial.print("\tis received\n");

parse_it(data_cpy);//call the function to find command and value
memset(data_cpy,0,strlen(data_cpy));
index_cpy = 0;
}else{
data_cpy[index_cpy] = data_buffer[i];
index_cpy = index_cpy + 1;
}//end of If Else condition
}//end of for loop

}//end of parse_commands() function

//FUNCTION TO SEPERATE COMMAND AND VALUE
void parse_it(char* data){
byte index_cmd = 0;
int length = strlen(data);
for(int i = 0; i < length+1; i++){
if(!isdigit(data[i])){
data_cmd[index_cmd] = data[i];
index_cmd = index_cmd + 1;
}else{
know_cmd(data_cmd,data);
memset(data_cmd,0,strlen(data_cmd));
memset(data,0,strlen(data));
break;
}//end of If Else condition
}//end of for loop
}//end of parse_it() function


void know_cmd( char* cmd,char* val){
if(strncmp(cmd,cmd_1,strlen(cmd_1)) == 0){
Serial.println("you received command for Anil");
float result = get_val(val);
Serial.print("The value for anil is\t");
Serial.print(result);
Serial.println("");
}else if(strncmp(cmd,cmd_2,strlen(cmd_2)) == 0){
Serial.println("you received command for Sunil");
get_val(val);
}else{
Serial.println("You received wrong command");
}//end of If Else-If condition
//memset(cmd,0,i);

}//end of know_cmd() fucntion

float get_val(char* val){
byte index_val = 0;
for(int j=0; j<strlen(val)+1; j++){
if(isdigit(val[j])){
data_val[index_val] = val[j];
index_val = index_val + 1;
}
}//end of for loop
Serial.println("the value of the command is");
Serial.println(data_val);
float return_val = atof(data_val);
memset(data_val,0,strlen(data_val));
return return_val;
}//end of get_val function

No comments:

Post a Comment