Serial communication with Python

Serial communication using pySerial

Serial communication is still taken care of in embedded systems. Recently, Linux boards that can be used for embedding such as Raspberry Pi have come out, so it is convenient to be able to use serial communication from Python.

Install Since it is registered on PyPi, install it using pip or easy_install.

$ pip install pyserial

or

$ easy_install -U pyserial

How to use

Open and write

Very easy if you just output characters

>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0', 9600)  #Set the device name and baud rate and open the port
>>> ser.write("hello")      #output
>>> ser.close()             #Port close

Lead

Since timeout can be set easily, it can be easily set to non-blocking form.

>>> import serial
>>> ser = serial.Serial('/dev/ttyS0', timeout=0.1)  #Set timeout in seconds (default):None)Baud rate is 9600 by default
>>> c = ser.read()  #Read one character
>>> str = ser.read(10)  #Also read the number of characters specified, but only the amount that can be read
>>> line = ser.readline()  #Line end'¥n'Lead up to
>>> ser.close()

reference

Official documentation Welcome to pySerial’s documentation

Recommended Posts

Serial communication with Python
Serial communication with python
Socket communication with Python
HTTP communication with Python
Introduction to serial communication [Python]
View Python communication with Fiddler
Socket communication with Python LEGO Mindstorms
Serial communication with Raspberry Pi + PySerial
Just check serial communication with tk
I tried SMTP communication with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Serial communication control with python and I2C communication (using USBGPIO8 device)
Bingo with python
Zundokokiyoshi with python
Serial communication control with python and SPI communication (using USBGPIO8 device)
Excel with Python
Microcomputer with Python
Cast with python
Socket communication using socketserver with python now
[Python] [Windows] Serial communication in Python using DLL
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
Communication processing by Python
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python