arduino串⼝监视器数据传到本地txt⽂件
python
⼀、准备
arduino 的串⼝监视器能够传输数据 python
1. 运⾏程序出现报错 在arduino 烧⼊程序后不要打开串⼝,运⾏程序。(串⼝和程序只能开⼀个)关闭串⼝打开程序;打开程序打不开
串⼝
2. 安装必要的serial库 win+r cmd pip install pyserial(serial库应该有两个⼀个⽼版⼀个新版本-----百度)
python怎么读取串口数据⼆、使⽤步骤
1.引⼊库
# ctrl c  停⽌
import serial
import re
ser = serial.Serial('COM3',2500000, timeout=1) # 串⼝波特率
aa =open(r'本地⽬录\a.txt','w', encoding='utf-8')  # 本地⽬录没有⾃动创建
ser.write("testing".encode())
try:
while1:
wind = adline()  # 读取
st =str(wind)  # 列表转换字符串
sst = re.findall(r'\b\d+\b', st)  # 字符串提取数字
if sst ==[]:
sst =0
else:
aa.write(str(sst[0])+'\n')
except KeyboardInterrupt:
ser.close()
aa.close()