How to implement serial communication in python

王林
Release: 2024-02-20 22:21:03
Original
1351 people have browsed it

How to implement serial communication in python

Title: Implementation and code examples of Serial communication in Python

Introduction:
Serial communication is a common method for data transmission and is widely used In various fields, such as Internet of Things, embedded systems, robots, etc. As a simple and easy-to-use scripting language, Python also provides the Serial library to implement serial communication functions. This article will introduce how to use Python's Serial library for serial communication, and attach specific code examples.

1. Installation of the Serial library
Before performing serial communication in Python, we need to install the PySerial library. You can enter the following command through the command line to install:

pip install pyserial
Copy after login

After the installation is complete, we can use the Serial library in Python.

2. Basic usage of the Serial library
The Serial library provides a series of interfaces and methods for setting and controlling serial port parameters and sending and receiving data. The following is an example of usage of the Serial library:

  1. Import Serial library

    import serial
    Copy after login
  2. Create Serial object

    ser = serial.Serial()
    Copy after login
  3. Open the serial port

    ser.port = 'COM1'  # 设置串口号
    ser.baudrate = 9600  # 设置波特率
    ser.open()  # 打开串口
    Copy after login
  4. Send data

    ser.write(b'Hello, world!')  # 发送数据
    Copy after login
  5. Receive data

    data = ser.readline()  # 读取数据
    print(data.decode())  # 打印数据
    Copy after login
  6. Close the serial port

    ser.close()  # 关闭串口
    Copy after login

3. Practical application example
In order to demonstrate the use of the Serial library more clearly, below we will show a simple practical application example of serial communication: sending temperature data through the serial port, and Receive and print temperature information from the serial port.

  1. Sender code:

    import serial
    import time
    
    ser = serial.Serial()
    ser.port = 'COM1'
    ser.baudrate = 9600
    ser.open()
    
    while True:
     temperature = input("请输入当前温度:")  # 输入温度
     ser.write(temperature.encode())  # 发送温度数据
    
     time.sleep(1)  # 延时1秒
    Copy after login
  2. Receiver code:

    import serial
    
    ser = serial.Serial()
    ser.port = 'COM2'
    ser.baudrate = 9600
    ser.open()
    
    while True:
     data = ser.readline()  # 读取串口数据
     temperature = float(data.decode())  # 将字符串转换为浮点数
     print("当前温度:{}℃".format(temperature))  # 打印温度信息
    Copy after login

    In the above example, The sending end obtains the temperature data through the input function, and sends the data to the serial port after encoding. The receiving end reads data from the serial port, decodes the data into a string through the decode function, and then converts it into a floating point number and prints it.

    Conclusion:
    Through the use of the Serial library in Python, we can easily implement serial communication and send and receive data. We hope that the code examples in this article can help readers better understand and apply the Serial library to achieve various serial communication needs.

    The above is the detailed content of How to implement serial communication in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!