Back to tutorial index

Reading data from serial in Python

Reading data from serial in Python is very simple, but this is only the start.. Install PySerial with pip install pyserial

# Import the library
import serial

# Try to connect to the port
try:
    fabkit = serial.Serial('/dev/tty.usbmodem1421', 9600)
except:
    print "Failed to connect"
    exit()

# Read data and print it to terminal... until you stop the program
while 1:
    line = fabkit.readline()
    print line

# We should close the connection... but since there's a while 1 loop before, we never reach this
fabkit.close()