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

yipeiwu_com7年前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
	

以上这篇python 串口读取+存储+输出处理实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Java中重定向输出流实现用文件记录程序日志

System中的out,error都是final类型的,不能做改动。但通过setOut()可以设置新的输出流,从而实现写日志的功能。 import java.io.PrintStre...

Python 3.x基于Xml数据的Http请求方法

Python 3.x基于Xml数据的Http请求方法

1. 前言 由于公司的一个项目是基于B/S架构与WEB服务通信,使用XML数据作为通信数据,在添加新功能时,WEB端与客户端分别由不同的部门负责,所以在WEB端功能实现过程中,需要自己发...

python通过Windows下远程控制Linux系统

python通过Windows下远程控制Linux系统

一、学习目标 【通过Windows下远程控制Linux系统实现对socket模块认识】 二、实验环境 Windows下(模拟客户端 [ IP:192.168.43.87 ] ):pyth...

python3 selenium自动化 frame表单嵌套的切换方法

python3 selenium自动化 frame表单嵌套的切换方法

在web自动化测试中,测试工程师经常会碰到frame表单嵌套结构,直接定位会报错,我们需要切换表单后才能成功定位。 我拿QQ邮箱登录来作为例子说下frame怎么切换。 qq邮箱页面按F...

使用Python微信库itchat获得好友和群组已撤回的消息

使用Python微信库itchat获得好友和群组已撤回的消息

具体代码如下所述: #coding=utf-8 import itchat from itchat.content import TEXT from itchat.content i...