【发布时间】:2017-05-24 06:11:50
【问题描述】:
首先我知道我的问题经常被问到。但我没有在其中找到解决方案。
我使用 USBTMC 来控制示波器。 Here 你可以找到更多关于它的信息。我能够捕获屏幕并将其写入文件(见图)。但我想每隔n 秒实时绘制屏幕。例如,我想使用matplotlib.pyplot。
这是我的代码(拼命尝试用pyplot 绘制数据):
import usbtmc
from time import sleep
import matplotlib.pyplot as plot
import numpy as np
import subprocess
maxTries = 3
scope = usbtmc.Instrument(0x0699, 0x03a6)
print scope.ask("*IDN?")
scope.write("ACQ:STOPA SEQ")
scope.write("ACQ:STATE ON")
while ( True ):
#get trigger state
trigState = scope.ask("TRIG:STATE?")
#check if Acq complete
if ( trigState.endswith('SAVE') ):
print 'Acquisition complete. Writing into file ...'
#save screen
scope.write("SAVE:IMAG:FILEF PNG")
scope.write("HARDCOPY START")
#HERE I get binary data
screenData = scope.read_raw()
#HERE I try to convert it?
strData = np.fromstring( screenData, dtype=np.uint8 )
#HERE I try to plot previous
plot.plot( strData )
plot.show()
#rewrite in file (this works fine)
try:
outFile = open("screen.png", "wb")
outFile.write( screenData )
except IOError:
print 'Error: cannot write to file'
else:
print 'Data was written successfully in file: ', outFile.name
finally:
outFile.close()
#continue doing something
【问题讨论】:
-
您是否尝试过
plt.imshow(strData),但首先您需要根据屏幕尺寸对数据进行整形:strData.reshape(screen_y, screen_x),其中 screen_x * screen_y =len(strData)。 -
我像你说的那样试过了,我收到以下错误
Traceback (most recent call last): File "./ScreenCapture.py", line 31, in <module> strData.reshape( screen_y, screen_x ) ValueError: cannot reshape array of size 77878 into shape (77878,77878) -
我应该提一下,如果你的数组大小是 77878,那么屏幕 x,y 应该是这个因素。但是 77878 的因数是 1693 x 46,这是一个奇怪的屏幕尺寸,所以你得到的数据可能包含的信息不仅仅是屏幕像素,或者不是 8 位。
-
如何知道这些数据的特征?
-
screenData是一个纯粹的png image。我认为您不想浏览字节并自己解码。一个简单的选择可能是使用plt.imread。
标签: python matplotlib binary-data data-conversion