0% found this document useful (0 votes)
20 views

656 PDFsam PythonNotesForProfessionals

Uploaded by

Fabricio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

656 PDFsam PythonNotesForProfessionals

Uploaded by

Fabricio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 154: Python Serial Communication

(pyserial)
parameter details
port Device name e.g. /dev/ttyUSB0 on GNU/Linux or COM3 on Windows.
baudrate type: int default: 9600 standard values: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
baudrate
4800, 9600, 19200, 38400, 57600, 115200

Section 154.1: Initialize serial device


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

Section 154.2: Read from serial port


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)

Section 154.3: Check what serial ports are available on your


machine
To get a list of available serial ports use

python -m serial.tools.list_ports

at a command prompt or

from serial.tools import list_ports

GoalKicker.com – Python® Notes for Professionals 631

You might also like