Home > Article > Backend Development > 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()
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!