First of all I know my question is frequently asked. But I have not found a solution in them.
I work with USBTMC to control oscilloscope. Here you can find more information about it. I am able to capture screen and write it into a file (see picture). But I want to plot screen every n secs in real time. I would like to use matplotlib.pyplot, for example.
Here is my code (with a desperate attempt to plot data with 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
After run this code I get ... look at the picture. enter image description here
1 Answer 1
Unfortunately I cannot test it, but you may try something like this
import io
screenData = scope.read_raw()
arrayData = plt.imread(io.BytesIO(screenData))
plt.imshow(arrayData)
plt.show()
I would like to note that for live plotting, it is probably better not to obtain the image from the scope's screen but the raw data. This should allow for much faster operation.
5 Comments
Traceback (most recent call last): File "./ScreenCapture.py", line 33, in <module> arrayData = plot.imread(io.BytesIO(screenData)) File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2177, in imread return _imread(*args, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1258, in imread return handler(fname) RuntimeError: _image_module::readpng: file not recognized as a PNG file. I think this oscilloscope does not support saving in png format. What if eps instead?outFile = open("screen.png", "wb"); outFile.write( screenData ) works according to you and also because you set scope.write("SAVE:IMAG:FILEF PNG"), I was under the impression that you do have a png image. Do you get the same error, when opening the saved file instead of the io-stream?.png (with feh). Or I do not understand you. Do you mean I should try open this file with io?plt.imread("screen.png")Explore related questions
See similar questions with these tags.
plt.imshow(strData), but first you need to do reshape on your data according to the screen size:strData.reshape(screen_y, screen_x), where 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)screenDatais a pure png image. I don't think you want to go through the bytes and decode it yourself. An easy option may be to useplt.imread.