13 – Serial Programming in Python

Communication is to be established between two devices to transfer data between them. Generally, two types of communication are used in telecommunication and data communication. They are,

  1. Serial Communication
  2. Parallel Communication

In Serial Communication only one bit is sent at a time. So only one wire is required to send data. In parallel communication generally, eight or more wires are used. Serial communication is preferred since no of wires used is less, reduces the cost of distance communication.In this post, serial communication using python programming is explained.

Since Serial Communication is to be established between two devices, Arduino is used as another device in this post. Arduino is programmed to send ‘Reply from Arduino‘ when it receives ‘s‘ from the computer using Python. To know more about Arduino click here.

ARDUINO CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char r;
void setup() {
  // initialize serial:
  Serial.begin(9600);
}
 
void loop() {
  if(Serial.available()>0){
    //To read a character
    r=Serial.read();
    if(r=='s')
    Serial.println("Reply from Arduino");
    //To send data only if the input is 's'
  }
}

OUTPUT OF SERIAL MONITOR:

PYTHON CODE:

To install the module for serial in python the following command is used.

pip install pyserial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#Only works in python 2.7
import serial
import time
#Port name
port='COM3'
baudrate=9600
ser=serial.Serial(port,baudrate,timeout=0)
for i in range(1):
    ser.write('s')
    time.sleep(0.1)
    #Read untill \n
    print "Using readline()", ser.readline()
    ser.flushInput()
    ser.write('s')
    time.sleep(0.1)
    #Read a single character
    print "Using read()", ser.read()
    ser.flushInput()
    ser.write('s')
    time.sleep(0.1)
    #Read upto 10 character's
    print "Using read(10)",ser.read(10)
    ser.flushInput()
    ser.write('s')
    time.sleep(0.1)
    #Read upto 100 character's
    print "Using read(100)",ser.read(100)

Port  – The port name to which the device is attached to. It can be found using Device Manager.

Baudrate – Data transfer speed in bits per second. The baud rate of the two devices should be same to establish communication.

read(No_of_characters) – To read the no of characters specified in the parameter.

readline() – To read until a line space or ‘\n’.

write() – To write to serial port.

OUTPUT:

1
2
3
4
5
6
7
8
9
10
>>> 
Using readline() Reply from Arduino
 
 
Using read() R
Using read(10) Reply from
Using read(100) Reply from Arduino
 
 
>>>
THANK YOU

 

SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.