SERIAL communication between Arduino and PC

Tutorial on Serial Communication

When we talk about serial communication between two computers or between computer and a micro controller, The first thing which we required is the RS232 port or USB port. According to the industrial application mostly used port is RS232 .


PC -PC COMMUNICATION 
PC to PC circuit, each end handshake looped
To communicate between two PC without handshaking you need straight cable. i mean only tx , rx and gnd.
So,  i will explain the difference between the both, which i have noticed 
1. The RS232 port can work upto 40 meters where as USB can work only upto 5 meters.For more than 40 metres it depends on baud rate and resistance of wire.
2. We can get max upto 15V from RS232 but USB generates max upto 5 V.
So, for industrial application we prefer RS232 .
So, here is my universal code for serial communication between ARDUINO and PC

Connect Arduino to PC and depending upon the Operating System, there are some interfaces which  we can download as they are open source.
1. Hyper Terminal    Windows
2. Putty                    Windows
3. Mini Com             Linux
4. Serial monitor of arduino

BAUD RATE:

For example, your robot would pass sensor data to your laptop at 38400 bits per second and your laptop would listen for this stream of 1s and 0s expecting a new bit every 1/38400bps = 26us (0.000026 seconds). As long as the robot outputs bits at the pre-determined speed, your laptop can understand it.
Remember to always configure all your devices to the same baud rate for communication to work!

So Lets start.:
The below written code is for serial communication and according to this code if we send a data from computer using hyper terminal or minicom from LINUX ... we can read it very easily in arduino platform using its serial monitor.

ARDUINO CODE

void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()>0)   // To check what is coming on the serial buffer of the{
{
char in;
in=Serial.read();  // To check what is read in the serial buffer
Serial.println(in);  // Print the incoming data accordingly
}
}

No comments:

Post a Comment