Serial communication with python

Use pyserial

For serial communication, it is convenient to use pyserial.

install

First, install pyserial using pip.

$ pip install pyserial

only this.

How to use

Send

Import, configure, output, and finally close. Very easy.

>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial-******', 9600, timeout=1)
>>> ser.write("hello")
>>> ser.close()

In the setting part, specify the device name as the first argument, then the baud rate, and then the timeout. If you want to look up the device name on your mac, go to the terminal

$ ls /dev/tty.*

If you enter,'/dev/tty.usbserial-*****' will be displayed, so you can check it.

Receive

In the argument of read (),

--If nothing is specified, one character is read --If you specify an integer (such as 10), read the specified number of characters. --Read to the end of the line with readline ()

>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial-*****', 9600, timeout=1)
>>> c = ser.read()
>>> string = ser.read(10)
>>> line = ser.readline()
>>> ser.close

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