1.安装pyserial
https://pypi.python.org/pypi/pyserial
Doc:http://pythonhosted.org/pyserial/
使用python package index (pypi)
1
|
pip install pyserial - 3.1 . 1 - py2.py3 - none - any .whl |
2. demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import serial from time import sleep def recv(serial): while true: data = serial.read_all() if data = = '': continue else : break sleep( 0.02 ) return data if __name__ = = '__main__' : serial = serial.serial( 'com5' , 9600 , timeout = 0.5 ) #/dev/ttyusb0 if serial.isopen() : print ( "open success" ) else : print ( "open failed" ) while true: data = recv(serial) if data ! = b'' : print ( "receive : " ,data) serial.write(data) #数据写回 |
3.结果
以上这篇python 串口读写的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/itas109/article/details/78874165