实时绘图实在绘不出来..请有了解的帮我看一下哪儿错了

2018-04-06 01:13:23 +08:00
 yellowtail
# Uncomment the next two lines if you want to save the animation
#import matplotlib
#matplotlib.use("Agg")

from matplotlib.pylab import *
from mpl_toolkits.axes_grid1 import host_subplot
import matplotlib.animation as animation
import numpy as np
import matplotlib.pyplot as plt
import time
import visa

rm=visa.ResourceManager()
print(rm.list_resources())
#A=rm.open_resource("ASRL5::INSTR")
A=rm.open_resource('USB0::0x05E6::0x2100::8007164::0::INSTR')
B=rm.open_resource("ASRL4::INSTR")
A.write("*RST ")
A.write("*CLS ")
B.timeout=25000
B.write("*RST ")
B.write("*CLS ")
B.write(":SENSE:FUNCTION 'RES'")
B.write(":FORMAT:ELEMENTS RES")

B.write(":OUTPUT ON")
#inst.write("CONFigure:RESistance")
A.write("CONFigure:VOLTage:DC")
# Sent for figure
font = {'size' : 9}
matplotlib.rc('font', **font)

# Setup figure and subplots
f0 = figure(num = 0, figsize = (12, 8))#, dpi = 100)
f0.suptitle("test", fontsize=12)
ax01 = subplot2grid((2, 2), (0, 0))
ax02 = subplot2grid((2, 2), (0, 1))
ax03 = subplot2grid((2, 2), (1, 0), colspan=2, rowspan=1)
ax04 = ax03.twinx()
#tight_layout()

# Set titles of subplots
ax01.set_title('Votage vs Time')
ax02.set_title('Resistance vs Time')
ax03.set_title('Votage and Resistance vs Time')

# set y-limits
ax01.set_ylim(1.67,5)
ax02.set_ylim(0,20)
ax03.set_ylim(1.67,5)
ax04.set_ylim(0,20)

# sex x-limits
ax01.set_xlim(0,2000)
ax02.set_xlim(0,2000)
ax03.set_xlim(0,2000)
ax04.set_xlim(0,2000)

# Turn on grids
ax01.grid(True)
ax02.grid(True)
ax03.grid(True)

# set label names
ax01.set_xlabel("time")
ax01.set_ylabel("Votage")
ax02.set_xlabel("time")
ax02.set_ylabel("Resistance")
ax03.set_xlabel("time")
ax03.set_ylabel("Votage")
ax04.set_ylabel("Resistance")

# Data Placeholders
yp1=zeros(0)
yv1=zeros(0)
t=zeros(0)

# set plots
p011, = ax01.plot(t,yp1,'b-', label="yp1")


p021, = ax02.plot(t,yv1,'b-', label="yv1")


p031, = ax03.plot(t,yp1,'b-', label="yp1")
p032, = ax04.plot(t,yv1,'g-', label="yv1")

# set lagends
ax01.legend([p011], [p011.get_label()])
ax02.legend([p021], [p021.get_label()])
ax03.legend([p031], [p031.get_label()])

# Data Update

x=0

def updateData(self):

global yp1
global yv1
global t
global x

a1 =float(A.query("READ?"))
b1 =float(B.query("READ?"))
yp1=append(yp1,a1)
yv1=append(yv1,b1)
t=append(t,x)
x+=1

p011.set_data(t,yp1)


p021.set_data(t,yv1)


p031.set_data(t,yp1)
p032.set_data(t,yv1)


return p011, p021, p031, p032

# interval: draw new frame every 'interval' ms
# frames: number of frames to draw
simulation = animation.FuncAnimation(f0, updateData, blit=False, frames=200, interval=20)

# Uncomment the next line if you want to save the animation
#simulation.save(filename='sim.mp4',fps=30,dpi=300)

plt.show()

制图的代码是网上找的,自己进行了修改。运行以后图片的窗口会卡死。请大家帮我看一看,谢谢!
2522 次点击
所在节点    Python
6 条回复
yellowtail
2018-04-06 03:01:21 +08:00
重新调试了一下 发现每次给 a1 和 b1 赋固定的数 是可以实现的 但是调用其他的读数就出问题
yellowtail
2018-04-06 03:35:59 +08:00
又调试了一遍 a1 可以读数 但是只要 b1 读数就卡死
yellowtail
2018-04-06 04:03:15 +08:00
成功了 原因和以前 仪器传输的信号没有添加分隔符\r 程序无法分割信号 造成卡死 是不是可以说图片窗口出来以后 制图的代码就没什么问题了?
ZoomQuiet
2018-04-06 08:50:38 +08:00
是也乎 ╮(╯▽╰)╭

等等…为毛一定要弹窗?
输出为硬盘图片文件不好嘛?
xmoiduts
2018-04-06 09:03:19 +08:00
先将就着用,遇到问题再查也来得及。但是硬件(串口)传数据有可能缺字节导致数据异常,要做好判定机制。
@ZoomQuiet 说不定是无线抄表上位机呢 /滑稽
bravecarrot
2018-04-06 11:32:45 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/444577

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX