python 串口读取+存储+输出处理

研究了一晚上的成果。

	import serial
	import win32com.client
	import matplotlib.pyplot as plt
	import numpy as np
	ser = serial.Serial("COM6", 115200, bytesize = 8,timeout=0.5)  # 打开串口
	print("我要开始了:")
	res=[]
	while (1):
	    ch = ser.readline(1)
	    result = ''
	    hLen = len(ch)
	    for i in range(hLen):
	        hvol = ch[i]
	        hhex = '%02x' % hvol
	        result += hhex + ' '
	    sz=[]
	    res.append(result)
	
	    if len(res)==100:
	        j=0
	        for i in range(len(res)-1):
	            # 十六进制转十进制
	            if "fc" in res[i]:
	                print(i)
	                sz.append(int(res[i + 1], 16)*256+int(res[i],16))
	                #print(sz)
	            else:
	                continue
	
	        print("asd")
	        print(type(sz[1]))
	
	        speaker = win32com.client.Dispatch("SAPI.SpVoice")
	        speaker.Speak("请保持均匀呼吸")
	        x = []
	        for i in range(0, len(sz)):
	            x.append(i)
	        plt.plot(x, sz)
	        plt.show()
	        print(res)
	        break
	



  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值