News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(8)

[python] serial module, read and write serial data!

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.

1. The installation of the serial module

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

2. Use of the basic functions of the serial library

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

  1. ser.read() ####从端口读字节数据,默认1个字节
  2. ser.read_all() ####从端口接收全部数据
  3. ser.readline() ###读一行数据
  4. ser.readlines() ###读多行数据

3. Example demonstration

Go directly to the code:

  1. import serial
  2. import time
  3. if __name__ == '__main__':
  4. ser = serial.Serial('COM3',115200,timeout=5) ##连接串口,打开
  5. time.sleep(0.5)
  6.     Write = ser.write(b'Hello\n') ##发送数据
  7.     Read = ser.read() ###接收1个字节数据
  8.     print(Read)
  9.    
  10.     Write = ser.write(b'Hello\n') ##发送数据
  11.     Read = ser.readline() ##接收一行数据
  12. print(Read)
  13.     Write = ser.write(b'Hello\n') ##发送数据
  14.     Read = ser.read_all() ###接收所有数据
  15. print(Read)
  16.     Write = ser.write(b'Hello\n') ##发送数据
  17. Read = ser.readlines() ###读多行数据
  18. print(Read)
  19.     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



Category of website: technical article > Blog

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.

25 0
collect article
collected

Comment content: (supports up to 255 characters)