posted on 2023-06-06 09:54 read(660) comment(0) like(25) collect(3)
Serial port communication refers to a communication method that transmits data bit by bit through data signal lines, ground lines, control lines, etc. between peripherals and computers. This communication method uses fewer data lines, which can save communication costs in long-distance communication, but its transmission speed is lower than parallel transmission. The serial port is a very common device communication protocol on the computer. The pyserial module encapsulates python's access to the serial port and provides a unified interface for the use of multiple platforms.
Install using the pip interface
pip install serial
The detailed description of the pip interface can be seen: https://blog.csdn.net/pengneng123/article/details/129556320
1. Serial port initialization function
ser = serial.Serial('COM3',115200,timeout=5)
Parameter 1: com3 is the port number of the serial port
Parameter 2: 115200 is the baud rate of the serial port
Parameter 3: timeout is the timeout setting of the serial port
2.write serial port write data
Write = ser.write(b'bsp\n')
Send data to the serial port
b: This parameter represents the bytes type, sending a string directly will report an error
\n: means newline
bsp: the content to send
3.read() serial port read data
- ser.read() ####从端口读字节数据,默认1个字节
-
- ser.read_all() ####从端口接收全部数据
-
- ser.readline() ###读一行数据
-
- ser.readlines() ###读多行数据
Go directly to the code:
- import serial
- import time
-
- if __name__ == '__main__':
- ser = serial.Serial('COM3',115200,timeout=5) ##连接串口,打开
- time.sleep(0.5)
-
- Write = ser.write(b'Hello\n') ##发送数据
- Read = ser.read() ###接收1个字节数据
- print(Read)
-
- Write = ser.write(b'Hello\n') ##发送数据
- Read = ser.readline() ##接收一行数据
- print(Read)
-
- Write = ser.write(b'Hello\n') ##发送数据
- Read = ser.read_all() ###接收所有数据
- print(Read)
-
- Write = ser.write(b'Hello\n') ##发送数据
- Read = ser.readlines() ###读多行数据
- print(Read)
-
- ser.close() ###关闭串口连接
output:
There are also the following functions and functions
ser.isOpen() | Check if the port is open |
ser.open() | open port |
flush() | wait for all data to be written out |
flushInput() | discard all data in the receive buffer |
flushOutput() | Terminate the current write operation and discard the data in the send buffer |
The basic serial read and write operations are as shown above.
@Neng
Author:Abstract
link:http://www.pythonblackhole.com/blog/article/79721/4ee2c9ceeaf314342cd0/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!