LRDPRDX
LRDPRDX

Reputation: 683

How to plot binary data in python?

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

Upvotes: 0

Views: 3450

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339250

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.

Upvotes: 1

Related Questions