What is the method of calling multiple serial ports in Python?

王林
Release: 2024-03-01 18:07:25
forward
1221 people have browsed it

What is the method of calling multiple serial ports in Python?

In python, you can use the third-party library pyserial to implement multiple serial port calls. The following is a simple sample code:

import serial

# 设置串口参数
ser1 = serial.Serial('COM1', 9600)
ser2 = serial.Serial('COM2', 9600)

# 发送数据到串口1
ser1.write(b'Hello from COM1')

# 发送数据到串口2
ser2.write(b'Hello from COM2')

# 读取串口1的数据
data1 = ser1.read(10)
print(data1)

# 读取串口2的数据
data2 = ser2.read(10)
print(data2)

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

In the above example, we first import the serial library, and then open two serial ports COM1 and COM2 to send and receive data respectively. Finally close both serial ports. You can pass in different serial port parameters, send different data, and read different data according to your needs.

The above is the detailed content of What is the method of calling multiple serial ports in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:lsjlt.com
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!