通常,您与微通信所做的工作是将单个字符用于轻量级或创建通信协议。基本上你有一个开始标志,结束标志和某种校验和,以确保数据正确传输。有很多方法可以做到这一点。

以下代码适用于Python 3.您可能必须对字节数据进行更改。

# On micro

data = b"[Hello,1234]"

serial.write(data)

在电脑上,你会运行,如果您使用的是GUI

def read_data(ser, buf=b'', callback=None):

if callback is None:

callback = print

# Read enough data for a message

buf += ser.read(ser.inwaiting()) # If you are using threading +10 or something so the thread has to wait for more data, this makes the thread sleep and allows the main thread to run.

while b"[" not in buf or b"]" not in buf:

buf += ser.read(ser.inwaiting())

# There may be multiple messages received

while b"[" in buf and b']' in buf:

# Find the message

start = buf.find(b'[')

buf = buf[start+1:]

end = buf.find(b']')

msg_parts = buf[:end].split(",") # buf now has b"Hello, 1234"

buf = buf[end+1:]

# Check the checksum to make sure the data is valid

if msg_parts[-1] == b"1234": # There are many different ways to make a good checksum

callback(msg_parts[:-1])

return buf

running = True

ser = serial.serial("COM10", 9600)

buf = b''

while running:

buf = read_data(ser, buf)

线程是非常有用的。然后,当GUI显示数据时,您可以让线程在后台读取数据。

import time

import threading

running = threading.Event()

running.set()

def thread_read(ser, callback=None):

buf = b''

while running.is_set():

buf = read_data(ser, buf, callback)

def msg_parsed(msg_parts):

# Do something with the parsed data

print(msg_parsed)

ser = serial.serial("COM10", 9600)

th = threading.Thread(target=thread_read, args=(ser, msg_parsed))

th.start()

# Do other stuff while the thread is running in the background

start = time.clock()

duration = 5 # Run for 5 seconds

while running.is_set():

time.sleep(1) # Do other processing instead of sleep

if time.clock() - start > duration

running.clear()

th.join() # Wait for the thread to finish up and exit

ser.close() # Close the serial port

注意,在线程例子中,我使用回调它是被作为一个变量传递后来被称为函数。另一种方式是将数据放入队列中,然后在队列的另一部分处理队列中的数据。

python串口连续数据_Python代码从串口连续接收可变数据相关推荐

  1. python后端接收数据_python后端开发使用flask接收前端数据,处理后返回结果

    from flask import Flask, request import json, time, datetime import pysolr import pandas as pd ''' 使 ...

  2. python示波器 波形数据_Python在嵌入式开发中的应用——数据示波器

    Python在嵌入式开发中的应用 引言 在嵌入式开发中我们常常要进行数据分析.算法设计.原型验证.自动化测试.辅助工具设计,每个环节的工作效率对整体的开发都非常重要.选用一个好的辅助开发工具是非常必要 ...

  3. python天勤金叉编程代码大全_天勤终端数据解决方案

    作者:金属成色 目录 环境准备 如果没有安装vn.py最新发行版(这里默认为vnpy-1.7.1).MongoDB.Anaconda.PyCharm软件,请参照右边'环境安装'教程. 对于天勤数据解决 ...

  4. python抓取数据库数据_Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储...

    Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...

  5. python两列相乘_Python代码将两列相乘,然后用值创建新列

    我正在编写一个python代码,它首先将分钟数据平均为小时数据.然后我想乘以小时数据中两列中的值,并用乘以的值创建一个新列.我被困在乘法步上了.import pandas as pd import n ...

  6. python从txt拿取数据_python requests + xpath 获取分页详情页数据存入到txt文件中

    直接代码,如有不懂请加群讨论 # *-* coding:utf-8 *-* # import json import requests import pytesseract import time i ...

  7. python抽取某个时间段的数据_python 批量提取excel 指定时间段的数据

    [Python] 纯文本查看 复制代码import pandas as pd import os import datetime path = r'C:\Users\plm\Desktop\text1 ...

  8. python模拟登录爬取数据_python 模拟登录爬取淘宝数据

    淘宝现在需要登录才能爬取搜索商品,首先在登录页面登录chrome F12 开发者模式抓包 登录请求 发现有一个post请求,这个就是登录的请求了,看下面的from data 登录信息 由from da ...

  9. matlab如何读取excel文件中的数据?_Python自动化之从Excel文件读取数据

    前言: 在Python语言,常用的excel读写库有xrld和openpyxl两个,当然pandas库也可以从excel文件中读取数据,但这里不建议使用.有个问题就是,xrld只能用于读取数据而不能用 ...

最新文章

  1. 【百度地图API】如何制作班级地理通讯录?LBS通讯录
  2. mac 10.12显示隐藏文件
  3. A*寻路算法与它的速度
  4. atlas怎么看日志_亿级的日志治理!微服务最佳方案,ELK stack从零搭建
  5. 使用MFC开发ActiveX控件
  6. SDWebImage点滴
  7. 5000册售罄!怎能不为你骄傲?「博客出书的故事⑤」
  8. CF888E Maximum Subsequence (Meet in the middle,贪心)
  9. 初步理解NServiceBus
  10. 2019.03.25 bzoj4572: [Scoi2016]围棋(轮廓线dp)
  11. 分布排序(distribution sorts)算法大串讲
  12. Android Zip解压缩目录穿越导致文件覆盖漏洞
  13. linux 蓝牙串口 调试,linux 蓝牙串口 连接android手机调试
  14. 思科路由器如何导出配置文件_如何备份cisco路由器配置文件
  15. 基于SSM企业人事管理系统.doc
  16. 超级管理器Android,超级文件管理器官方最新版下载-超级文件管理器 安卓版v5.5.2-PC6安卓网...
  17. ethtool修改网卡mac地址流程
  18. fydeos 安装linux程序,FydeOS安装教程-电脑系统安装手册
  19. 快递企业如何完成运单订阅消息的推送
  20. 坚果的2022年终总结

热门文章

  1. 开发者须知:哪些广告最让用户感到厌烦
  2. 给Nodejs回调加超时限制
  3. browser.html – HTML 实现 Firefox UI
  4. java openSession和getCurrentSession的比较
  5. mysql报错ERROR 1045 (28000)
  6. ARP***原理及解决方法与CMD命令分类(1)
  7. 2018.12.20 Spring环境如何搭建
  8. Vijos CoVH之再破难关(搜索+hash)
  9. nginx 配置虚拟主机
  10. linux android真机测试