博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 读取数据库数据,动态绘图
阅读量:4088 次
发布时间:2019-05-25

本文共 1475 字,大约阅读时间需要 4 分钟。

因为没有UI,运行时需要在提示符下输入串口相关参数,com端口,波特率...

手动输入相关串口参数

代码如下:

#-*- coding: utf-8 -*-

 

# 串口测试程序

import serial

import matplotlib.pyplot as plt

import numpy as np

import time

import re

 

 

# User input comport and bundrate

comport = input('Please input comport (like COM3) for your connected device: ')

baudrate = input('Please input baudrate (like 9600) for your connected device: ')

bytes = input('Please input bytes type of uart data (1->1 byte, 2->2 bytes): ')

bytes = int(bytes)

print('You selected %s, baudrate %d, %d byte.' % (comport, int(baudrate), bytes))

 

serialport = serial.Serial(comport, int(baudrate), timeout=1, parity=serial.PARITY_EVEN, rtscts=1)

if serialport.isOpen():

    print("open success")

else:

    print("open failed")

 

plt.grid(True) # 添加网格

plt.ion()    # interactive mode

plt.figure(1)

plt.xlabel('times')

plt.ylabel('data')

plt.title('Diagram of UART data by Python')

t = [0]

m = [0]

i = 0

intdata = 0

data = ''

count = 0

 

while True:

    if i > 300:    # 300次数据后,清除画布,重新开始,避免数据量过大导致卡顿。

        t = [0]

        m = [0]

        i = 0

        plt.cla()

    count = serialport.inWaiting()

    if count > 0 :

        if (bytes == 1):

            data = serialport.read(1)

        elif (bytes == 2):

            data = serialport.read(2)

        if data !='':

            intdata = int.from_bytes(data, byteorder='big', signed = False)

            print('%d byte data %d' % (bytes, intdata))

            i = i+1

            t.append(i)

            m.append(intdata)

            plt.plot(t, m, '-r')     

            # plt.scatter(i, intdata)

            plt.draw()

 

    plt.pause(0.002)

 

但单片机送出数据速度很快时, python plot 绘图会明显卡顿。

为解决此问题,已经用C# 重新做了个winform UI, 使用chart控件来绘图。可以参考本人另一篇博客。

转载地址:http://qduii.baihongyu.com/

你可能感兴趣的文章
[重新解答]百度笔试:数组按频次排列
查看>>
多个单例对象,析构顺序模拟
查看>>
weak_ptr使用的几个实例
查看>>
225. 用队列实现栈
查看>>
Centos7使用分别使用编译安装和yum安装Python3.6环境
查看>>
Centos7配置阿里的kubernetes的yum源
查看>>
Kubeternetes部署时init的时候踩过的坑及解决方案
查看>>
(python版)Leetcode-11.盛最多水的容器
查看>>
(python版) Leetcode-1.两数之和
查看>>
(python版) Leetcode-15.三数之和
查看>>
OpenCV计算机视觉实战 - Task5 - 停车场车位识别【项目实战】(附完整代码)
查看>>
(python版) Leetcode-350.两个数组的交集 II
查看>>
OpenCV计算机视觉实战 - Task6 - 答题卡识别判卷【项目实战】(附完整代码)
查看>>
如何刷Leetcode?【从400到700到900的全网总结篇】
查看>>
(python版) Leetcode-2.两数相加
查看>>
(python版) Leetcode-21.合并两个有序链表 merge-two-sorted-lists
查看>>
OpenCV计算机视觉实战 - Task7 - 背景建模 & 光流估计
查看>>
(python版) Leetcode-160.相交链表
查看>>
(python版) Leetcode-349.两个数组的交集
查看>>
(python版) Leetcode-283. 移动零
查看>>