Python Language Python Serial Communication (pyserial) Read from serial port

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Initialize serial device

import serial
#Serial takes two parameters: serial device and baudrate
ser = serial.Serial('/dev/ttyUSB0', 9600)

to read single byte from serial device

data = ser.read()

to read given number of bytes from the serial device

data = ser.read(size=5)

to read one line from serial device.

data = ser.readline()

to read the data from serial device while something is being written over it.

#for python2.7
data = ser.read(ser.inWaiting())

#for python3
ser.read(ser.inWaiting)


Got any Python Language Question?