如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:12951803
https://pypi.python.org/pypi/pyserial
Doc:http://pythonhosted.org/pyserial/
使用Python Package Index (PyPi)
pip install pyserial-3.1.1-py2.py3-none-any.whl
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) #数据写回
觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:12951803