起因:学校运河杯报了个项目,制作一个天气预测的装置。我用arduino跑了BME280模块,用蓝牙模块实现两块arduino主从机透传。但是为了分析,还需要提取出数据。因此我用python写了个上位机程序,用pyserial模块实现arduiho和电脑的串口通讯,再用xlwt模块写入excel表格,用time模块获取时间作为excel的文件名。

 import xlwt
import time
import serial
#设置表格样式
def set_style(name,height,bold=False):
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font
return style #写Excel
def write_excel():
if serial.isOpen():
print ('串口已打开\n')
f = xlwt.Workbook()
sheet1 = f.add_sheet('arduino_data',cell_overwrite_ok=True)
row0 = ["temp","pres","hum"]
time1=time.localtime(time.time())
#写第一行
for i in range(len(row0)):
sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))
i=1
time.sleep(5)
serial.flushInput()
while True:
try:
size = serial.inWaiting()
if size != 0:
response = serial.read(size) # 读取内容并显示
s=response.decode('utf-8').rstrip('\r\n').split('\t')
if len(s)!=3:
serial.flushInput()
continue
else:
try:
for j in range(len(s)):
sheet1.write(i,j,int(s[j]),set_style('Times New Roman',220,False))
print(s)
serial.flushInput() # 清空接收缓存区
i = i+1
time.sleep(0.5)
except ValueError:
serial.flushInput()
continue
except KeyboardInterrupt:
time2=time.localtime(time.time())
f.save(r'C:\Users\10020\Desktop\arduino_data\{0}.{1}_{2:0>2d}.{3:0>2d}.{4:0>2d}-{5}.{6}_{7:0>2d}.{8:0>2d}.{9:0>2d}.xls'.format\
(time1[1],time1[2],time1[3],time1[4],time1[5],
time2[1],time2[2],time2[3],time2[4],time2[5]))
serial.close()
print(time1)
print(time2)
quit() if __name__ == '__main__':
serial = serial.Serial('COM3',9600,timeout=2)
write_excel()

运行代码后会一直从串口读取arduino的数据,然后写入excel。按Ctrl+c来中止代码进程,此时会在C:\Users\10020\Desktop\arduino_data\这个文件夹下生成以“开始运行时间-结束运行时间”为名称的xls文件。

代码的运行效果:

需要注意的是:

  1. 串口和波特率根据电脑上显示的COM口和设置的arduino波特率决定
  2. arduino是通过串口发送字节串到电脑,需要编码成utf-8再对字符串进行处理
  3. 每一次接受完数据要清楚数据缓存

2019-10-14-14:44:49

python-Arduino串口传输数据到电脑并保存至excel表格的更多相关文章

  1. Python安装第三方库 xlrd 和 xlwt 。处理Excel表格

    1. 到   https://pypi.python.org/simple/xlwt/ 和https://pypi.python.org/simple/xlrt/ 下载 xlrd  和  xlwt  ...

  2. Python:27行代码实现将多个Excel表格内容批量汇总合并到一个表格

    序言 (https://jq.qq.com/?_wv=1027&k=GmeRhIX0) 老板最近越来越过分了,快下班了发给我几百个表格让我把内容合并到一个表格内去.还好我会Python,分分钟 ...

  3. Python脚本:实现对象集合List导入到excel表格,支持mysql,postergrsql,MongoDB

    import xlwt import os import datetime #验证export_filed中的字段是否在对象字段中 def checkField(obj_list,filed_dict ...

  4. win10上使用php与python实现与arduino串口通信

    注意: php 需要php7,安装及开启php_dio.dll com口按照实际的进行设置,如果不知道可以打开arduino编辑器进行查看 可以与用户实现命令行交互,但是效率过慢,不清楚如何优化,使用 ...

  5. ASCIITable: 演示 Arduino 串口输出的进阶功能

    原文地址 - https://www.arduino.cc/en/Tutorial/ASCIITable ASCII字符表 本例展示了高级的串口打印功能,通过本功能可以在Arduino软件(IDE)的 ...

  6. Python的串口通信(pyserial)

    串口通信是指外设和计算机间,通过数据信号线 .地线.控制线等,按位进行传输数据的一种通讯方式.这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低.串口是计算机上一种非 ...

  7. Arduino串口的一些高级用法

    1.配置串口通信数据位.校验位.停止位通常我们使用Serial.begin(speed)来完成串口的初始化,这种方式,只能配置串口的波特率.而使用Serial.begin(speed, config) ...

  8. Arduino 串口的一些高级用法

    来源: 1.配置串口通信数据位.校验位.停止位 通常我们使用Serial.begin(speed)来完成串口的初始化,这种方式,只能配置串口的波特率. 而使用Serial.begin(speed, c ...

  9. python使用get在百度搜索并保存第一页搜索结果

    python使用get在百度搜索并保存第一页搜索结果 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用在意我的感受 #coding:utf-8 import ur ...

随机推荐

  1. Kafka中的HW、LEO、LSO等分别代表什么?

    HW . LEO 等概念和上一篇文章所说的 ISR有着紧密的关系,如果不了解 ISR 可以先看下ISR相关的介绍. HW (High Watermark)俗称高水位,它标识了一个特定的消息偏移量(of ...

  2. 11-散列4 Hashing - Hard Version (30 分)

    Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...

  3. GitHub 上传文件过大报错:remote: error: GH001: Large files detected.

    1.查看哪个文件过大了 报错信息: remote: Resolving deltas: 100% (24/24), completed with 3 local objects. remote: wa ...

  4. Rattle

    Rattle使用RGtk2 包提供的Gnome图形用户界面,可以在WINDOWS,MAC OS/X,Linux等多个系统中使用. Rattle基于大量的R包:RGtk2, pmml, colorspa ...

  5. Sc config http start= disabled

    我不小心使用这个命令 Sc config http start= disabled, 现在http服务 无法启动 管理员运行 sc config http start= demand & ne ...

  6. Ansible14:Playbook条件语句

    目录 简介 when关键字 1. when基本使用 2. 比较运算符 3. 逻辑运算符 条件判断与tests 判断变量 判断执行结果 判断路径 判断字符串 判断整除 其他tests 条件判断与bloc ...

  7. Linux下常用目录有哪些?分别有什么作用?

    /boot:这个目录是用来存放与系统启动相关的文件 /root:root用户的家目录 /bin:存放大部分的二进制的可执行文件,也就是大部分的linux命令. /tmp:这个文件目录一般是公共的,也就 ...

  8. RuntimeError: Model class myapp.models.Test doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

    没有添加子应用在settings里面!

  9. HTML中的元素是有属性的:标准与解释器

    元素的属性只有有标准和相应的解释器才有存在的意义. HTML中的元素是有属性的:这些额外的属性值可以配置元素或者以各种方式来调整元素的行为,进而满足用户所需的标准. https://developer ...

  10. 使用位运算实现int32位 整数的加减乘除

    我觉得比较难想的是加法吧. 首先加法,脑海中脑补二进制加法,相同位相加,超过2 ,则进1,留0 那么用位运算怎么实现呢?其实理解了异或和与操作,就很容易想出来了. 我觉得异或操作和与操作完全就是实现加 ...