Introduction to serial communication [Python]

Introduction

It's a simple "** Introduction to learning and implementing serial communication **".

What is serial communication?

Serial communication: A communication method that sequentially transmits digital data bit by bit. Parallel communication: Communication method for simultaneous transmission of multiple bits of digital data

Communication method Advantages Disadvantages
Serial communication low cost slow
Parallel communication High cost fast

If you want to know more, this article is recommended. I tried to summarize the serial communication method for beginners of electronic work in an easy-to-understand manner

Implemented in Python (pyserial)

Install pyserial

First, hit the following command to install the package. (You don't have to enter $.)

python


$ pip install pyserial

Search for devices that can communicate

First, search for devices that can communicate. Try hitting the following command. (The second line is the response from the terminal.)

python


$ ls -l /dev/tty.*
crw-rw-rw-  1 root  wheel   18,   0 11 28 17:48 /dev/tty.Bluetooth-Incoming-Port

A device called /dev/tty.Bluetooth-Incoming-Port was found.

Read

Next, let's read it.

read.py


import serial

readSer = serial.Serial('/dev/tty.Bluetooth-Incoming-Port',9600, timeout=3)
c = readSer.read() # 1 byte
string = readSer.read(10) # 10 byte
line = readSer.readline() # 1 line (upto '\n')
print("Read Serial:")
print(c)
print(string)
print(line)
readSer.close()

Code description

  1. Install the package.
  2. Set the device name, baud rate (communication speed), timeout, and open the port.
    (serial.Serial (device name, baud rate, timeout))
  3. Read and print.
  4. Close the port.

Write

write.py


import serial

serialCommand = "test"
writeSer = serial.Serial('/dev/tty.Bluetooth-Incoming-Port',9600, timeout=3)
writeSer.write(serialCommand.encode())
writeSer.close()

Code description

  1. Install the package.
  2. Set the device name, baud rate (communication speed), timeout, and open the port.
    (serial.Serial (device name, baud rate, timeout))
  3. Encode and write the command you want to send.
  4. Close the port.

Whole sample code

main.py


import serial

print("===== Start Program =====\n")

# Set Parameter
deviceName = '/dev/tty.Bluetooth-Incoming-Port'    # search by `ls -l /dev/tty.*`
baudrateNum = 9600
timeoutNum = 3
print("===== Set Parameter Complete =====\n")

# # Read Serial
readSer = serial.Serial(deviceName, baudrateNum, timeout=timeoutNum)
c = readSer.read()
string = readSer.read(10)
line = readSer.readline()
print("Read Serial:")
print(c)
print(string)
print(line)
readSer.close()
print("===== Read Serial Complete =====\n")

# Write Serial
serialCommand = "test"
writeSer = serial.Serial(deviceName, baudrateNum, timeout=timeoutNum)
writeSer.write(serialCommand.encode())
writeSer.close()
print("===== Write Serial Complete =====\n")

print("===== End Program =====\n")

References

-I tried to summarize the serial communication method for beginners of electronic work in an easy-to-understand manner -[Introduction to Python] Explaining how to execute serial communication with pySerial -Serial communication with python

Recommended Posts

Introduction to serial communication [Python]
Serial communication with Python
Serial communication with python
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to Python Django (2) Win
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
[Introduction to Udemy Python3 + Application] 18. List methods
Introduction to Scrapy (3)
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
Introduction to Supervisor
Introduction of Python
Introduction to Tkinter 1: Introduction
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Python] [Windows] Serial communication in Python using DLL
Introduction to PyQt
[Introduction to Udemy Python3 + Application] 45. enumerate function
Introduction to Scrapy (2)
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Python] Let's use foreach with Python
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 65. Exception handling