0

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.

enter image description here

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

5
  • Have you tried 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). Commented May 24, 2017 at 6:37
  • I tried like you said and I get the following error 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) Commented May 24, 2017 at 6:50
  • I should mention, if your array size is 77878, then screen x,y should be factors of this. But the factors of 77878 are 1693 x 46, which is a weird screen size, so the data you are getting might contain more information than just screen pixels, or it is not 8 bit. Commented May 24, 2017 at 6:56
  • How to know characteristics of this data? Commented May 24, 2017 at 7:29
  • The screenData is 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 use plt.imread. Commented May 24, 2017 at 7:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You are right. But I do not know how to do it. By the way I need to see all settings which on scope's display.
After your advice the following error occurred: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?
Because this 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?
No, I do not have any error when open .png (with feh). Or I do not understand you. Do you mean I should try open this file with io?
What I mean is if you get the same error if you use plt.imread("screen.png")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.