Monday 17 November 2014

Serial Communication on Arduino

Just I wanted to you know guy's  whatever i write here not is my own brain....
its just a collection of so many resources..... using my own words... I will to try to make it simple.. and reply me if there is any mistakes.. and those mistakes are most welcomed.

It took me almost two weeks to understand a bit of Serial Communication Fundamentals... I thought these are worth mentioning

Ok.. First of all we can communicate with arduino usb cable connected to Pc using digital pins 0 and 1. arduino provides an serial monitor to communicate with it.
In Arduino Uno digital Pins 0 and 1 are called Hardware serial. they communicate with built-in UART in arduino.

we need to know two commands( I don't know they are called commands? tell me if you know) to receive the data from arduino serial.
1.Serial.available()
2.Serial.read()

One Quick Point: Whatever we Send or Receive from the serial is a character. a byte of data.

when we call Serial.available(), it returns us the number of bytes in the Serial Buffer and '0' if there is nothing.

Serial.read(), will returns a single byte or character from the serial buffer and we receive the oldest entry in the buffer.

we can send the data using Serial.print() command.

so if we want to send a data from arduino to pc via Serial communication.. the program will look like this.

void setup(){
Serial.begin(9600); //start the Serial communication 9600 is baud rate. baud rate : bits per second
Serial.println("Arduino");//print the data
}
void loop(){
// here loop doing nothing....
}


I don't know why we write void setup() and void loop().... these are just one of the rules and regulations you have to follow otherwise you won't survive in arduino world. there are reasons for them But I dont wanna know.. why.. I dont wanna go that deep in the arduino ocean.. I am just here touching the water.. I dont wanna go and drown my self in ... I dont even know how to swim yet.
Ok.. now back to the Serial.print("");
when we call the serial.print(""), it will write the data into serialbuffer and send the data byte by byte using interrupts.. and again i dont want to go deep. I wanted to you to know that arduino just load it to that buffer and serial interrupts take care of it. so arduino wont wait until it sends finishing.
we can check this using Serial.flush()
Quick point : Serial.flush() is only for Transmitter Section. It just wait till the transmitter is fully transferred.
To receive data serially first we need to check is there any data available on serial buffer.
we can do this by using Serial.available() command.
Quick point: we don't have control over the serial communication
so to receive data serially the program is like

void setup(){
Serial.begin(9600); start serial communication with 9600 baud rate
}
void loop(){
while(Serial.available()){
//wait until user enters data or serial data available
}
char in_byte = serial.read();
Serial.println(in_byte);
}


the above code just receives one byte from Serial and prints it in Serial monitor.

what if we wanted to receive more than one byte.
Quick note: if we used baud rate 9600, then to receive 1 byte it takes 10 micro seconds(approximately).
But to execute a instruction arduino takes less than 50 nano seconds. so it with in the time of receiving a single character, arduino will execute a 100's of executions. So it will be a waste of time to wait for all the serial data to come.

So we dont want waste the arduino time.
so we want to send a signal that all the data is received on arduino and then only we have to read the data. otherwise we will be reading whatever it is present in the buffer, it may be half of our required data or full data if we are lucky... I dont considered myself as lucky.. so i don't wanted to go on that way.
So lets write our program.....
goal of the program : we have to read the full data (Complete String) from the serial Buffer.
Quick note : when we press the "Enter". we are actually sending a carriage return. so by default when we enter data we are sending a data + carriage return
Ok.. lets start the coding.
first we need to have a buffer to store the data which is received in the serial buffer. so we need to initialize the buffer

#define data_buffer_size 40 // maximum number of bytes we may receive
char data_buffer[data_buffer_size +1 ] // +1 is for Null character


quick note: in c++ we have to add a null character '0' at the end of the character array, so the compiler will recognize it as end of the array (I am not sure if is it write or wrong let me know if it is wrong)

after that we need to write the setup() function.. goes like this.

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

if you wanted to learn arduino, besides books.. Arduino forum is a great resource.. there are so many people like us struggling with this little board and its coding..
many users are providing help and showing a way to solve our problems...
there is post by robin2. which is really helpful if you are new to programming language like me Planning and Implementing an Arduino Program.

In loop() function


void loop(){
const String question_1 = "Please enter the Data_1 "; // specifying the question to be asked
ask_user_ques(question_1); // this function is used to ask the user question. it passes argument question to the function where it is printed on serial monitor
while(!get_the_response()){;//get_the_response() function is used to check whether data is fully received or not. if total data is received it stored in the data buffer for further use
//wait until get_the_response() returns true
}///end of the while function
// here we received the full data so display it
Serial.print("the data you entered is --------> \t"); // "\t" leaves the Tab space
Serial.print(data_buffer);// print the received data
Serial.println("") // print a line
}//end of the loop function


//here we are going to define our two functions
void ask_user_ques(String question){
Serial.println(question); // print the data on the serial monitor
return;
}//end of the ask_user_ques() function


boolean get_the_response(){
char in_byte; // variable to assign the received character
byte byte_index; // to indicate the number of bytes
while(!Serial.available()){
//wait until user enters the data
// note the "!" sign
}//end of while loop
while(Serial.available()){ // this loop continuously checks is there any data available in the Serial buffer and run the loop until it is empty.
in_char = Serial.read(); // read the character from the serial buffer. we can only read one character at a time
if (in_char == '\r'){ // if received character is the end character... we can change this to our requirements
data_buffer[byte_index] = 0; // terminating the character array with Null Terminator
byte_index = 0// resetting the byte_index so it can read from the stating
return true; // we received the all data return true. so we can read the data from data_buffer variable
}//end of the if
else {// if we received anything else other than ending character
data_buffer[byte_index] = in_char; // add the received character into the data buffer
byte_index++;// increment the byte index which is equal to byte_index = byte_index+1
}
}// end of the while loop
return false; // we do not received the full data Yet.. So Return false
}// end of get_the_response() functtion

this is my first i hope you understand it. if you don't.. please let me know.......

No comments:

Post a Comment